<?php
/**
 * نصب جداول HVAC Reward Engine (v17.8)
 * php run-service-reward-install.php
 */
declare(strict_types=1);

if ( ! defined( 'ACC_BASE_PATH' ) ) {
    define( 'ACC_BASE_PATH', __DIR__ );
    require __DIR__ . '/standalone/bootstrap.php';
}

header( 'Content-Type: application/json; charset=utf-8' );
global $wpdb;
$pre = $wpdb->prefix;
$charset = 'DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';

$ddl = [
    "CREATE TABLE IF NOT EXISTS {$pre}svc_reward_accounts (
        id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
        actor_type VARCHAR(20) NOT NULL DEFAULT 'customer',
        user_id BIGINT UNSIGNED NULL,
        phone VARCHAR(20) NULL,
        display_name VARCHAR(200) NULL,
        tier VARCHAR(30) NOT NULL DEFAULT 'normal',
        points_total BIGINT NOT NULL DEFAULT 0,
        points_available BIGINT NOT NULL DEFAULT 0,
        hvac_credits BIGINT NOT NULL DEFAULT 0,
        lifetime_earned BIGINT NOT NULL DEFAULT 0,
        rank_score BIGINT NOT NULL DEFAULT 0,
        referral_code VARCHAR(12) NOT NULL DEFAULT '',
        referred_by BIGINT UNSIGNED NULL,
        created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
        updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        PRIMARY KEY (id),
        UNIQUE KEY referral_code (referral_code),
        KEY actor_user (actor_type, user_id),
        KEY actor_phone (actor_type, phone),
        KEY rank_score (rank_score)
    ) {$charset}",

    "CREATE TABLE IF NOT EXISTS {$pre}svc_reward_ledger (
        id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
        account_id BIGINT UNSIGNED NOT NULL,
        direction VARCHAR(10) NOT NULL DEFAULT 'earn',
        event_key VARCHAR(60) NOT NULL DEFAULT '',
        points INT NOT NULL DEFAULT 0,
        credits INT NOT NULL DEFAULT 0,
        order_value DECIMAL(14,2) NOT NULL DEFAULT 0,
        tier_mult DECIMAL(5,2) NOT NULL DEFAULT 1,
        quality_mult DECIMAL(5,2) NOT NULL DEFAULT 1,
        ref_type VARCHAR(40) NULL,
        ref_id BIGINT UNSIGNED NULL,
        dedupe_key VARCHAR(80) NULL,
        note VARCHAR(255) NULL,
        created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
        PRIMARY KEY (id),
        KEY account_id (account_id),
        KEY dedupe (account_id, event_key, dedupe_key)
    ) {$charset}",

    "CREATE TABLE IF NOT EXISTS {$pre}svc_reward_badges (
        id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
        account_id BIGINT UNSIGNED NOT NULL,
        badge_key VARCHAR(40) NOT NULL,
        earned_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
        PRIMARY KEY (id),
        UNIQUE KEY account_badge (account_id, badge_key)
    ) {$charset}",
];

foreach ( $ddl as $sql ) {
    $wpdb->query( $sql );
}

update_option( 'acc_engine_db_version', '17.8' );

echo json_encode( [
    'success' => true,
    'db_version' => get_option( 'acc_engine_db_version' ),
    'tables' => [
        'svc_reward_accounts' => (bool) $wpdb->get_var( "SHOW TABLES LIKE '{$pre}svc_reward_accounts'" ),
        'svc_reward_ledger'   => (bool) $wpdb->get_var( "SHOW TABLES LIKE '{$pre}svc_reward_ledger'" ),
        'svc_reward_badges'   => (bool) $wpdb->get_var( "SHOW TABLES LIKE '{$pre}svc_reward_badges'" ),
    ],
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT );
