<?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_assignments table for mentor-mentee pairing management.
     * Tracks the complete lifecycle of mentorship relationships.
     */
    public function up(): void
    {
        Schema::create('mentorship_assignments', function (Blueprint $table) {
            $table->id();
            
            // Programme relationship
            $table->foreignId('programme_id')
                ->constrained('mentorship_programmes')
                ->cascadeOnDelete();
            
            // Mentor relationship
            $table->foreignId('mentor_id')
                ->constrained('users')
                ->cascadeOnDelete()
                ->comment('The mentor in this assignment');
            
            // Mentee relationship
            $table->foreignId('mentee_id')
                ->constrained('users')
                ->cascadeOnDelete()
                ->comment('The mentee in this assignment');
            
            // Application references (for tracking source)
            $table->foreignId('mentor_application_id')
                ->nullable()
                ->constrained('mentorship_applications')
                ->nullOnDelete();
            $table->foreignId('mentee_application_id')
                ->nullable()
                ->constrained('mentorship_applications')
                ->nullOnDelete();
            
            // Assignment details
            $table->text('matching_criteria')->nullable()->comment('Why this match was made');
            $table->text('goals')->nullable()->comment('Mentorship goals for this assignment');
            $table->text('notes')->nullable();
            
            // Scheduling
            $table->date('start_date')->nullable();
            $table->date('end_date')->nullable();
            $table->string('preferred_meeting_time', 100)->nullable();
            $table->string('meeting_frequency', 50)->nullable()->default('weekly');
            
            // Status tracking
            $table->enum('status', [
                'pending_acceptance',
                'active',
                'on_hold',
                'completed',
                'cancelled',
                'reassigned'
            ])->default('pending_acceptance')->index();
            
            // Progress tracking
            $table->integer('progress_percentage')->default(0);
            $table->integer('sessions_completed')->default(0);
            $table->integer('sessions_total')->default(0);
            
            // Milestone tracking
            $table->json('milestones')->nullable()->comment('Array of milestones with completion status');
            $table->timestamp('last_session_at')->nullable();
            $table->timestamp('next_session_at')->nullable();
            
            // Completion information
            $table->timestamp('completed_at')->nullable();
            $table->text('completion_notes')->nullable();
            $table->boolean('certificate_eligible')->default(false);
            $table->boolean('certificate_issued')->default(false);
            
            // Feedback and ratings
            $table->integer('mentor_rating')->nullable()->comment('Rating given by mentee (1-5)');
            $table->text('mentor_feedback')->nullable();
            $table->integer('mentee_rating')->nullable()->comment('Rating given by mentor (1-5)');
            $table->text('mentee_feedback')->nullable();
            
            // Assignment metadata
            $table->foreignId('assigned_by')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();
            $table->timestamp('assigned_at')->nullable();
            
            // Acceptance tracking
            $table->timestamp('mentor_accepted_at')->nullable();
            $table->timestamp('mentee_accepted_at')->nullable();
            
            // Reassignment tracking
            $table->foreignId('previous_assignment_id')
                ->nullable()
                ->constrained('mentorship_assignments')
                ->nullOnDelete()
                ->comment('Previous assignment if this is a reassignment');
            $table->text('reassignment_reason')->nullable();
            
            // Timestamps and soft deletes
            $table->timestamps();
            $table->softDeletes();
            
            // Indexes
            $table->index(['programme_id', 'status']);
            $table->index(['mentor_id', 'status']);
            $table->index(['mentee_id', 'status']);
            $table->index(['status', 'start_date']);
            $table->unique(['programme_id', 'mentor_id', 'mentee_id'], 'unique_mentor_mentee_programme');
        });
    }

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