<?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_sessions table to store individual training session events
     * including date, time, venue, meeting links, and session type.
     *
     * @return void
     */
    public function up(): void
    {
        Schema::create('training_sessions', function (Blueprint $table) {
            $table->id();
            
            // Relationship to Training Package
            $table->foreignId('training_package_id')
                ->constrained('training_packages')
                ->cascadeOnDelete();
            
            // Session Details
            $table->string('session_name', 191);
            $table->text('description')->nullable();
            $table->integer('session_order')->default(1);
            
            // Schedule Information
            $table->date('session_date');
            $table->time('start_time');
            $table->time('end_time');
            
            // Session Type and Delivery
            $table->enum('session_type', ['in_person', 'online', 'hybrid'])->default('in_person');
            
            // Venue Information (for in-person and hybrid)
            $table->string('venue_name')->nullable();
            $table->text('venue_address')->nullable();
            $table->string('venue_city')->nullable();
            $table->string('venue_country')->default('Uganda');
            
            // Online Meeting Information
            $table->string('meeting_link', 500)->nullable();
            $table->string('meeting_id')->nullable();
            $table->string('meeting_password')->nullable();
            $table->string('meeting_provider')->nullable(); // google_meet, zoom, teams
            
            // Capacity Override
            $table->integer('max_participants')->nullable();
            
            // Session Status
            $table->enum('status', ['scheduled', 'ongoing', 'completed', 'cancelled', 'postponed'])
                ->default('scheduled');
            
            // Cancellation/Postponement Information
            $table->text('cancellation_reason')->nullable();
            $table->date('new_date')->nullable();
            
            // Materials and Resources
            $table->json('materials')->nullable();
            $table->string('recording_url', 500)->nullable();
            
            // Audit Fields
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            
            // Timestamps
            $table->timestamps();
            
            // Indexes
            $table->index('training_package_id');
            $table->index('session_date');
            $table->index('status');
            $table->index(['session_date', 'start_time']);
        });
    }

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