<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('members', function (Blueprint $table) {
            $table->id();
            $table->string('member_number', 20)->nullable()->unique();
            $table->foreignId('branch_id')->constrained()->restrictOnDelete();
            $table->foreignId('household_id')->nullable()->constrained()->nullOnDelete();
            $table->string('household_role', 20)->nullable();
            $table->string('title', 20)->nullable();
            $table->string('first_name', 100);
            $table->string('middle_name', 100)->nullable();
            $table->string('last_name', 100);
            $table->string('preferred_name', 100)->nullable();
            $table->string('gender', 10);
            $table->date('date_of_birth')->nullable();
            $table->string('marital_status', 20)->nullable();
            $table->string('email', 150)->nullable();
            $table->string('phone_primary', 30)->nullable();
            $table->string('phone_secondary', 30)->nullable();
            $table->string('address_line1', 150)->nullable();
            $table->string('address_line2', 150)->nullable();
            $table->string('city', 100)->nullable();
            $table->string('state_province', 100)->nullable();
            $table->string('postal_code', 20)->nullable();
            $table->string('country', 2)->nullable();
            $table->string('photo_path', 255)->nullable();
            $table->string('membership_status', 20)->default('member');
            $table->date('date_joined')->nullable();
            $table->date('date_baptized')->nullable();
            $table->string('baptism_type', 30)->nullable();
            $table->string('occupation', 150)->nullable();
            $table->string('employer', 150)->nullable();
            $table->string('emergency_contact_name', 150)->nullable();
            $table->string('emergency_contact_phone', 30)->nullable();
            $table->string('emergency_contact_relationship', 50)->nullable();
            $table->json('custom_fields')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->softDeletes();
            $table->timestamps();

            $table->index(['branch_id', 'last_name', 'first_name', 'id'], 'members_branch_list_index');
            $table->index(['branch_id', 'membership_status']);
            $table->index('membership_status');
            $table->index('phone_primary');
            $table->index('email');
            $table->index('date_of_birth');
            $table->index('city');
            $table->fullText(['first_name', 'middle_name', 'last_name', 'preferred_name']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('members');
    }
};
