<?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('students', function (Blueprint $table) {
            $table->id();
            $table->branchColumn();
            $table->foreignId('family_id')->constrained('families')->cascadeOnUpdate()->restrictOnDelete();

            // Academic placement
            $table->foreignId('academic_session_id')->nullable()->constrained('academic_sessions')->nullOnDelete();
            $table->foreignId('class_id')->nullable()->constrained('classes')->nullOnDelete();
            $table->foreignId('section_id')->nullable()->constrained('sections')->nullOnDelete();
            $table->string('group', 50)->nullable();              // Science / Arts / Commerce

            // Identifiers
            $table->string('admission_number', 50);
            $table->string('registration_number', 50)->nullable();
            $table->string('roll_number', 50)->nullable();
            $table->string('gr_number', 50)->nullable();

            // Basic info
            $table->string('name');
            $table->string('name_urdu')->nullable();
            $table->string('photo')->nullable();
            $table->enum('gender', ['male', 'female', 'other'])->default('male');
            $table->date('date_of_birth')->nullable();
            $table->string('place_of_birth')->nullable();
            $table->string('religion', 50)->nullable();
            $table->string('nationality', 50)->default('Pakistani');
            $table->string('b_form_number', 30)->nullable();
            $table->string('blood_group', 5)->nullable();

            // Admission / status
            $table->date('admission_date')->nullable();
            $table->enum('status', ['active', 'inactive', 'passed_out', 'left', 'withdrawn'])->default('active')->index();

            // Medical
            $table->text('allergies')->nullable();
            $table->text('medical_conditions')->nullable();
            $table->text('disability_info')->nullable();
            $table->text('medical_notes')->nullable();

            // Transport (simple fields until the Transport module lands)
            $table->string('transport_route')->nullable();
            $table->string('transport_stop')->nullable();
            $table->string('pickup_point')->nullable();

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

            $table->unique(['branch_id', 'admission_number']);
            $table->index(['class_id', 'section_id']);
            $table->index('name');
        });
    }

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