<?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('companies', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();

            // Identidad
            $table->string('name');
            $table->string('slug')->unique();           // URL pública: /empresa/{slug}
            $table->string('legal_name')->nullable();
            $table->string('document_type', 20)->nullable();  // NIT, CC, etc.
            $table->string('document_number', 50)->nullable();

            // Contacto
            $table->string('email');
            $table->string('phone', 25)->nullable();
            $table->string('whatsapp', 25)->nullable();
            $table->string('website')->nullable();

            // Ubicación
            $table->string('address')->nullable();
            $table->string('city', 100)->nullable();
            $table->string('state', 100)->nullable();
            $table->string('country', 100)->default('Colombia');
            $table->string('postal_code', 20)->nullable();
            $table->decimal('latitude', 10, 7)->nullable();
            $table->decimal('longitude', 10, 7)->nullable();

            // Branding
            $table->string('logo_path')->nullable();
            $table->string('cover_path')->nullable();
            $table->string('primary_color', 7)->default('#4f46e5');
            $table->string('secondary_color', 7)->default('#06b6d4');
            $table->text('description')->nullable();
            $table->json('social_links')->nullable();

            // Configuración operativa
            $table->string('timezone', 50)->default('America/Bogota');
            $table->string('locale', 10)->default('es');
            $table->string('currency', 3)->default('COP');
            $table->unsignedInteger('booking_buffer_minutes')->default(0);
            $table->unsignedInteger('booking_min_advance_minutes')->default(30);
            $table->unsignedInteger('booking_max_advance_days')->default(60);
            $table->boolean('allow_guest_booking')->default(true);
            $table->boolean('allow_customer_cancel')->default(true);
            $table->boolean('allow_customer_reschedule')->default(true);
            $table->unsignedInteger('cancellation_hours_limit')->default(2);

            // SaaS state
            $table->enum('status', ['trial', 'active', 'suspended', 'cancelled'])->default('trial');
            $table->foreignId('plan_id')->nullable()->constrained('plans')->nullOnDelete();
            $table->timestamp('trial_ends_at')->nullable();
            $table->timestamp('subscription_starts_at')->nullable();
            $table->timestamp('subscription_ends_at')->nullable();
            $table->timestamp('suspended_at')->nullable();
            $table->string('suspension_reason')->nullable();

            $table->json('settings')->nullable();

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

            $table->index(['status', 'subscription_ends_at']);
            $table->index(['plan_id', 'status']);
        });
    }

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