<?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('training_programs', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('academy_id')->constrained('academies');
            $table->foreignId('activity_id')->constrained('activities');
            $table->foreignId('branch_id')->nullable()->constrained('branches');
            $table->string('name');
            $table->string('name_ar');
            $table->string('slug', 100);
            $table->text('description')->nullable();
            $table->text('description_ar')->nullable();
            $table->jsonb('objectives')->default('[]');
            $table->string('skill_level', 20)->default('all');
            $table->integer('age_min')->nullable();
            $table->integer('age_max')->nullable();
            $table->string('gender', 10)->default('all');
            $table->integer('sessions_per_week')->default(2);
            $table->integer('session_duration_minutes')->default(60);
            $table->integer('total_sessions')->nullable();
            $table->integer('program_duration_weeks')->nullable();
            $table->integer('min_participants')->default(1);
            $table->integer('max_participants')->default(20);
            $table->boolean('allow_waitlist')->default(true);
            $table->integer('attendance_requirement_percent')->nullable();
            $table->boolean('assessment_required')->default(false);
            $table->jsonb('prerequisites')->default('[]');
            $table->string('facility_type', 30)->nullable();
            $table->jsonb('equipment_required')->default('[]');
            $table->boolean('registration_open')->default(true);
            $table->date('registration_deadline')->nullable();
            $table->date('program_start_date')->nullable();
            $table->date('program_end_date')->nullable();
            $table->string('renewal_policy', 20)->default('manual_renew');
            $table->text('cancellation_policy')->nullable();
            $table->text('refund_policy')->nullable();
            $table->string('status', 20)->default('draft');
            $table->boolean('featured')->default(false);
            $table->integer('sort_order')->default(0);
            $table->jsonb('metadata')->default('{}');
            $table->foreignId('created_by')->constrained('users');
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['academy_id', 'slug']);
            $table->index(['academy_id', 'status']);
            $table->index(['academy_id', 'activity_id']);
        });

        DB::statement("ALTER TABLE training_programs ADD CONSTRAINT training_programs_status_check CHECK (status IN ('draft', 'active', 'full', 'closed', 'archived'))");
        DB::statement("ALTER TABLE training_programs ADD CONSTRAINT training_programs_skill_level_check CHECK (skill_level IN ('all', 'beginner', 'intermediate', 'advanced', 'professional'))");
        DB::statement("ALTER TABLE training_programs ADD CONSTRAINT training_programs_gender_check CHECK (gender IN ('all', 'male', 'female'))");
        DB::statement("ALTER TABLE training_programs ADD CONSTRAINT training_programs_renewal_policy_check CHECK (renewal_policy IN ('auto_renew', 'manual_renew', 'one_time'))");
    }

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