<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('core_customers', function (Blueprint $table) {
            $table->id();
            $table->string('customer_code')->unique();
            $table->string('name');
            $table->string('company_name')->nullable();
            $table->string('customer_type')->default('b2c');
            $table->string('status')->default('active');
            $table->string('email')->nullable();
            $table->string('phone')->nullable();
            $table->string('mobile')->nullable();
            $table->string('website')->nullable();
            $table->string('tax_id')->nullable();
            $table->string('vat_number')->nullable();
            $table->decimal('credit_limit', 15, 2)->nullable();
            $table->foreignId('payment_term_id')->nullable()->constrained('core_payment_terms')->nullOnDelete();
            $table->string('currency_code', 3)->default('USD');
            $table->decimal('latitude', 10, 8)->nullable();
            $table->decimal('longitude', 11, 8)->nullable();
            $table->text('notes')->nullable();
            $table->json('tags')->nullable();
            $table->json('metadata')->nullable();
            $table->boolean('is_active')->default(true);

            // CRM Fields - Purchase tracking
            $table->decimal('lifetime_value', 15, 2)->default(0);
            $table->timestamp('first_purchase_date')->nullable();
            $table->timestamp('last_purchase_date')->nullable();
            $table->integer('purchase_count')->default(0);
            $table->decimal('average_order_value', 15, 2)->default(0);

            // CRM Fields - Credit management
            $table->decimal('credit_used', 15, 2)->default(0);
            $table->string('payment_terms')->default('cod');

            // CRM Fields - Discounts and tax
            $table->decimal('discount_percentage', 5, 2)->default(0);
            $table->boolean('tax_exempt')->default(false);

            // CRM Fields - Preferences
            $table->string('preferred_payment_method')->nullable();
            $table->string('preferred_delivery_method')->nullable();

            // CRM Fields - Blacklist
            $table->boolean('is_blacklisted')->default(false);
            $table->text('blacklist_reason')->nullable();

            // CRM Fields - Customer group and company relationship
            $table->foreignId('customer_group_id')->nullable();
            $table->foreignId('company_id')->nullable();

            $table->foreignId('created_by_id')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by_id')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['customer_type', 'status']);
            $table->index('is_active');
            $table->index('is_blacklisted');
            $table->index('customer_group_id');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('core_customers');
    }
};
