<?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('trainers', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('academy_id')->constrained('academies');
            $table->foreignId('employee_id')->unique()->constrained('employees');
            $table->string('trainer_number', 20);
            $table->text('bio')->nullable();
            $table->text('bio_ar')->nullable();
            $table->jsonb('specializations')->default('[]');
            $table->jsonb('sports')->default('[]');
            $table->integer('max_daily_sessions')->nullable();
            $table->decimal('max_weekly_hours', 4, 1)->nullable();
            $table->string('compensation_model', 20);
            $table->bigInteger('hourly_rate')->nullable();
            $table->bigInteger('session_rate')->nullable();
            $table->bigInteger('group_rate')->nullable();
            $table->bigInteger('player_rate')->nullable();
            $table->decimal('revenue_share_percent', 5, 2)->nullable();
            $table->decimal('rating', 3, 2)->nullable();
            $table->integer('total_sessions_conducted')->default(0);
            $table->string('status', 20)->default('active');
            $table->timestamps();

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

        DB::statement("ALTER TABLE trainers ADD CONSTRAINT trainers_compensation_model_check CHECK (compensation_model IN ('salary', 'hourly', 'per_session', 'per_group', 'per_player', 'revenue_share', 'contract', 'hybrid'))");
        DB::statement("ALTER TABLE trainers ADD CONSTRAINT trainers_status_check CHECK (status IN ('active', 'inactive', 'on_leave', 'suspended'))");
    }

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