<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('membership_applications', function (Blueprint $table) {
            $table->id();
            $table->string('reference_number', 50)->unique();
            $table->foreignId('membership_category_id')->constrained()->onDelete('cascade');
            $table->foreignId('user_id')->nullable()->constrained()->onDelete('set null');
            
            // Applicant Data (JSON for flexibility)
            $table->json('applicant_data');
            
            // Contact Information
            $table->string('first_name', 191);
            $table->string('last_name', 191);
            $table->string('email', 191);
            $table->string('phone', 50)->nullable();
            $table->text('address')->nullable();
            $table->string('city', 100)->nullable();
            $table->string('country', 100)->nullable();
            
            // Professional Information
            $table->string('occupation', 191)->nullable();
            $table->string('organization', 191)->nullable();
            $table->text('professional_background')->nullable();
            
            // Application Status
            $table->enum('status', ['pending', 'under_review', 'approved', 'rejected', 'withdrawn'])->default('pending');
            $table->foreignId('reviewed_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamp('reviewed_at')->nullable();
            $table->text('review_notes')->nullable();
            $table->text('rejection_reason')->nullable();
            
            // Payment Information
            $table->boolean('payment_required')->default(true);
            $table->boolean('payment_completed')->default(false);
            $table->string('payment_reference')->nullable();
            $table->timestamp('payment_date')->nullable();
            
            // Document uploads
            $table->json('documents')->nullable();
            
            $table->timestamps();
            $table->softDeletes();
            
            $table->index('reference_number');
            $table->index('status');
            $table->index('email');
            $table->index('membership_category_id');
            $table->index('created_at');
        });
    }

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