<?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::table('associations', function (Blueprint $table) {
            // Subdomain for Professional plan (e.g., first-baptist.guild360.com)
            $table->string('subdomain', 100)->nullable()->unique()->after('slug');

            // Custom domain for Enterprise plan (e.g., mychurch.com)
            $table->string('custom_domain', 255)->nullable()->unique()->after('subdomain');

            // Domain verification status
            $table->timestamp('domain_verified_at')->nullable()->after('custom_domain');
            $table->string('domain_verification_token', 64)->nullable()->after('domain_verified_at');

            // Domain configuration
            $table->enum('domain_type', ['path', 'subdomain', 'custom'])->default('path')->after('domain_verification_token');
            $table->boolean('force_ssl')->default(true)->after('domain_type');

            // Indexes for faster domain lookups
            $table->index('subdomain');
            $table->index('custom_domain');
            $table->index('domain_type');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('associations', function (Blueprint $table) {
            $table->dropIndex(['subdomain']);
            $table->dropIndex(['custom_domain']);
            $table->dropIndex(['domain_type']);

            $table->dropColumn([
                'subdomain',
                'custom_domain',
                'domain_verified_at',
                'domain_verification_token',
                'domain_type',
                'force_ssl',
            ]);
        });
    }
};
