<?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::create('pension_cases', function (Blueprint $table) {
            $table->id();
            $table->uuid('slug')->unique()->index();
            $table->foreignId('client_id')->constrained()->cascadeOnDelete();

            // Salesforce / external identifiers
            $table->string('sf_case_id')->nullable();
            $table->string('case_number')->nullable();

            // Ownership / assignment
            $table->string('case_owner')->nullable();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('case_last_modified_by')->nullable();
            $table->string('claims_writer')->nullable();
            $table->string('file_investigator')->nullable();

            // Timestamps
            $table->timestamp('opened_at')->nullable();
            $table->timestamp('case_last_modified_at')->nullable();
            $table->timestamp('closed_at')->nullable();

            // Status flags
            $table->boolean('is_open')->default(true);
            $table->boolean('is_closed')->default(false);

            // Claim classification
            $table->string('claim_subject')->nullable();
            $table->string('claim_basis')->nullable();
            $table->string('priority')->nullable();

            // Settlement & claim statuses
            $table->string('settlement_status')->nullable();
            $table->string('bank_claim_status')->nullable();
            $table->string('fos_claim_status')->nullable();
            $table->string('fscs_claim_status')->nullable();
            $table->string('client_settlement_instruction')->nullable();

            // Narrative fields
            $table->text('reasons_for_not_proceeding')->nullable();
            $table->longText('additional_claims_info')->nullable();
            $table->longText('detail_of_outstanding_evidence')->nullable();

            // Investment details
            $table->string('underlying_investment')->nullable();
            $table->string('investment_library')->nullable();
            $table->string('pcp_subject')->nullable();

            // FSCS contact
            $table->string('fscs_contact_name')->nullable();
            $table->string('fscs_contact_reference')->nullable();
            $table->string('fscs_reference')->nullable();

            // Client identifiers
            $table->string('national_insurance')->nullable();
            $table->string('cmc_client_reference')->nullable();
            $table->string('car_reg')->nullable();
            $table->string('atc')->nullable();

            // Documentation references
            $table->string('tob_issued_by')->nullable();
            $table->string('sar_to')->nullable();
            $table->string('lead_source_cpf')->nullable();
            $table->string('fee_structure')->nullable();

            // Client flags
            $table->boolean('vulnerable_client_flag')->default(false);
            $table->text('vulnerable_client_details')->nullable();
            $table->boolean('special_contact_requirements_flag')->default(false);
            $table->text('special_contact_requirements_details')->nullable();
            $table->boolean('sms_opt_out')->default(false);
            $table->boolean('do_not_contact')->default(false);
            $table->boolean('ifa_referred')->default(false);
            $table->boolean('ifa_refer_opted_out')->default(false);
            $table->boolean('client_in_dispute')->default(false);
            $table->boolean('sar_sent')->default(false);
            $table->boolean('sar_received')->default(false);
            $table->boolean('fee_capping_flag')->default(false);
            $table->boolean('atc_received')->default(false);

            // Financial
            $table->decimal('settlement_to_date_raw', 15, 2)->nullable();
            $table->decimal('settlement_to_date_new', 15, 2)->nullable();
            $table->decimal('projected_future_settlement', 15, 2)->nullable();
            $table->decimal('quantum', 15, 2)->nullable();
            $table->decimal('cpfc_fee_percentage', 8, 4)->nullable();
            $table->decimal('cpfc_fee_total_nett', 15, 2)->nullable();
            $table->decimal('our_fees_percentage_raw', 8, 4)->nullable();

            // Key dates
            $table->date('issued_tob_date')->nullable();
            $table->date('tob_in_date')->nullable();
            $table->date('ifa_referred_date')->nullable();
            $table->date('application_submitted_date')->nullable();
            $table->date('date_requested_evidence')->nullable();
            $table->date('fos_allocated_date')->nullable();
            $table->date('fos_app_submitted_date')->nullable();
            $table->date('fos_app_written_sent_to_client_date')->nullable();
            $table->date('fos_complaint_letter_sent_to_client_date')->nullable();
            $table->date('fos_initial_complaint_sent_date')->nullable();
            $table->date('fos_under_review_with_investigator_date')->nullable();
            $table->date('fscs_allocated_date')->nullable();
            $table->date('sar_received_date')->nullable();
            $table->date('sar_reviewed_scanned_date')->nullable();
            $table->date('application_out_for_signature_date')->nullable();
            $table->date('allocated_to_investigator_date')->nullable();

            $table->timestamps();

            $table->index('client_id');
            $table->index('sf_case_id');
            $table->index('case_number');
            $table->index('national_insurance');
            $table->index('cmc_client_reference');
            $table->index('is_open');
        });
    }

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