<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * Creates the training_session_trainers pivot table to link
     * training sessions with trainers (users who conduct training).
     *
     * @return void
     */
    public function up(): void
    {
        Schema::create('training_session_trainers', function (Blueprint $table) {
            $table->id();
            
            // Foreign Keys
            $table->foreignId('training_session_id')
                ->constrained('training_sessions')
                ->cascadeOnDelete();
            $table->foreignId('trainer_id')
                ->constrained('users')
                ->cascadeOnDelete();
            
            // Trainer Role in Session
            $table->enum('role', ['lead', 'assistant', 'guest'])->default('lead');
            
            // Session Topic/Focus
            $table->string('topic')->nullable();
            $table->text('notes')->nullable();
            
            // Compensation (if applicable)
            $table->decimal('compensation_amount', 10, 2)->nullable();
            $table->string('compensation_currency', 3)->default('UGX');
            
            // Confirmation Status
            $table->enum('confirmation_status', ['pending', 'confirmed', 'declined'])
                ->default('pending');
            $table->timestamp('confirmed_at')->nullable();
            
            // Timestamps
            $table->timestamps();
            
            // Unique Constraint
            $table->unique(['training_session_id', 'trainer_id'], 'unique_session_trainer');
            
            // Indexes
            $table->index('trainer_id');
            $table->index('role');
            $table->index('confirmation_status');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down(): void
    {
        Schema::dropIfExists('training_session_trainers');
    }
};
