<?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('customers', function (Blueprint $table) {
            $table->id();
            $table->ulid('uuid')->unique();
            $table->string('business_name', 200);
            $table->string('phone', 20);
            $table->string('nic', 30)->nullable();
            $table->string('email', 150)->nullable();
            $table->string('street_line1', 255)->nullable();
            $table->string('street_line2', 255)->nullable();
            $table->foreignId('city_id')->nullable()->constrained('cities')->nullOnDelete();
            $table->string('legacy_city', 100)->nullable(); // preserve free-text city during migration
            $table->foreignId('assigned_rep_id')->nullable(); // FK added in sales_reps migration
            $table->foreignId('assigned_user_id')->nullable()->constrained('users')->nullOnDelete(); // legacy assignedTo
            $table->boolean('is_active')->default(true);
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('deleted_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();
            $table->index('phone');
            $table->index('nic');
            $table->index('business_name');
        });

        Schema::create('customer_contacts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('customer_id')->constrained('customers')->cascadeOnUpdate()->cascadeOnDelete();
            $table->string('name', 150);
            $table->string('phone', 20);
            $table->timestamps();
        });

        Schema::create('delivery_addresses', function (Blueprint $table) {
            $table->id();
            $table->foreignId('customer_id')->constrained('customers')->cascadeOnUpdate()->cascadeOnDelete();
            $table->string('label', 100)->nullable();
            $table->string('street_line1', 255);
            $table->string('street_line2', 255)->nullable();
            $table->string('city', 100); // free text — kept simple; could FK to cities later
            $table->text('note')->nullable();
            $table->timestamps();
        });

        Schema::create('customer_credit_balances', function (Blueprint $table) {
            $table->id();
            $table->foreignId('customer_id')->unique()->constrained('customers')->cascadeOnDelete();
            $table->decimal('balance', 14, 2)->default(0);
            $table->timestamps();
        });

        Schema::create('customer_credit_ledger', function (Blueprint $table) {
            $table->id();
            $table->foreignId('customer_id')->constrained('customers')->cascadeOnDelete();
            $table->enum('type', ['credit_in', 'credit_out']);
            $table->decimal('amount', 14, 2);
            $table->string('reference_type', 100)->nullable();
            $table->unsignedBigInteger('reference_id')->nullable();
            $table->decimal('balance_after', 14, 2);
            $table->text('note')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('created_at')->useCurrent();
            $table->index(['customer_id', 'created_at']);
            $table->index(['reference_type', 'reference_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('customer_credit_ledger');
        Schema::dropIfExists('customer_credit_balances');
        Schema::dropIfExists('delivery_addresses');
        Schema::dropIfExists('customer_contacts');
        Schema::dropIfExists('customers');
    }
};
