<?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('crm_leads', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id');
            $table->string('lead_number', 50)->unique();
            $table->enum('source_type', ['website', 'call', 'email', 'referral', 'event', 'cold_outreach']);
            $table->string('lead_name');
            $table->string('lead_email', 255)->nullable();
            $table->string('lead_phone', 20)->nullable();
            $table->string('company_name', 255)->nullable();
            $table->string('industry', 100)->nullable();
            $table->integer('estimated_monthly_trips')->nullable();
            $table->decimal('estimated_monthly_budget', 12, 2)->nullable();
            $table->enum('status', [
                'lead_created', 'lead_qualified', 'contacted', 'quoted',
                'converted', 'lost'
            ])->default('lead_created');
            $table->unsignedBigInteger('assigned_to')->nullable();
            $table->unsignedBigInteger('converted_to_account_id')->nullable();
            $table->text('notes')->nullable();
            $table->json('tags')->nullable();
            $table->timestamps();
            
            $table->foreign('tenant_id')->references('id')->on('tenants')->onDelete('cascade');
            $table->foreign('assigned_to')->references('id')->on('users')->nullOnDelete();
            $table->foreign('converted_to_account_id')->references('id')->on('corporate_accounts')->nullOnDelete();
            $table->index('tenant_id');
            $table->index('status');
            $table->index('assigned_to');
        });
    }

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