<?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_applications table for public registration applications.
     * Handles both mentor and mentee applications with payment integration.
     */
    public function up(): void
    {
        Schema::create('mentorship_applications', function (Blueprint $table) {
            $table->id();
            
            // Programme relationship
            $table->foreignId('programme_id')
                ->constrained('mentorship_programmes')
                ->cascadeOnDelete();
            $table->foreignId('category_id')
                ->nullable()
                ->constrained('mentorship_categories')
                ->nullOnDelete();
            
            // Applicant information
            $table->string('name', 191);
            $table->string('email', 191)->index();
            $table->string('phone', 50)->nullable();
            $table->string('country', 100)->nullable();
            $table->string('city', 100)->nullable();
            $table->text('address')->nullable();
            
            // Professional information
            $table->string('occupation', 191)->nullable();
            $table->string('organization', 191)->nullable();
            $table->string('education_level', 100)->nullable();
            $table->text('bio')->nullable();
            
            // Role preference
            $table->enum('role_preference', ['mentor', 'mentee'])->default('mentee');
            
            // Area of interest (for matching)
            $table->text('areas_of_interest')->nullable()->comment('JSON array of interest areas');
            $table->text('skills')->nullable()->comment('JSON array of skills');
            $table->text('experience')->nullable()->comment('Relevant experience description');
            $table->text('motivation')->nullable()->comment('Motivation for joining');
            $table->text('expectations')->nullable()->comment('What they expect from the programme');
            
            // Mentor-specific fields
            $table->text('expertise_areas')->nullable();
            $table->integer('years_of_experience')->nullable();
            $table->text('mentoring_experience')->nullable();
            $table->text('availability')->nullable()->comment('Availability schedule');
            
            // Mentee-specific fields
            $table->text('learning_goals')->nullable();
            $table->text('preferred_mentor_qualities')->nullable();
            $table->text('time_commitment')->nullable();
            
            // Application status
            $table->enum('status', [
                'pending', 
                'under_review', 
                'approved', 
                'rejected', 
                'waitlisted',
                'withdrawn',
                'payment_pending'
            ])->default('pending')->index();
            
            // Payment information
            $table->boolean('payment_required')->default(false);
            $table->decimal('payment_amount', 12, 2)->default(0.00);
            $table->string('payment_currency', 3)->default('UGX');
            $table->enum('payment_status', ['pending', 'processing', 'paid', 'failed', 'refunded'])
                ->default('pending');
            $table->string('payment_reference', 191)->nullable();
            $table->string('payment_phone', 50)->nullable();
            $table->string('payment_method', 50)->nullable();
            $table->timestamp('payment_initiated_at')->nullable();
            $table->timestamp('payment_completed_at')->nullable();
            $table->json('payment_details')->nullable();
            
            // User account (created upon approval)
            $table->foreignId('user_id')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete()
                ->comment('User account created after approval');
            
            // Review and approval
            $table->foreignId('reviewed_by')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();
            $table->timestamp('reviewed_at')->nullable();
            $table->text('review_notes')->nullable();
            $table->text('rejection_reason')->nullable();
            
            // Additional information
            $table->text('additional_notes')->nullable();
            $table->json('custom_fields')->nullable()->comment('Dynamic form fields');
            
            // Documents
            $table->json('documents')->nullable()->comment('Uploaded documents (CV, certificates, etc.)');
            $table->string('profile_image', 500)->nullable();
            
            // Source tracking
            $table->string('source', 100)->nullable()->comment('Where they heard about the programme');
            $table->string('utm_source', 100)->nullable();
            $table->string('utm_campaign', 100)->nullable();
            
            // Consent
            $table->boolean('terms_accepted')->default(false);
            $table->boolean('privacy_accepted')->default(false);
            $table->timestamp('consent_at')->nullable();
            
            // Timestamps and soft deletes
            $table->timestamps();
            $table->softDeletes();
            
            // Indexes
            $table->index(['programme_id', 'status']);
            $table->index(['email', 'programme_id']);
            $table->index(['status', 'created_at']);
            $table->index('payment_reference');
            $table->index('user_id');
        });
    }

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