<?php

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

/**
 * PRC-1143 — konsolide şema: `quote_items` tablosunun TAM tanımı (tek dosya).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('quote_items', function (Blueprint $t) {
            $t->id();
            $t->unsignedBigInteger('quote_id');
            $t->enum('type', ['custom','product','domain'])->nullable()->default('custom');
            $t->unsignedBigInteger('product_id')->nullable();
            $t->string('billing_cycle', 255)->nullable();
            $t->string('domain_name', 255)->nullable();
            $t->unsignedBigInteger('tld_id')->nullable();
            $t->string('domain_action', 255)->nullable();
            $t->integer('registration_period')->nullable();
            $t->json('config_options')->nullable();
            $t->decimal('config_options_total', 12, 2)->default(0.00);
            $t->string('description', 255);
            $t->integer('quantity')->default(1);
            $t->decimal('unit_price', 10, 2);
            $t->decimal('discount', 5, 2)->default(0.00);
            $t->boolean('taxable')->default(1);
            $t->decimal('tax_rate', 5, 2)->default(0.00);
            $t->decimal('tax_amount', 10, 2)->default(0.00);
            $t->decimal('total', 10, 2);
            $t->integer('sort_order')->default(0);
            $t->timestamp('created_at')->nullable();
            $t->timestamp('updated_at')->nullable();
            $t->index('product_id', 'quote_items_product_id_foreign');
            $t->index('quote_id', 'quote_items_quote_id_foreign');
            $t->index('tld_id', 'quote_items_tld_id_foreign');
            $t->foreign('product_id', 'quote_items_product_id_foreign')->references('id')->on('products')->onDelete('set null');
            $t->foreign('quote_id', 'quote_items_quote_id_foreign')->references('id')->on('quotes')->onDelete('cascade');
            $t->foreign('tld_id', 'quote_items_tld_id_foreign')->references('id')->on('tlds')->onDelete('set null');
        });
    }

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