-- =========================================================
-- MIGRATION: Admin accounts + Payment API config + Virtual accounts
-- =========================================================

-- Separate admins table — never shares login with regular users
CREATE TABLE IF NOT EXISTS admins (
    id              INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name            VARCHAR(100)     NOT NULL,
    email           VARCHAR(150)     NOT NULL UNIQUE,
    password_hash   VARCHAR(255)     NOT NULL,
    role            ENUM('superadmin','admin','support') NOT NULL DEFAULT 'admin',
    is_active       TINYINT(1)       NOT NULL DEFAULT 1,
    last_login_at   DATETIME         DEFAULT NULL,
    created_at      DATETIME         NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Admin-configurable payment/virtual-account provider settings
-- (edit via admin dashboard — no code changes needed to swap providers)
CREATE TABLE IF NOT EXISTS payment_apis (
    id              INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    provider_name   VARCHAR(60)      NOT NULL,
    provider_slug   VARCHAR(40)      NOT NULL UNIQUE,   -- e.g. 'ncwallet', 'paystack'
    base_url        VARCHAR(255)     DEFAULT NULL,
    api_key         VARCHAR(255)     DEFAULT NULL,
    secret_key      VARCHAR(255)     DEFAULT NULL,
    webhook_secret  VARCHAR(255)     DEFAULT NULL,
    extra_config    TEXT             DEFAULT NULL,       -- JSON, e.g. {"trnx_pin":"1234"}
    is_active       TINYINT(1)       NOT NULL DEFAULT 0,
    is_default      TINYINT(1)       NOT NULL DEFAULT 0,
    created_at      DATETIME         NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at      DATETIME         NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Seed an NCWallet row so it shows up in admin/payment-apis.php ready to configure
INSERT INTO payment_apis (provider_name, provider_slug, base_url, is_active, is_default)
VALUES ('NCWallet Africa', 'ncwallet', 'https://www.ncwallet.africa', 0, 1)
ON DUPLICATE KEY UPDATE provider_name = provider_name;

-- Real bank virtual account details (NUBAN), separate from our internal
-- display-only account_number already on the wallets table
ALTER TABLE wallets
    ADD COLUMN virtual_account_number VARCHAR(20)  DEFAULT NULL AFTER account_number,
    ADD COLUMN virtual_account_bank   VARCHAR(60)  DEFAULT NULL AFTER virtual_account_number,
    ADD COLUMN virtual_account_name  VARCHAR(150) DEFAULT NULL AFTER virtual_account_bank,
    ADD COLUMN virtual_account_ref    VARCHAR(64)  DEFAULT NULL AFTER virtual_account_name,
    ADD COLUMN wallet_provider        VARCHAR(30)  DEFAULT NULL AFTER virtual_account_ref,
    ADD UNIQUE KEY uniq_virtual_account (virtual_account_number);
