<?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('work_orders', function (Blueprint $t) {
            $t->id();
            $t->foreignId('company_id')->constrained()->cascadeOnDelete();
            $t->string('number', 32);                            // WO-001 — generat per tenant
            $t->foreignId('client_id')->nullable()->constrained()->nullOnDelete();
            $t->foreignId('vehicle_id')->nullable()->constrained()->nullOnDelete();
            $t->foreignId('master_id')->nullable()->constrained('users')->nullOnDelete();
            $t->foreignId('deal_id')->nullable()->constrained()->nullOnDelete();
            $t->foreignId('appointment_id')->nullable()->constrained()->nullOnDelete();

            $t->date('opened_at');
            $t->date('closed_at')->nullable();
            $t->unsignedInteger('mileage_in')->nullable();
            $t->unsignedInteger('mileage_out')->nullable();

            $t->text('complaint')->nullable();                    // jaluire client
            $t->text('diagnosis')->nullable();
            $t->text('recommendations')->nullable();

            $t->string('status')->default('new');
            // new / diagnosis / agreement / approved / in_work /
            // awaiting_parts / ready / done / cancelled
            $t->string('pay_status')->default('unpaid');          // unpaid / partial / paid
            $t->boolean('approved')->default(false);
            $t->timestamp('approved_at')->nullable();

            $t->decimal('discount_pct', 5, 2)->default(0);
            $t->decimal('total', 12, 2)->default(0);              // calculat (works + parts - discount)

            $t->timestamps();
            $t->softDeletes();

            $t->unique(['company_id', 'number']);
            $t->index(['company_id', 'status']);
            $t->index(['company_id', 'opened_at']);
        });

        Schema::create('wo_works', function (Blueprint $t) {
            $t->id();
            $t->foreignId('company_id')->constrained()->cascadeOnDelete();
            $t->foreignId('work_order_id')->constrained()->cascadeOnDelete();
            $t->foreignId('labor_id')->nullable()->constrained()->nullOnDelete();
            $t->foreignId('master_id')->nullable()->constrained('users')->nullOnDelete();

            $t->string('name');                                   // snapshot din labor.name_ro la momentul adăugării
            $t->decimal('hours', 5, 2)->default(1);
            $t->decimal('price_per_hour', 10, 2)->default(0);     // tarif normo-oră
            $t->decimal('total', 10, 2)->default(0);              // hours * price_per_hour
            $t->string('status')->default('todo');                // todo / in_progress / done
            $t->text('notes')->nullable();

            $t->timestamps();

            $t->index(['company_id', 'work_order_id']);
        });

        Schema::create('wo_parts', function (Blueprint $t) {
            $t->id();
            $t->foreignId('company_id')->constrained()->cascadeOnDelete();
            $t->foreignId('work_order_id')->constrained()->cascadeOnDelete();

            $t->string('name');                                   // ex: "Filtru ulei MANN W811/80"
            $t->string('article', 64)->nullable();
            $t->string('brand', 64)->nullable();
            $t->decimal('qty', 8, 2)->default(1);
            $t->string('unit', 16)->default('buc');
            $t->decimal('buy_price', 10, 2)->default(0);
            $t->decimal('sell_price', 10, 2)->default(0);
            $t->decimal('discount_pct', 5, 2)->default(0);
            $t->decimal('total', 12, 2)->default(0);              // qty * sell_price * (1-disc/100)
            $t->string('status')->default('needed');              // needed / ordered / delivered / installed
            $t->text('notes')->nullable();

            $t->timestamps();

            $t->index(['company_id', 'work_order_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('wo_parts');
        Schema::dropIfExists('wo_works');
        Schema::dropIfExists('work_orders');
    }
};
