-- Astrabank Upgrade v2 (Cards + Dashboard KPIs)
-- Run this if you already imported schema.sql before.

CREATE TABLE IF NOT EXISTS card_requests (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  user_id BIGINT NOT NULL,
  card_type ENUM('virtual') DEFAULT 'virtual',
  network ENUM('visa','mastercard') DEFAULT 'visa',
  tier ENUM('standard','premium') DEFAULT 'standard',
  status ENUM('pending','approved','rejected') DEFAULT 'pending',
  note VARCHAR(255) NULL,
  reviewed_by BIGINT NULL,
  reviewed_at DATETIME NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT fk_cr_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE,
  INDEX idx_cr_status (status)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS virtual_cards (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  user_id BIGINT NOT NULL,
  request_id BIGINT NULL,
  network ENUM('visa','mastercard') DEFAULT 'visa',
  cardholder_name VARCHAR(120) NOT NULL,
  pan_enc TEXT NOT NULL,
  last4 VARCHAR(4) NOT NULL,
  exp_month TINYINT NOT NULL,
  exp_year SMALLINT NOT NULL,
  cvv_enc TEXT NOT NULL,
  status ENUM('active','frozen','closed') DEFAULT 'active',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT fk_vc_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE,
  CONSTRAINT fk_vc_req FOREIGN KEY(request_id) REFERENCES card_requests(id) ON DELETE SET NULL,
  INDEX idx_vc_user (user_id)
) ENGINE=InnoDB;

-- Add app_key to config.php manually (see app/config/config.php)
