<?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 mentorship_sessions table for session scheduling and management.
     * Supports both online (Google Meet) and in-person sessions.
     */
    public function up(): void
    {
        Schema::create('mentorship_sessions', function (Blueprint $table) {
            $table->id();
            
            // Assignment relationship
            $table->foreignId('assignment_id')
                ->constrained('mentorship_assignments')
                ->cascadeOnDelete();
            
            // Programme reference (for easier queries)
            $table->foreignId('programme_id')
                ->constrained('mentorship_programmes')
                ->cascadeOnDelete();
            
            // Session details
            $table->string('title', 191);
            $table->text('description')->nullable();
            $table->text('agenda')->nullable();
            $table->text('notes')->nullable();
            
            // Session type
            $table->enum('type', ['online', 'in_person'])->default('online');
            $table->enum('session_number', ['first', 'regular', 'last'])->default('regular');
            
            // Scheduling
            $table->dateTime('start_time');
            $table->dateTime('end_time');
            $table->string('timezone', 50)->default('Africa/Kampala');
            $table->integer('duration_minutes')->default(60);
            
            // Location (for in-person sessions)
            $table->string('venue', 500)->nullable();
            $table->text('venue_address')->nullable();
            $table->string('venue_instructions')->nullable();
            
            // Online meeting details (Google Meet integration)
            $table->string('meeting_link', 500)->nullable();
            $table->string('meeting_id', 100)->nullable();
            $table->string('meeting_password', 100)->nullable();
            $table->string('calendar_event_id', 191)->nullable();
            $table->json('meeting_details')->nullable();
            
            // Status tracking
            $table->enum('status', [
                'scheduled',
                'confirmed',
                'in_progress',
                'completed',
                'cancelled',
                'rescheduled',
                'no_show'
            ])->default('scheduled')->index();
            
            // Attendance tracking
            $table->boolean('mentor_attended')->default(false);
            $table->timestamp('mentor_joined_at')->nullable();
            $table->boolean('mentee_attended')->default(false);
            $table->timestamp('mentee_joined_at')->nullable();
            
            // Session outcomes
            $table->text('summary')->nullable()->comment('Session summary notes');
            $table->text('action_items')->nullable()->comment('Action items for next session');
            $table->text('resources_shared')->nullable()->comment('Resources shared during session');
            $table->json('attachments')->nullable();
            
            // Feedback
            $table->integer('mentor_rating')->nullable()->comment('Session rating by mentor (1-5)');
            $table->text('mentor_feedback')->nullable();
            $table->integer('mentee_rating')->nullable()->comment('Session rating by mentee (1-5)');
            $table->text('mentee_feedback')->nullable();
            
            // Rescheduling information
            $table->foreignId('rescheduled_from')
                ->nullable()
                ->constrained('mentorship_sessions')
                ->nullOnDelete();
            $table->text('reschedule_reason')->nullable();
            $table->timestamp('rescheduled_at')->nullable();
            
            // Cancellation information
            $table->foreignId('cancelled_by')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();
            $table->timestamp('cancelled_at')->nullable();
            $table->text('cancellation_reason')->nullable();
            
            // Reminder tracking
            $table->boolean('reminder_sent_24h')->default(false);
            $table->boolean('reminder_sent_1h')->default(false);
            $table->timestamp('last_reminder_at')->nullable();
            
            // Creation tracking
            $table->foreignId('created_by')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();
            
            // Timestamps and soft deletes
            $table->timestamps();
            $table->softDeletes();
            
            // Indexes
            $table->index(['assignment_id', 'status']);
            $table->index(['programme_id', 'start_time']);
            $table->index(['start_time', 'status']);
            $table->index('calendar_event_id');
        });
    }

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