<?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('risk_cap_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('risk_cap_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('version_id')->constrained('risk_cap_versions')->cascadeOnDelete();
            $table->foreignId('draw_id')->nullable()->constrained('draws')->nullOnDelete();
            $table->char('normalized_number', 4);
            $table->bigInteger('cap_amount');
            $table->string('cap_type', 16);
            $table->timestamps();

            $table->index(['version_id', 'draw_id', 'normalized_number'], 'idx_risk_cap_items_lookup');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('risk_cap_items');
        Schema::dropIfExists('risk_cap_versions');
    }
};
