<?php

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

return new class extends Migration {
    /**
     * Run the migrations.
     */
    public function up(): void {
        Schema::table('further_information', function (Blueprint $table) {
            // Section 7a care-background capture fields
            $table->string('name_before_care')->nullable()->after('legal_name_change');
            $table->string('mothers_name')->nullable()->after('name_before_care');
            $table->string('fathers_name')->nullable()->after('mothers_name');

            $table->string('address_prior_line_1')->nullable()->after('fathers_name');
            $table->string('address_prior_line_2')->nullable()->after('address_prior_line_1');
            $table->string('address_prior_line_3')->nullable()->after('address_prior_line_2');
            $table->string('address_prior_postcode')->nullable()->after('address_prior_line_3');

            $table->string('gp_surgery')->nullable()->after('address_prior_postcode');
            $table->json('hospitals')->nullable()->after('gp_surgery');

            // Links to the rows mirrored into the client's other features. Kept so
            // re-saving Section 7a updates the same row instead of duplicating it.
            $table->foreignId('name_before_care_previous_name_id')->nullable()->constrained('previous_names')->nullOnDelete();
            $table->foreignId('mother_relation_id')->nullable()->constrained('relations')->nullOnDelete();
            $table->foreignId('father_relation_id')->nullable()->constrained('relations')->nullOnDelete();
            $table->foreignId('address_prior_to_care_id')->nullable()->constrained('addresses')->nullOnDelete();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void {
        Schema::table('further_information', function (Blueprint $table) {
            $table->dropConstrainedForeignId('name_before_care_previous_name_id');
            $table->dropConstrainedForeignId('mother_relation_id');
            $table->dropConstrainedForeignId('father_relation_id');
            $table->dropConstrainedForeignId('address_prior_to_care_id');

            $table->dropColumn([
                'name_before_care',
                'mothers_name',
                'fathers_name',
                'address_prior_line_1',
                'address_prior_line_2',
                'address_prior_line_3',
                'address_prior_postcode',
                'gp_surgery',
                'hospitals',
            ]);
        });
    }
};
