<?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('association_user', function (Blueprint $table) {
            $table->id();
            $table->foreignId('association_id')->constrained('associations')->cascadeOnDelete();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            
            // Membership Details
            $table->string('membership_number')->nullable();
            $table->enum('membership_tier', ['regular', 'premium', 'lifetime', 'honorary'])->default('regular');
            $table->enum('status', ['pending', 'active', 'suspended', 'exited'])->default('pending');
            
            // Dates
            $table->timestamp('joined_at')->nullable();
            $table->timestamp('approved_at')->nullable();
            $table->timestamp('exited_at')->nullable();
            $table->text('exit_reason')->nullable();
            
            // Emergency Contact
            $table->string('emergency_contact_name')->nullable();
            $table->string('emergency_contact_phone')->nullable();
            $table->string('emergency_contact_relationship')->nullable();
            
            // Referral Tracking
            $table->foreignId('referred_by')->nullable()->constrained('users');
            
            // Metadata
            $table->timestamps();
            
            // Constraints & Indexes
            $table->unique(['association_id', 'user_id']);
            $table->index(['association_id', 'status']);
            $table->index('membership_number');
        });
    }

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