<?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('guardian_participant', function (Blueprint $table) {
            $table->id();
            $table->foreignId('guardian_id')->constrained('guardians')->cascadeOnDelete();
            $table->foreignId('participant_id')->constrained('participants')->cascadeOnDelete();
            $table->string('relationship_type', 30);
            $table->boolean('is_primary')->default(false);
            $table->boolean('is_emergency_contact')->default(false);
            $table->boolean('can_pickup')->default(true);
            $table->boolean('receives_notifications')->default(true);
            $table->boolean('can_authorize_payment')->default(false);
            $table->timestamp('created_at')->useCurrent();

            $table->unique(['guardian_id', 'participant_id']);
            $table->index('participant_id');
        });

        DB::statement("ALTER TABLE guardian_participant ADD CONSTRAINT guardian_participant_relationship_type_check CHECK (relationship_type IN ('father', 'mother', 'brother', 'sister', 'uncle', 'aunt', 'grandfather', 'grandmother', 'guardian', 'other'))");
    }

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