<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * Every hostname a company answers to — its handed-out subdomain and,
     * optionally, a domain it already owns. The tenant-resolver middleware
     * looks a request's Host header up in this table.
     */
    public function up(): void
    {
        Schema::create('domains', function (Blueprint $table) {
            $table->id();
            $table->foreignId('company_id')->constrained()->cascadeOnDelete();
            $table->string('hostname')->unique();
            $table->enum('type', ['subdomain', 'custom'])->default('subdomain');
            // Present on custom domains only, until DNS verification succeeds.
            $table->string('verification_token')->nullable();
            $table->timestamp('verified_at')->nullable();
            $table->enum('ssl_status', ['not_applicable', 'pending', 'issued', 'failed'])->default('not_applicable');
            $table->boolean('is_primary')->default(false);
            $table->timestamps();

            $table->index(['company_id', 'type']);
        });
    }

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