<?php

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

/**
 * PRC-1143 — konsolide şema: `invoices` tablosunun TAM tanımı (tek dosya).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('invoices', function (Blueprint $t) {
            $t->id();
            $t->unsignedBigInteger('brand_id')->nullable();
            $t->unsignedBigInteger('client_id');
            $t->unsignedBigInteger('order_id')->nullable();
            $t->unsignedBigInteger('billing_address_id')->nullable();
            $t->enum('status', ['pending','unpaid','paid','overdue','cancelled','refunded','fraud','proforma'])->default('pending');
            $t->string('invoice_number', 255);
            $t->string('paid_invoice_number', 255)->nullable();
            $t->decimal('subtotal', 10, 2)->default(0.00);
            $t->decimal('setup_fee', 10, 2)->default(0.00);
            $t->decimal('tax_amount', 10, 2)->default(0.00);
            $t->decimal('discount_amount', 10, 2)->default(0.00);
            $t->decimal('gateway_fee', 10, 2)->default(0.00);
            $t->unsignedTinyInteger('installment_count')->default(1);
            $t->decimal('installment_surcharge', 15, 2)->default(0.00);
            $t->string('installment_payment_id', 32)->nullable();
            $t->decimal('total_amount', 10, 2)->default(0.00);
            $t->decimal('credit_applied', 12, 2)->default(0.00);
            $t->decimal('tax_rate', 5, 2)->default(0.00);
            $t->string('tax_exemption_reason', 255)->nullable();
            $t->boolean('accounting_excluded')->default(0);
            $t->unsignedBigInteger('currency_id');
            $t->decimal('exchange_rate', 20, 8)->nullable();
            $t->string('payment_method', 255)->nullable();
            $t->boolean('auto_pay_enabled')->default(0);
            $t->unsignedTinyInteger('auto_pay_attempts')->default(0);
            $t->timestamp('last_auto_pay_attempt_at')->nullable();
            $t->json('files')->nullable();
            $t->string('legal_invoice_pdf_path', 255)->nullable();
            $t->string('legal_invoice_external_id', 100)->nullable();
            $t->string('legal_invoice_number', 100)->nullable();
            $t->timestamp('legal_invoice_issued_at')->nullable();
            $t->string('reference_number', 255)->nullable();
            $t->text('notes')->nullable();
            $t->string('domain_lifecycle_type', 20)->nullable();
            $t->unsignedBigInteger('domain_id')->nullable();
            $t->timestamp('due_date')->nullable();
            $t->timestamp('issued_at')->nullable();
            $t->timestamp('paid_date')->nullable();
            $t->timestamp('refund_date')->nullable();
            $t->string('refund_external_id', 100)->nullable();
            $t->timestamp('cancel_date')->nullable();
            $t->decimal('grace_period_fee', 10, 2)->default(0.00);
            $t->decimal('redemption_fee', 10, 2)->default(0.00);
            $t->decimal('processing_fee', 10, 2)->default(0.00);
            $t->decimal('late_fee', 10, 2)->default(0.00);
            $t->timestamp('last_late_fee_applied_at')->nullable();
            $t->json('billing_info_snapshot')->nullable();
            $t->json('client_info_snapshot')->nullable();
            $t->json('custom_field_values')->nullable();
            $t->timestamp('created_at')->nullable();
            $t->timestamp('updated_at')->nullable();
            $t->index('brand_id', 'invoices_brand_id_index');
            $t->index('client_id', 'invoices_client_id_index');
            $t->index('currency_id', 'invoices_currency_id_index');
            $t->index(['domain_lifecycle_type', 'domain_id'], 'invoices_domain_lifecycle_type_domain_id_index');
            $t->index('due_date', 'invoices_due_date_index');
            $t->unique('invoice_number', 'invoices_invoice_number_unique');
            $t->index('order_id', 'invoices_order_id_index');
            $t->index('status', 'invoices_status_index');
        });
    }

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