<?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_application_references', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->foreignUuid('membership_application_id')
                ->constrained('membership_applications')
                ->cascadeOnDelete();

            $table->string('reference_type')->comment('PROPOSER, SUPPORTER');

            // Applicant may select at submit; admin may fill in later
            $table->foreignUuid('user_id')->nullable()->constrained('users')->nullOnDelete();

            // Set only when admin assigns or updates this reference
            $table->foreignUuid('assigned_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('assigned_at')->nullable();

            $table->timestamps();

            $table->unique(['membership_application_id', 'reference_type']);
        });
    }

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