<?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('quotations', function (Blueprint $table): void {
            $table->id();
            $table->ulid('public_id')->unique();
            $table->foreignId('company_id')->constrained()->cascadeOnDelete();
            $table->foreignId('branch_id')->constrained()->restrictOnDelete();
            $table->foreignId('party_id')->constrained()->restrictOnDelete();
            $table->foreignId('contract_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('converted_job_id')->nullable()->constrained('transport_jobs')->nullOnDelete();
            $table->string('quotation_number')->unique();
            $table->date('quotation_date');
            $table->date('expiry_date')->nullable();
            $table->char('currency_code', 3)->default('AED');
            $table->decimal('exchange_rate', 20, 8)->default(1);
            $table->string('pricing_mode', 20)->default('exclusive');
            $table->decimal('subtotal', 20, 4)->default(0);
            $table->decimal('discount_total', 20, 4)->default(0);
            $table->decimal('taxable_total', 20, 4)->default(0);
            $table->decimal('vat_total', 20, 4)->default(0);
            $table->decimal('grand_total', 20, 4)->default(0);
            $table->string('payment_terms')->nullable();
            $table->longText('terms')->nullable();
            $table->text('notes')->nullable();
            $table->unsignedInteger('version')->default(1);
            $table->string('status', 30)->default('draft')->index();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('approved_at')->nullable();
            $table->timestamp('sent_at')->nullable();
            $table->timestamp('accepted_at')->nullable();
            $table->timestamps();
            $table->index(['party_id', 'quotation_date', 'status']);
        });

        Schema::create('quotation_items', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('quotation_id')->constrained()->cascadeOnDelete();
            $table->unsignedInteger('line_number');
            $table->foreignId('service_type_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('route_id')->nullable()->constrained('transport_routes')->nullOnDelete();
            $table->string('vehicle_type')->nullable();
            $table->text('description');
            $table->decimal('quantity', 20, 4)->default(1);
            $table->string('unit', 30)->nullable();
            $table->decimal('unit_rate', 20, 4);
            $table->decimal('discount_amount', 20, 4)->default(0);
            $table->decimal('taxable_amount', 20, 4);
            $table->decimal('vat_rate', 9, 4)->default(0);
            $table->decimal('vat_amount', 20, 4)->default(0);
            $table->decimal('line_total', 20, 4);
            $table->foreignId('tax_code_id')->nullable()->constrained()->restrictOnDelete();
            $table->timestamps();
            $table->unique(['quotation_id', 'line_number']);
        });
    }

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