<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('odds_versions', function (Blueprint $table) {
            $table->id();
            $table->unsignedInteger('version_no');
            $table->string('status', 16);
            $table->timestamp('effective_at')->nullable();
            $table->foreignId('updated_by')->nullable()->constrained('admin_users')->nullOnDelete();
            $table->string('reason', 255)->nullable();
            $table->timestamps();
        });

        Schema::create('odds_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('version_id')->constrained('odds_versions')->cascadeOnDelete();
            $table->string('play_code', 32);
            $table->string('prize_scope', 32);
            $table->bigInteger('odds_value')->default(0);
            $table->decimal('rebate_rate', 8, 4)->default(0);
            $table->decimal('commission_rate', 8, 4)->default(0);
            $table->string('currency_code', 16);
            $table->json('extra_config_json')->nullable();
            $table->timestamps();

            $table->index(['version_id', 'play_code'], 'idx_odds_items_version_play');
            $table->unique(
                ['version_id', 'play_code', 'prize_scope', 'currency_code'],
                'uk_odds_items_version_play_prize_currency'
            );
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('odds_items');
        Schema::dropIfExists('odds_versions');
    }
};
