<?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
    {
        // Parent / guardian account — a first-class household record. Every
        // student belongs to one; siblings share it, which drives the family
        // (sibling) fee discount.
        Schema::create('guardians', function (Blueprint $table) {
            $table->id();
            $table->foreignId('branch_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete(); // optional login account
            $table->string('name');                 // father / guardian full name
            $table->string('relation')->default('father'); // father|mother|guardian
            $table->string('occupation')->nullable();
            $table->string('phone')->nullable();
            $table->string('email')->nullable();
            $table->string('national_id')->nullable();
            $table->string('address')->nullable();
            $table->string('mother_name')->nullable();
            $table->string('mother_phone')->nullable();
            $table->string('photo')->nullable();     // base64 data URI
            $table->text('notes')->nullable();
            $table->string('status')->default('active');
            $table->timestamps();
            $table->softDeletes();
            $table->index(['branch_id', 'phone']);
        });

        Schema::table('students', function (Blueprint $table) {
            $table->foreignId('guardian_id')->nullable()->after('branch_id')->constrained('guardians')->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('students', function (Blueprint $table) {
            $table->dropConstrainedForeignId('guardian_id');
        });
        Schema::dropIfExists('guardians');
    }
};
