<?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('leads', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('phone')->index();
            $table->string('email')->nullable();
            $table->string('interested_vehicle')->nullable(); // e.g. "Toyota Harrier Z Leather 2022"
            $table->foreignId('vehicle_id')->nullable()->constrained('vehicles')->nullOnDelete();
            $table->foreignId('auction_vehicle_id')->nullable()->constrained('auction_vehicles')->nullOnDelete();
            $table->decimal('budget_bdt', 14, 2)->nullable();

            $table->enum('source', [
                'website_contact',
                'test_drive_modal',
                'preorder_calculator',
                'auction_page',
                'whatsapp_click',
                'phone_call',
                'walk_in'
            ])->default('website_contact');

            $table->text('message')->nullable();
            $table->foreignId('assigned_to')->nullable()->constrained('users')->nullOnDelete();

            $table->enum('status', [
                'new',
                'contacted',
                'interested',
                'follow_up',
                'converted',
                'lost'
            ])->default('new');

            $table->text('sales_notes')->nullable();
            $table->timestamp('last_contacted_at')->nullable();
            $table->timestamps();

            $table->index('status');
        });
    }

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