<?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('deals', function (Blueprint $t) {
            $t->id();
            $t->foreignId('company_id')->constrained()->cascadeOnDelete();
            $t->foreignId('client_id')->nullable()->constrained()->nullOnDelete();
            $t->foreignId('vehicle_id')->nullable()->constrained()->nullOnDelete();

            $t->string('name');                 // descriere scurtă: BMW X5 — Diagnostic
            $t->decimal('price', 12, 2)->default(0);
            $t->string('stage')->default('new'); // new/contact/agree/scheduled/arrived/in_work/done/lost
            $t->string('source')->nullable();
            $t->text('note')->nullable();

            $t->foreignId('assigned_to')->nullable()->constrained('users')->nullOnDelete();
            $t->timestamp('won_at')->nullable();
            $t->timestamp('lost_at')->nullable();
            $t->string('lost_reason')->nullable();

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

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

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