<?php

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

/**
 * PRC-1143 — konsolide şema: `quotes` tablosunun TAM tanımı (tek dosya).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('quotes', function (Blueprint $t) {
            $t->id();
            $t->string('quote_number', 255);
            $t->string('subject', 255);
            $t->unsignedBigInteger('client_id');
            $t->unsignedBigInteger('admin_id')->nullable();
            $t->enum('status', ['draft','sent','accepted','rejected','expired','converted'])->default('draft');
            $t->unsignedBigInteger('currency_id')->nullable();
            $t->decimal('sub_total', 10, 2)->default(0.00);
            $t->decimal('tax_amount', 10, 2)->default(0.00);
            $t->decimal('total', 10, 2)->default(0.00);
            $t->date('valid_until');
            $t->text('proposal')->nullable();
            $t->text('admin_notes')->nullable();
            $t->text('client_notes')->nullable();
            $t->unsignedBigInteger('converted_invoice_id')->nullable();
            $t->timestamp('sent_at')->nullable();
            $t->timestamp('accepted_at')->nullable();
            $t->timestamp('rejected_at')->nullable();
            $t->timestamp('created_at')->nullable();
            $t->timestamp('updated_at')->nullable();
            $t->index('admin_id', 'quotes_admin_id_foreign');
            $t->index('client_id', 'quotes_client_id_foreign');
            $t->index('converted_invoice_id', 'quotes_converted_invoice_id_foreign');
            $t->index('currency_id', 'quotes_currency_id_foreign');
            $t->unique('quote_number', 'quotes_quote_number_unique');
            $t->index('status', 'quotes_status_index');
            $t->index('valid_until', 'quotes_valid_until_index');
            $t->foreign('admin_id', 'quotes_admin_id_foreign')->references('id')->on('admins')->onDelete('set null');
            $t->foreign('client_id', 'quotes_client_id_foreign')->references('id')->on('clients')->onDelete('cascade');
            $t->foreign('converted_invoice_id', 'quotes_converted_invoice_id_foreign')->references('id')->on('invoices')->onDelete('set null');
            $t->foreign('currency_id', 'quotes_currency_id_foreign')->references('id')->on('currencies')->onDelete('set null');
        });
    }

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