<?php
/**
 * Platform v1 — وایت‌لیبل · API Key · Webhook · لید دمو
 * php run-platform-upgrade-v1.php
 */
declare(strict_types=1);

define('ACC_BASE_PATH', __DIR__);
require_once __DIR__ . '/standalone/bootstrap.php';

global $wpdb;
$pre = $wpdb->prefix;
$charset = 'DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';
$done = [];

function plat_col( $wpdb, string $table, string $col ): bool {
    return (bool) $wpdb->get_row("SHOW COLUMNS FROM `{$table}` LIKE '{$col}'");
}

function plat_add( $wpdb, string $table, string $col, string $ddl, array &$done ): void {
    if (!plat_col($wpdb, $table, $col)) {
        $wpdb->query("ALTER TABLE `{$table}` ADD COLUMN {$ddl}");
        $done[] = "{$table}.{$col}";
    }
}

$st = $pre . 'arag_shop_settings';
plat_add($wpdb, $st, 'brand_name', "brand_name VARCHAR(120) NOT NULL DEFAULT 'عرقیات' AFTER id", $done);
plat_add($wpdb, $st, 'logo_url', "logo_url VARCHAR(500) NOT NULL DEFAULT '' AFTER brand_name", $done);
plat_add($wpdb, $st, 'favicon_url', "favicon_url VARCHAR(500) NOT NULL DEFAULT '' AFTER logo_url", $done);
plat_add($wpdb, $st, 'primary_color', "primary_color VARCHAR(20) NOT NULL DEFAULT '#1b4332' AFTER favicon_url", $done);
plat_add($wpdb, $st, 'accent_color', "accent_color VARCHAR(20) NOT NULL DEFAULT '#52b788' AFTER primary_color", $done);
plat_add($wpdb, $st, 'support_phone', "support_phone VARCHAR(20) NOT NULL DEFAULT '' AFTER accent_color", $done);
plat_add($wpdb, $st, 'support_email', "support_email VARCHAR(120) NOT NULL DEFAULT '' AFTER support_phone", $done);
plat_add($wpdb, $st, 'platform_plan', "platform_plan VARCHAR(32) NOT NULL DEFAULT 'enterprise' AFTER support_email", $done);
plat_add($wpdb, $st, 'platform_tagline', "platform_tagline VARCHAR(240) NOT NULL DEFAULT 'پلتفرم فروش آنلاین ACC' AFTER platform_plan", $done);

$wpdb->query("CREATE TABLE IF NOT EXISTS {$pre}acc_shop_api_keys (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    label VARCHAR(120) NOT NULL DEFAULT '',
    key_hash CHAR(64) NOT NULL,
    key_prefix VARCHAR(20) NOT NULL DEFAULT '',
    scopes_json JSON NULL,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    last_used_at DATETIME NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    UNIQUE KEY key_hash (key_hash)
) {$charset}");
$done[] = 'acc_shop_api_keys';

$wpdb->query("CREATE TABLE IF NOT EXISTS {$pre}acc_shop_webhooks (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    label VARCHAR(120) NOT NULL DEFAULT '',
    url VARCHAR(500) NOT NULL,
    secret VARCHAR(64) NOT NULL DEFAULT '',
    events_json JSON NULL,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
) {$charset}");
$done[] = 'acc_shop_webhooks';

$wpdb->query("CREATE TABLE IF NOT EXISTS {$pre}acc_shop_webhook_log (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    webhook_id BIGINT UNSIGNED NOT NULL,
    event VARCHAR(60) NOT NULL DEFAULT '',
    http_status INT NOT NULL DEFAULT 0,
    response_body TEXT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    KEY webhook_id (webhook_id)
) {$charset}");
$done[] = 'acc_shop_webhook_log';

$wpdb->query("CREATE TABLE IF NOT EXISTS {$pre}acc_platform_leads (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    name VARCHAR(120) NOT NULL DEFAULT '',
    phone VARCHAR(20) NOT NULL DEFAULT '',
    email VARCHAR(120) NOT NULL DEFAULT '',
    shop_name VARCHAR(160) NOT NULL DEFAULT '',
    plan_key VARCHAR(32) NOT NULL DEFAULT 'pro',
    message TEXT NULL,
    source VARCHAR(40) NOT NULL DEFAULT 'platform',
    status VARCHAR(20) NOT NULL DEFAULT 'new',
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
) {$charset}");
$done[] = 'acc_platform_leads';

update_option('acc_shop_platform_version', '1.0', false);

header('Content-Type: application/json; charset=utf-8');
echo wp_json_encode(['success' => true, 'version' => '1.0', 'done' => $done], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
