<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('people', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('academy_id')->constrained('academies');
            $table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->string('name');
            $table->string('name_ar');
            $table->string('email')->nullable();
            $table->string('phone', 20)->nullable();
            $table->string('phone_secondary', 20)->nullable();
            $table->string('national_id', 14)->nullable();
            $table->date('date_of_birth')->nullable();
            $table->string('gender', 10)->nullable();
            $table->string('nationality', 50)->nullable()->default('Egyptian');
            $table->text('address')->nullable();
            $table->string('city')->nullable();
            $table->string('governorate')->nullable();
            $table->string('photo_path')->nullable();
            $table->string('blood_type', 5)->nullable();
            $table->text('medical_notes')->nullable();
            $table->string('emergency_contact_name')->nullable();
            $table->string('emergency_contact_phone', 20)->nullable();
            $table->string('emergency_contact_relation', 30)->nullable();
            $table->string('classification', 30)->nullable();
            $table->string('source', 50)->nullable();
            $table->text('notes')->nullable();
            $table->jsonb('metadata')->default('{}');
            $table->foreignId('created_by')->constrained('users');
            $table->timestamps();
            $table->softDeletes();

            $table->index(['academy_id', 'national_id']);
            $table->index(['academy_id', 'phone']);
        });

        DB::statement("ALTER TABLE people ADD CONSTRAINT people_gender_check CHECK (gender IN ('male', 'female'))");
        DB::statement("ALTER TABLE people ADD CONSTRAINT people_classification_check CHECK (classification IN ('regular', 'vip', 'scholarship', 'staff_child', 'trial'))");
    }

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