<?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('client_domains', function (Blueprint $table) {
            $table->id();
            $table->unsignedInteger('client_id');
            $table->unsignedInteger('tld_id');
            $table->string('domain');
            $table->enum('status', ['pending', 'active', 'expired', 'suspended', 'transferred'])->default('active');
            $table->boolean('pending_cancellation')->default(false);
            $table->timestamp('cancellation_date')->nullable();
            $table->string('lifecycle_status', 20)->default('active');
            $table->string('module')->nullable();
            $table->integer('period')->default(1);
            $table->date('create_date')->nullable();
            $table->date('expire_date')->nullable();
            $table->date('grace_start_date')->nullable();
            $table->date('redemption_start_date')->nullable();
            $table->date('due_date')->nullable();
            $table->date('next_due_date')->nullable();
            $table->date('invoice_date')->nullable();
            $table->date('last_check')->nullable();
            $table->boolean('dns_management')->nullable()->default(null);
            $table->boolean('email_forwarding')->nullable()->default(null);
            $table->boolean('id_protection')->nullable()->default(null);
            $table->boolean('epp_code')->nullable()->default(null);
            $table->boolean('is_premium')->default(false);
            $table->decimal('premium_register_price', 12, 2)->nullable();
            $table->decimal('premium_renew_price', 12, 2)->nullable();
            $table->decimal('premium_transfer_price', 12, 2)->nullable();
            $table->boolean('disable_reminders')->default(false);
            $table->boolean('auto_renew')->default(false);
            $table->enum('provisioning_status', ['pending', 'queued', 'processing', 'completed', 'failed', 'manual'])
                ->default('pending');
            $table->unsignedInteger('provisioning_attempt')->default(0);
            $table->text('provisioning_error')->nullable();
            $table->timestamp('provisioned_at')->nullable();
            $table->timestamp('last_provisioning_attempt')->nullable();
            $table->timestamp('last_sync_at')->nullable();
            $table->text('sync_error')->nullable();
            $table->json('cached_data')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index('client_id');
            $table->index('tld_id');
            $table->index('status');
            $table->index('expire_date');
            $table->index('lifecycle_status');
            $table->index(['lifecycle_status', 'expire_date']);
            $table->index('provisioning_status');
        });
    }

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