<?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('families', function (Blueprint $table) {
            $table->id();
            $table->branchColumn();
            $table->string('family_code', 40);

            // Father
            $table->string('father_name')->nullable();
            $table->string('father_cnic', 20)->nullable();
            $table->string('father_mobile', 30)->nullable();
            $table->string('father_occupation')->nullable();

            // Mother
            $table->string('mother_name')->nullable();
            $table->string('mother_cnic', 20)->nullable();
            $table->string('mother_mobile', 30)->nullable();

            // Guardian (if different)
            $table->string('guardian_name')->nullable();
            $table->string('guardian_cnic', 20)->nullable();
            $table->string('guardian_relation', 50)->nullable();

            $table->string('email')->nullable();
            $table->text('address')->nullable();
            $table->string('emergency_contact', 30)->nullable();

            // Optional linked parent portal login.
            $table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();

            $table->auditColumns();
            $table->timestamps();
            $table->softDeletes();

            $table->index('father_mobile');
            $table->unique(['branch_id', 'family_code']);
        });
    }

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