<?php


if (!function_exists('aycore_schema_table_exists')) {


function aycore_schema_table_exists(PDO $pdo, string $table): bool {
    try {
        $stmt = $pdo->prepare(
            "SELECT 1 FROM INFORMATION_SCHEMA.TABLES
             WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = :t LIMIT 1"
        );
        $stmt->execute([':t' => $table]);
        return (bool)$stmt->fetchColumn();
    } catch (\Throwable $e) {
        error_log('[schema] table_exists ' . $table . ': ' . $e->getMessage());
        return false;
    }
}


function aycore_schema_column_exists(PDO $pdo, string $table, string $column): bool {
    try {
        $stmt = $pdo->prepare(
            "SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS
             WHERE TABLE_SCHEMA = DATABASE()
               AND TABLE_NAME   = :t
               AND COLUMN_NAME  = :c LIMIT 1"
        );
        $stmt->execute([':t' => $table, ':c' => $column]);
        return (bool)$stmt->fetchColumn();
    } catch (\Throwable $e) {
        error_log('[schema] column_exists ' . $table . '.' . $column . ': ' . $e->getMessage());
        return false;
    }
}


function aycore_schema_ensure_column(PDO $pdo, string $table, string $column, string $definition): void {
    if (!aycore_schema_table_exists($pdo, $table)) {

        return;
    }
    if (aycore_schema_column_exists($pdo, $table, $column)) {
        return;
    }
    try {
        $sanT = preg_replace('/[^A-Za-z0-9_]/', '', $table);
        $sanC = preg_replace('/[^A-Za-z0-9_]/', '', $column);
        $pdo->exec("ALTER TABLE `{$sanT}` ADD COLUMN `{$sanC}` {$definition}");
    } catch (\PDOException $e) {

        if (!empty($e->errorInfo[1]) && (int)$e->errorInfo[1] === 1060) return;
        error_log('[schema] ensure_column ' . $table . '.' . $column . ': ' . $e->getMessage());
    } catch (\Throwable $e) {
        error_log('[schema] ensure_column ' . $table . '.' . $column . ': ' . $e->getMessage());
    }
}


function aycore_schema_ready(PDO $pdo): void {
    if (!empty($_SESSION['__aycore_schema_ok'])) {
        return;
    }


    aycore_schema_ensure_column(
        $pdo, 'marzban_panel', 'emergency_panel_status',
        "VARCHAR(50) NOT NULL DEFAULT 'off_emergency_panel'"
    );
    aycore_schema_ensure_column(
        $pdo, 'marzban_panel', 'national_net_status',
        "VARCHAR(50) NOT NULL DEFAULT 'off_national_net'"
    );
    aycore_schema_ensure_column(
        $pdo, 'marzban_panel', 'emergency_source_panel',
        "VARCHAR(191) NULL"
    );
    aycore_schema_ensure_column(
        $pdo, 'marzban_panel', 'stock_source_panel',
        "VARCHAR(191) NULL"
    );

    // AYCore: keep modern 3x-ui token mode and legacy cookie mode
    // explicitly separated instead of inferring the mode from mixed credentials.
    aycore_schema_ensure_column($pdo, 'marzban_panel', 'xui_mode', "VARCHAR(20) NOT NULL DEFAULT 'legacy'");
    aycore_schema_ensure_column($pdo, 'marzban_panel', 'xui_inbound_snapshot', "MEDIUMTEXT NULL");
    aycore_schema_ensure_column($pdo, 'marzban_panel', 'xui_last_sync', "INT UNSIGNED NOT NULL DEFAULT 0");
    aycore_schema_ensure_column($pdo, 'marzban_panel', 'xui_auto_inbounds', "VARCHAR(20) NOT NULL DEFAULT '0'");
    try {
        $pdo->exec("UPDATE marzban_panel SET xui_mode='modern' WHERE type='x-ui_single' AND COALESCE(xui_api_token,'')<>'' AND (xui_mode IS NULL OR xui_mode='' OR xui_mode='legacy')");
    } catch (\Throwable $e) {
        error_log('[schema] xui mode migration: ' . $e->getMessage());
    }

    // Rebecca: API-key first integration, service-driven provisioning and live OpenAPI discovery.
    aycore_schema_ensure_column($pdo, 'marzban_panel', 'rb_auth_mode', "VARCHAR(30) NOT NULL DEFAULT 'api_key'");
    aycore_schema_ensure_column($pdo, 'marzban_panel', 'rb_access_token', "TEXT NULL");
    aycore_schema_ensure_column($pdo, 'marzban_panel', 'rb_default_service_id', "INT NOT NULL DEFAULT 0");
    aycore_schema_ensure_column($pdo, 'marzban_panel', 'rb_service_snapshot', "MEDIUMTEXT NULL");
    aycore_schema_ensure_column($pdo, 'marzban_panel', 'rb_last_sync', "INT UNSIGNED NOT NULL DEFAULT 0");
    aycore_schema_ensure_column($pdo, 'marzban_panel', 'rb_openapi_cache', "MEDIUMTEXT NULL");
    aycore_schema_ensure_column($pdo, 'marzban_panel', 'rb_openapi_last_sync', "INT UNSIGNED NOT NULL DEFAULT 0");
    aycore_schema_ensure_column($pdo, 'marzban_panel', 'rb_capabilities', "MEDIUMTEXT NULL");


    // AY Operations — operational history. Never store raw secrets in meta_json.
    try {
        $pdo->exec("CREATE TABLE IF NOT EXISTS itz_ops_history (
            id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
            admin_user VARCHAR(190) NULL,
            action_name VARCHAR(120) NOT NULL,
            status_name VARCHAR(30) NOT NULL DEFAULT 'ok',
            meta_json MEDIUMTEXT NULL,
            created_at INT UNSIGNED NOT NULL DEFAULT 0,
            PRIMARY KEY (id),
            KEY idx_created_at (created_at),
            KEY idx_action (action_name)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
    } catch (\Throwable $e) { error_log('[schema] itz_ops_history: ' . $e->getMessage()); }

    // Web iTz Core Settings audit log. The UI can safely rollback core setting changes.
    try {
        $pdo->exec("CREATE TABLE IF NOT EXISTS itz_setting_history (
            id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
            field_name VARCHAR(191) NOT NULL,
            old_value MEDIUMTEXT NULL,
            new_value MEDIUMTEXT NULL,
            changed_by VARCHAR(191) NULL,
            created_at INT UNSIGNED NOT NULL DEFAULT 0,
            INDEX idx_field_time (field_name, created_at)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
    } catch (\Throwable $e) {
        error_log('[schema] itz_setting_history: ' . $e->getMessage());
    }

    if (!aycore_schema_table_exists($pdo, 'crypto_wallets')) {
        try {
            $pdo->exec("CREATE TABLE IF NOT EXISTS crypto_wallets (
                id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                currency VARCHAR(20) NOT NULL,
                network VARCHAR(20) NOT NULL,
                wallet_address VARCHAR(255) NOT NULL DEFAULT '',
                label VARCHAR(255) DEFAULT NULL,
                enabled TINYINT(1) NOT NULL DEFAULT 0,
                min_irt BIGINT NOT NULL DEFAULT 20000,
                max_irt BIGINT NOT NULL DEFAULT 100000000,
                cashback_percent DECIMAL(6,2) NOT NULL DEFAULT 0,
                rate_irt_override DECIMAL(20,4) DEFAULT NULL,
                created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
                updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
                UNIQUE KEY uniq_currency (currency)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");

            $seed = $pdo->prepare(
                "INSERT IGNORE INTO crypto_wallets (currency, network, wallet_address, label, enabled)
                 VALUES (?,?,?,?,0)"
            );
            foreach ([
                ['TRX',         'TRON', '', 'ترون (TRX)'],
                ['TON',         'TON',  '', 'تون (TON)'],
                ['USDT_TRC20',  'TRON', '', 'تتر روی شبکه ترون (USDT-TRC20)'],
                ['USDT_TON',    'TON',  '', 'تتر روی شبکه تون (USDT-TON)'],
            ] as $r) $seed->execute($r);
        } catch (\Throwable $e) {
            error_log('[schema] crypto_wallets: ' . $e->getMessage());
        }
    }


    if (!aycore_schema_table_exists($pdo, 'app')) {
        try {
            $pdo->exec("CREATE TABLE IF NOT EXISTS app (
                id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                name VARCHAR(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
                link VARCHAR(500) NOT NULL
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci");
        } catch (\Throwable $e) {
            error_log('[schema] app: ' . $e->getMessage());
        }
    }


    if (!aycore_schema_table_exists($pdo, 'shopSetting')) {
        try {
            $pdo->exec("CREATE TABLE IF NOT EXISTS shopSetting (
                Namevalue VARCHAR(500) PRIMARY KEY NOT NULL,
                value TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci");

            $seed = $pdo->prepare(
                "INSERT IGNORE INTO shopSetting (Namevalue, value) VALUES (?, ?)"
            );
            foreach ([
                ['statusextra',         'offextra'],
                ['statusdirectpabuy',   'ondirectbuy'],
                ['statustimeextra',     'ontimeextraa'],
                ['statusdisorder',      'offdisorder'],
                ['statuschangeservice', 'onstatus'],
                ['statusshowprice',     'offshowprice'],
                ['configshow',          'onconfig'],
                ['backserviecstatus',   'on'],
                ['minbalancebuybulk',   '0'],
                ['customvolmef',        '4000'],
                ['customvolmen',        '4000'],
                ['customvolmen2',       '4000'],
                ['customtimepricef',    '4000'],
                ['customtimepricen',    '4000'],
                ['customtimepricen2',   '4000'],
            ] as $r) $seed->execute($r);
        } catch (\Throwable $e) {
            error_log('[schema] shopSetting: ' . $e->getMessage());
        }
    }


    $_SESSION['__aycore_schema_ok'] = true;
}

}

