<?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('lead_statuses', function (Blueprint $t) {
            $t->id(); $t->string('name'); $t->string('slug')->unique();
            $t->unsignedInteger('sort_order')->default(0);
            $t->boolean('is_won')->default(false); $t->boolean('is_lost')->default(false); $t->timestamps();
        });

        Schema::create('customers', function (Blueprint $t) {
            $t->id(); $t->string('customer_code')->unique(); $t->string('name');
            $t->string('phone')->nullable()->index(); $t->string('email')->nullable();
            $t->string('company')->nullable(); $t->string('location')->nullable(); $t->text('address')->nullable();
            $t->string('source')->nullable(); $t->text('notes')->nullable(); $t->timestamps();
        });

        Schema::create('leads', function (Blueprint $t) {
            $t->id(); $t->string('lead_code')->unique();
            $t->string('meta_lead_id')->nullable()->unique();
            $t->string('name'); $t->string('phone')->nullable()->index(); $t->string('alternate_phone')->nullable();
            $t->string('email')->nullable()->index(); $t->string('company')->nullable(); $t->string('location')->nullable();
            $t->string('source',50)->default('manual')->index(); $t->string('campaign')->nullable(); $t->string('ad_set')->nullable();
            $t->string('ad')->nullable(); $t->string('lead_form')->nullable(); $t->string('product_service')->nullable();
            $t->string('status',50)->default('new')->index(); $t->string('priority',30)->default('normal');
            $t->foreignId('assigned_to')->nullable()->constrained('users')->nullOnDelete();
            $t->foreignId('assigned_user_id')->nullable()->constrained('users')->nullOnDelete();
            $t->foreignId('customer_id')->nullable()->constrained('customers')->nullOnDelete();
            $t->decimal('expected_value',15,2)->nullable(); $t->timestamp('last_contacted_at')->nullable();
            $t->timestamp('next_followup_at')->nullable()->index(); $t->text('notes')->nullable(); $t->timestamps();
        });

        Schema::create('follow_ups', function (Blueprint $t) {
            $t->id(); $t->foreignId('lead_id')->nullable()->constrained('leads')->cascadeOnDelete();
            $t->foreignId('assigned_to')->nullable()->constrained('users')->nullOnDelete();
            $t->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
            $t->string('type',40)->default('call'); $t->string('subject');
            $t->timestamp('scheduled_at')->nullable(); $t->timestamp('due_at')->nullable()->index();
            $t->text('notes')->nullable(); $t->text('result')->nullable(); $t->timestamp('completed_at')->nullable();
            $t->string('status',30)->default('pending')->index(); $t->timestamps();
        });

        Schema::create('products', function (Blueprint $t) {
            $t->id(); $t->string('name'); $t->string('sku')->nullable()->unique();
            $t->string('category')->nullable(); $t->decimal('price',15,2)->default(0);
            $t->decimal('tax_rate',8,2)->default(0); $t->boolean('active')->default(true); $t->timestamps();
        });

        Schema::create('quotations', function (Blueprint $t) {
            $t->id(); $t->string('quote_no')->unique(); $t->string('quotation_number')->nullable()->unique();
            $t->foreignId('customer_id')->nullable()->constrained('customers')->nullOnDelete();
            $t->foreignId('lead_id')->nullable()->constrained('leads')->nullOnDelete();
            $t->date('quotation_date')->nullable(); $t->date('valid_until')->nullable();
            $t->string('status',30)->default('draft'); $t->decimal('subtotal',15,2)->default(0);
            $t->decimal('discount',15,2)->default(0); $t->decimal('tax',15,2)->default(0);
            $t->decimal('total',15,2)->default(0); $t->text('notes')->nullable(); $t->text('terms')->nullable(); $t->timestamps();
        });
    }

    public function down(): void {
        Schema::dropIfExists('quotations'); Schema::dropIfExists('products'); Schema::dropIfExists('follow_ups');
        Schema::dropIfExists('leads'); Schema::dropIfExists('customers'); Schema::dropIfExists('lead_statuses');
    }
};
