<?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('wills_cases', function (Blueprint $table) {
            $table->foreignId('status_id')->nullable()->constrained('statuses')->nullOnDelete()->after('client_id');

            // Case assignment
            $table->string('rep')->nullable()->after('status_id');
            $table->string('case_owner')->nullable()->after('rep');

            // Products
            $table->json('products')->nullable()->after('case_owner');

            // Financial
            $table->string('drafting_contact')->nullable()->after('products');
            $table->string('our_fee')->nullable()->after('drafting_contact');
            $table->date('awt_invoice_issued_at')->nullable()->after('our_fee');
            $table->string('our_fee_status')->nullable()->after('awt_invoice_issued_at');
            $table->string('external_draft_fee')->nullable()->after('our_fee_status');

            // Communication milestones
            $table->date('welcome_letter_sent_at')->nullable()->after('external_draft_fee');
            $table->text('docs_outstanding_from_reps')->nullable()->after('welcome_letter_sent_at');
            $table->date('introduction_call_at')->nullable()->after('docs_outstanding_from_reps');
            $table->string('intro_call_by')->nullable()->after('introduction_call_at');

            // Document workflow
            $table->date('trust_docs_instructed_at')->nullable()->after('intro_call_by');
            $table->date('in_house_instructed_at')->nullable()->after('trust_docs_instructed_at');
            $table->date('drafts_received_at')->nullable()->after('in_house_instructed_at');
            $table->date('drafts_issued_to_client_at')->nullable()->after('drafts_received_at');
            $table->date('master_copies_signed_at')->nullable()->after('drafts_issued_to_client_at');
            $table->date('master_copies_received_at')->nullable()->after('master_copies_signed_at');
            $table->date('date_on_document_signed')->nullable()->after('master_copies_received_at');

            // Registration
            $table->boolean('documents_requiring_registration')->nullable()->after('date_on_document_signed');
            $table->json('documents_to_register_aiker')->nullable()->after('documents_requiring_registration');
            $table->json('documents_to_register_external')->nullable()->after('documents_to_register_aiker');
            $table->json('registration_completed_aiker')->nullable()->after('documents_to_register_external');
            $table->json('registration_completed_external')->nullable()->after('registration_completed_aiker');

            // Post-completion
            $table->date('client_letters_from_hmrc_at')->nullable()->after('registration_completed_external');
            $table->date('annual_minutes_task_set_at')->nullable()->after('client_letters_from_hmrc_at');
        });
    }

    public function down(): void {
        Schema::table('wills_cases', function (Blueprint $table) {
            $table->dropForeign(['status_id']);
            $table->dropColumn([
                'status_id',
                'rep',
                'case_owner',
                'products',
                'drafting_contact',
                'our_fee',
                'awt_invoice_issued_at',
                'our_fee_status',
                'external_draft_fee',
                'welcome_letter_sent_at',
                'docs_outstanding_from_reps',
                'introduction_call_at',
                'intro_call_by',
                'trust_docs_instructed_at',
                'in_house_instructed_at',
                'drafts_received_at',
                'drafts_issued_to_client_at',
                'master_copies_signed_at',
                'master_copies_received_at',
                'date_on_document_signed',
                'documents_requiring_registration',
                'documents_to_register_aiker',
                'documents_to_register_external',
                'registration_completed_aiker',
                'registration_completed_external',
                'client_letters_from_hmrc_at',
                'annual_minutes_task_set_at',
            ]);
        });
    }
};
