<?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('bookings', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('code', 20)->unique();    // BK-2026-000001 (visible al cliente)

            $table->foreignId('company_id')->constrained()->cascadeOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('customer_id')->constrained()->cascadeOnDelete();
            $table->foreignId('service_id')->constrained();
            $table->foreignId('employee_id')->nullable()->constrained()->nullOnDelete();

            // Estado y origen
            $table->enum('status', [
                'pending', 'confirmed', 'cancelled',
                'completed', 'no_show', 'rescheduled',
            ])->default('pending');
            $table->enum('source', ['public', 'admin', 'employee', 'api'])->default('public');

            // Tiempo
            $table->date('booking_date');
            $table->time('start_time');
            $table->time('end_time');
            $table->dateTime('scheduled_at');         // booking_date + start_time normalizado
            $table->unsignedInteger('duration_minutes');

            // Snapshot precios (no se rompen si cambia el servicio luego)
            $table->decimal('service_price', 12, 2)->default(0);
            $table->decimal('discount_amount', 12, 2)->default(0);
            $table->decimal('total_amount', 12, 2)->default(0);
            $table->decimal('paid_amount', 12, 2)->default(0);
            $table->string('currency', 3)->default('COP');

            // Cupón / promo aplicado
            $table->foreignId('coupon_id')->nullable();
            $table->foreignId('promotion_id')->nullable();

            // Notas / observaciones
            $table->text('customer_notes')->nullable();
            $table->text('internal_notes')->nullable();
            $table->string('cancellation_reason')->nullable();
            $table->timestamp('cancelled_at')->nullable();
            $table->foreignId('cancelled_by')->nullable()->constrained('users')->nullOnDelete();

            // Recordatorios
            $table->timestamp('reminder_sent_at')->nullable();
            $table->timestamp('confirmed_at')->nullable();
            $table->timestamp('completed_at')->nullable();

            // Reprogramación
            $table->foreignId('rescheduled_from_id')->nullable();
            $table->foreignId('rescheduled_to_id')->nullable();

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

            $table->index(['company_id', 'status', 'scheduled_at']);
            $table->index(['company_id', 'employee_id', 'scheduled_at']);
            $table->index(['company_id', 'customer_id', 'scheduled_at']);
            $table->index(['company_id', 'booking_date']);
            $table->index('scheduled_at');
        });
    }

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