<?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('participants', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('academy_id')->constrained('academies');
            $table->foreignId('person_id')->constrained('people');
            $table->string('participant_number', 20);
            $table->date('registration_date');
            $table->string('registration_source', 20)->default('walk_in');
            $table->foreignId('referred_by_id')->nullable()->constrained('participants');
            $table->foreignId('primary_guardian_id')->nullable()->constrained('guardians');
            $table->foreignId('primary_activity_id')->nullable()->constrained('activities');
            $table->string('skill_level', 20)->nullable();
            $table->boolean('medical_clearance')->default(false);
            $table->date('medical_clearance_expires')->nullable();
            $table->string('photo_path', 500)->nullable();
            $table->integer('jersey_number')->nullable();
            $table->string('jersey_size', 5)->nullable();
            $table->string('shoe_size', 5)->nullable();
            $table->string('dominant_hand', 5)->nullable();
            $table->string('dominant_foot', 5)->nullable();
            $table->decimal('height_cm', 5, 1)->nullable();
            $table->decimal('weight_kg', 5, 1)->nullable();
            $table->string('status', 20)->default('registered');
            $table->timestamp('status_changed_at')->nullable();
            $table->text('status_reason')->nullable();
            $table->string('membership_type', 20)->nullable();
            $table->date('membership_expires_at')->nullable();
            $table->bigInteger('total_paid')->default(0);
            $table->bigInteger('outstanding_balance')->default(0);
            $table->jsonb('metadata')->default('{}');
            $table->text('notes')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users');
            $table->foreignId('updated_by')->nullable()->constrained('users');
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['academy_id', 'person_id']);
            $table->unique(['academy_id', 'participant_number']);
            $table->index(['academy_id', 'status']);
            $table->index(['academy_id', 'primary_activity_id']);
            $table->index('primary_guardian_id');
        });

        DB::statement("ALTER TABLE participants ADD CONSTRAINT participants_status_check CHECK (status IN ('registered', 'active', 'frozen', 'suspended', 'inactive', 'graduated', 'transferred', 'withdrawn', 'blacklisted'))");
        DB::statement("ALTER TABLE participants ADD CONSTRAINT participants_registration_source_check CHECK (registration_source IN ('walk_in', 'online', 'referral', 'social_media', 'event', 'transfer', 'other'))");
        DB::statement("ALTER TABLE participants ADD CONSTRAINT participants_skill_level_check CHECK (skill_level IS NULL OR skill_level IN ('beginner', 'intermediate', 'advanced', 'professional'))");
        DB::statement("ALTER TABLE participants ADD CONSTRAINT participants_dominant_hand_check CHECK (dominant_hand IS NULL OR dominant_hand IN ('left', 'right', 'both'))");
        DB::statement("ALTER TABLE participants ADD CONSTRAINT participants_dominant_foot_check CHECK (dominant_foot IS NULL OR dominant_foot IN ('left', 'right', 'both'))");
        DB::statement("ALTER TABLE participants ADD CONSTRAINT participants_membership_type_check CHECK (membership_type IS NULL OR membership_type IN ('basic', 'silver', 'gold', 'platinum', 'custom'))");
    }

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