<?php
/**
 * 一次性修复脚本：补建生产库缺失的 settlement_batches 和 system_jobs 表。
 *
 * 用法：php fix-missing-tables.php
 * 执行完毕后删除此文件。
 */

require __DIR__ . '/vendor/autoload.php';

$app = require_once __DIR__ . '/bootstrap/app.php';
$app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

$created = [];
$skipped = [];

// ── 1. settlement_batches ──
if (! Schema::hasTable('settlement_batches')) {
    DB::statement('
        CREATE TABLE settlement_batches (
            id bigserial PRIMARY KEY,
            draw_id bigint NOT NULL REFERENCES draws(id) ON DELETE CASCADE,
            result_batch_id bigint NOT NULL REFERENCES draw_result_batches(id) ON DELETE CASCADE,
            settle_version integer NOT NULL DEFAULT 1,
            status varchar(32) NOT NULL,
            total_ticket_count integer NOT NULL DEFAULT 0,
            total_win_count integer NOT NULL DEFAULT 0,
            total_payout_amount bigint NOT NULL DEFAULT 0,
            total_jackpot_payout_amount bigint NOT NULL DEFAULT 0,
            review_status varchar(32) NOT NULL DEFAULT \'pending\',
            reviewed_by bigint REFERENCES admin_users(id) ON DELETE SET NULL,
            reviewed_at timestamp,
            review_remark varchar(255),
            paid_at timestamp,
            started_at timestamp,
            finished_at timestamp,
            created_at timestamp,
            updated_at timestamp
        )
    ');
    DB::statement('CREATE INDEX idx_settlement_batches_draw_version ON settlement_batches (draw_id, settle_version)');
    $created[] = 'settlement_batches';
} else {
    $skipped[] = 'settlement_batches (已存在)';
}

// 补录迁移记录
if (! DB::table('migrations')->where('migration', '2026_05_08_130008_create_settlement_and_jackpot_tables')->exists()) {
    DB::table('migrations')->insert([
        'migration' => '2026_05_08_130008_create_settlement_and_jackpot_tables',
        'batch' => 99,
    ]);
}

// ── 2. system_jobs ──
if (! Schema::hasTable('system_jobs')) {
    DB::statement('
        CREATE TABLE system_jobs (
            id bigserial PRIMARY KEY,
            job_key varchar(128) UNIQUE NOT NULL,
            name varchar(128) NOT NULL,
            schedule_cron varchar(64),
            is_enabled boolean NOT NULL DEFAULT true,
            last_started_at timestamp,
            last_finished_at timestamp,
            last_status varchar(32),
            created_at timestamp,
            updated_at timestamp
        )
    ');
    $created[] = 'system_jobs';
} else {
    $skipped[] = 'system_jobs (已存在)';
}

// 补录迁移记录
if (! DB::table('migrations')->where('migration', '2026_05_08_130009_create_report_audit_reconcile_tables')->exists()) {
    DB::table('migrations')->insert([
        'migration' => '2026_05_08_130009_create_report_audit_reconcile_tables',
        'batch' => 99,
    ]);
}

// ── 输出结果 ──
echo "=== 修复完成 ===\n";
echo "新建: " . (empty($created) ? '无' : implode(', ', $created)) . "\n";
echo "跳过: " . (empty($skipped) ? '无' : implode(', ', $skipped)) . "\n";
echo "\n接下来请执行: php artisan migrate --force\n";
