<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('customer_risk_profiles', function (Blueprint $table): void {
            $table->string('score_band')->nullable()->after('score');
            $table->string('score_version', 30)->nullable()->after('score_band');
            $table->decimal('confidence', 5, 2)->nullable()->after('score_version');
            $table->boolean('thin_file')->default(true)->after('confidence');
            $table->boolean('manual_review_required')->default(false)->after('thin_file');
            $table->string('last_trigger', 80)->nullable()->after('manual_review_required');

            $table->index(['score_band', 'calculated_at'], 'risk_profile_band_date_idx');
            $table->index(['score_version', 'calculated_at'], 'risk_profile_version_date_idx');
        });

        Schema::create('customer_credit_score_histories', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('customer_id')->constrained()->cascadeOnDelete();
            $table->foreignId('branch_id')->constrained()->restrictOnDelete();
            $table->decimal('score', 8, 2)->nullable();
            $table->string('score_band')->nullable();
            $table->string('risk_level')->default('unrated');
            $table->decimal('confidence', 5, 2)->nullable();
            $table->boolean('thin_file')->default(true);
            $table->boolean('manual_review_required')->default(false);
            $table->string('score_version', 30);
            $table->string('trigger', 80);
            $table->json('component_scores')->nullable();
            $table->json('factors')->nullable();
            $table->text('explanation')->nullable();
            $table->timestamp('calculated_at');
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->index(['customer_id', 'calculated_at'], 'credit_hist_customer_date_idx');
            $table->index(['branch_id', 'calculated_at'], 'credit_hist_branch_date_idx');
            $table->index(['score_band', 'calculated_at'], 'credit_hist_band_date_idx');
            $table->index(['score_version', 'calculated_at'], 'credit_hist_version_date_idx');
        });

        Schema::table('loans', function (Blueprint $table): void {
            $table->foreignId('application_credit_score_history_id')
                ->nullable()
                ->after('customer_id')
                ->constrained('customer_credit_score_histories')
                ->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('loans', function (Blueprint $table): void {
            $table->dropConstrainedForeignId('application_credit_score_history_id');
        });

        Schema::dropIfExists('customer_credit_score_histories');

        Schema::table('customer_risk_profiles', function (Blueprint $table): void {
            $table->dropIndex('risk_profile_band_date_idx');
            $table->dropIndex('risk_profile_version_date_idx');
            $table->dropColumn([
                'score_band',
                'score_version',
                'confidence',
                'thin_file',
                'manual_review_required',
                'last_trigger',
            ]);
        });
    }
};
