<?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->uuid('uuid')->unique();
            $table->foreignId('company_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();

            $table->string('first_name');
            $table->string('last_name')->nullable();
            $table->string('email')->nullable();
            $table->string('phone', 25);
            $table->string('document_type', 20)->nullable();
            $table->string('document_number', 50)->nullable();
            $table->date('birthdate')->nullable();
            $table->enum('gender', ['male', 'female', 'other'])->nullable();
            $table->string('address')->nullable();
            $table->string('city', 100)->nullable();

            $table->string('emergency_contact_name')->nullable();
            $table->string('emergency_contact_phone', 25)->nullable();

            $table->text('notes')->nullable();
            $table->boolean('is_frequent')->default(false);
            $table->boolean('is_active')->default(true);
            $table->boolean('marketing_opt_in')->default(true);

            $table->timestamp('first_booking_at')->nullable();
            $table->timestamp('last_booking_at')->nullable();
            $table->unsignedInteger('total_bookings')->default(0);
            $table->decimal('total_spent', 12, 2)->default(0);

            $table->timestamps();
            $table->softDeletes();

            $table->index(['company_id', 'phone']);
            $table->index(['company_id', 'email']);
            $table->index(['company_id', 'is_frequent']);
            $table->unique(['company_id', 'phone']);
        });
    }

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