<?php

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

/**
 * Estimates / quotations (spec §5.5 / §6.6) — native vrc_ tables. When the
 * per-business `estimate_source` setting is 'erp', estimates are generated
 * through the ERP quotation flow instead (adapter; the native tables go
 * unused). A tokenized approval link lets a customer approve without login.
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('vrc_estimates')) {
            Schema::create('vrc_estimates', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->string('number', 60)->nullable();
                $t->unsignedBigInteger('repair_order_id')->nullable();
                $t->unsignedInteger('erp_customer_id');
                $t->unsignedBigInteger('vehicle_id')->nullable();
                $t->unsignedInteger('location_id')->nullable();
                $t->string('status', 20)->default('draft'); // draft|sent|approved|declined|expired|converted
                $t->date('valid_until')->nullable();
                $t->decimal('subtotal', 15, 2)->default(0);
                $t->decimal('discount_total', 15, 2)->default(0);
                $t->decimal('tax_total', 15, 2)->default(0);
                $t->decimal('grand_total', 15, 2)->default(0);
                $t->string('currency', 5)->nullable();
                $t->text('notes')->nullable();
                $t->text('terms')->nullable();
                $t->string('approval_token', 64)->nullable()->unique();
                $t->dateTime('sent_at')->nullable();
                $t->dateTime('decided_at')->nullable();
                $t->unsignedInteger('decided_by')->nullable();
                $t->string('decision_method', 20)->nullable(); // portal|email|phone|in_person
                $t->string('pdf_path', 255)->nullable();
                $t->unsignedInteger('created_by')->nullable();
                $t->timestamps();
                $t->index(['business_id', 'status']);
                $t->index(['business_id', 'erp_customer_id']);
                $t->index(['business_id', 'repair_order_id']);
            });
        }

        if (! Schema::hasTable('vrc_estimate_items')) {
            Schema::create('vrc_estimate_items', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('estimate_id');
                $t->string('kind', 10)->default('service'); // service|part
                $t->unsignedBigInteger('ref_id')->nullable(); // vrc_service id or ERP product id
                $t->string('description', 255);
                $t->decimal('qty', 8, 2)->default(1);
                $t->decimal('unit_price', 15, 2)->default(0);
                $t->decimal('discount', 15, 2)->default(0);
                $t->string('discount_type', 12)->default('fixed');
                $t->unsignedBigInteger('tax_rate_id')->nullable();
                $t->decimal('line_total', 15, 2)->default(0);
                $t->integer('sort_order')->default(0);
                $t->timestamps();
                $t->index(['business_id', 'estimate_id']);
            });
        }
    }

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