-- ── deposits: add missing columns ──────────────────────────
ALTER TABLE `deposits`
    ADD COLUMN IF NOT EXISTS `gateway_code`    VARCHAR(50)    NULL   AFTER `method`,
    ADD COLUMN IF NOT EXISTS `unique_amount`   DECIMAL(15,4)  NULL   AFTER `gateway_code`,
    ADD COLUMN IF NOT EXISTS `gateway_data`    TEXT           NULL   AFTER `unique_amount`,
    ADD COLUMN IF NOT EXISTS `expires_at`      TIMESTAMP      NULL   AFTER `gateway_data`,
    ADD COLUMN IF NOT EXISTS `approved_at`     TIMESTAMP      NULL   AFTER `expires_at`;

-- ── users: add missing columns ──────────────────────────────
ALTER TABLE `users`
    ADD COLUMN IF NOT EXISTS `whatsapp`        VARCHAR(100)   NULL   AFTER `phone`,
    ADD COLUMN IF NOT EXISTS `facebook`        VARCHAR(255)   NULL   AFTER `whatsapp`,
    ADD COLUMN IF NOT EXISTS `profile_photo`   VARCHAR(500)   NULL   AFTER `facebook`;

-- ── agent_applications: add whatsapp / facebook ─────────────
ALTER TABLE `agent_applications`
    ADD COLUMN IF NOT EXISTS `whatsapp`  VARCHAR(100)  NULL  AFTER `phone`,
    ADD COLUMN IF NOT EXISTS `facebook`  VARCHAR(255)  NULL  AFTER `whatsapp`;

-- ── payment_gateways table ───────────────────────────────────
CREATE TABLE IF NOT EXISTS `payment_gateways` (
    `id`          BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `code`        VARCHAR(50)   UNIQUE NOT NULL,
    `name`        VARCHAR(100)  NOT NULL,
    `description` TEXT          NULL,
    `icon`        VARCHAR(255)  NULL,
    `is_enabled`  TINYINT(1)    DEFAULT 0,
    `settings`    TEXT          NULL,
    `min_amount`  DECIMAL(15,4) DEFAULT 1.0000,
    `max_amount`  DECIMAL(15,4) DEFAULT 10000.0000,
    `sort_order`  INT           DEFAULT 0,
    `created_at`  TIMESTAMP     DEFAULT CURRENT_TIMESTAMP,
    `updated_at`  TIMESTAMP     DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB;

INSERT IGNORE INTO `payment_gateways`
    (`code`,`name`,`description`,`icon`,`is_enabled`,`settings`,`min_amount`,`max_amount`,`sort_order`)
VALUES
('manual','Manual USDT (TRC20)',
 'Send USDT to our wallet and submit transaction hash. Admin verifies within 24h.',
 '💰', 1, '{"wallet_address":""}', 1.0000, 10000.0000, 1),
('oxapay','OxaPay (Auto)',
 'Instant automated crypto payment. Supports USDT, BTC, ETH and more.',
 '⚡', 0, '{"api_key":""}', 1.0000, 10000.0000, 2),
('paygate_to','PayGate.to (Auto)',
 'Auto-detected USDT TRC20 deposit via PayGate.to unique address.',
 '🚀', 0, '{"wallet_address":""}', 1.0000, 10000.0000, 3);

-- ── personal_access_tokens (Sanctum) ────────────────────────
CREATE TABLE IF NOT EXISTS `personal_access_tokens` (
    `id`             BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `tokenable_type` VARCHAR(255) NOT NULL,
    `tokenable_id`   BIGINT UNSIGNED NOT NULL,
    `name`           VARCHAR(255) NOT NULL,
    `token`          VARCHAR(64)  UNIQUE NOT NULL,
    `abilities`      TEXT         NULL,
    `last_used_at`   TIMESTAMP    NULL,
    `expires_at`     TIMESTAMP    NULL,
    `created_at`     TIMESTAMP    NULL,
    `updated_at`     TIMESTAMP    NULL,
    INDEX `pat_tokenable_idx` (`tokenable_type`,`tokenable_id`)
) ENGINE=InnoDB;
