<?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_programmes table for mentorship programme management.
     * Includes fee structure, certificate configuration, and category relationships.
     */
    public function up(): void
    {
        Schema::create('mentorship_programmes', function (Blueprint $table) {
            $table->id();
            
            // Programme identification
            $table->string('title', 191);
            $table->string('slug', 191)->unique();
            $table->text('description')->nullable();
            $table->text('objectives')->nullable()->comment('Learning objectives');
            $table->text('requirements')->nullable()->comment('Programme requirements');
            $table->text('target_audience')->nullable();
            
            // Category relationship
            $table->foreignId('category_id')
                ->nullable()
                ->constrained('mentorship_categories')
                ->nullOnDelete();
            
            // Fee structure
            $table->boolean('is_free')->default(false);
            $table->decimal('fee_amount', 12, 2)->default(0.00);
            $table->string('currency', 3)->default('UGX');
            $table->text('fee_description')->nullable()->comment('What the fee covers');
            
            // Certificate configuration
            $table->boolean('certificate_enabled')->default(true);
            $table->enum('certificate_orientation', ['portrait', 'landscape'])->default('landscape');
            $table->string('certificate_template', 191)->nullable();
            
            // Duration and scheduling
            $table->integer('duration_weeks')->nullable();
            $table->integer('sessions_count')->default(0)->comment('Expected number of sessions');
            $table->integer('max_mentees')->default(0)->comment('Maximum mentees per mentor, 0 = unlimited');
            $table->integer('max_mentors')->default(0)->comment('Maximum mentors, 0 = unlimited');
            
            // Programme type and format
            $table->enum('programme_type', ['one_on_one', 'group', 'hybrid'])->default('one_on_one');
            $table->enum('session_type', ['online', 'in_person', 'both'])->default('both');
            $table->string('venue', 500)->nullable()->comment('Default venue for in-person sessions');
            
            // Status and visibility
            $table->boolean('is_active')->default(true)->index();
            $table->boolean('is_featured')->default(false);
            $table->timestamp('registration_start')->nullable();
            $table->timestamp('registration_end')->nullable();
            $table->timestamp('programme_start')->nullable();
            $table->timestamp('programme_end')->nullable();
            
            // Display and media
            $table->string('featured_image', 500)->nullable();
            $table->json('gallery_images')->nullable();
            $table->string('banner_image', 500)->nullable();
            
            // SEO
            $table->string('meta_title', 191)->nullable();
            $table->text('meta_description')->nullable();
            
            // Statistics (denormalized for performance)
            $table->integer('applications_count')->default(0);
            $table->integer('assignments_count')->default(0);
            $table->integer('completions_count')->default(0);
            
            // User tracking
            $table->foreignId('created_by')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();
            $table->foreignId('updated_by')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();
            
            // Timestamps and soft deletes
            $table->timestamps();
            $table->softDeletes();
            
            // Indexes
            $table->index(['is_active', 'is_featured']);
            $table->index(['category_id', 'is_active']);
            $table->index(['programme_start', 'programme_end']);
            $table->index('slug');
        });
    }

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