<?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::table('payments', function (Blueprint $table) {
            // Idempotency key para prevenir pagos duplicados en sincronización offline
            $table->uuid('idempotency_key')->nullable()->unique()->after('original_payment_id');

            // Device ID para tracking de dispositivos
            $table->string('device_id', 255)->nullable()->after('idempotency_key');

            // Timestamp de cuando se creó el pago offline (antes de sincronizar)
            $table->timestamp('offline_created_at')->nullable()->after('device_id');

            // Índice compuesto para búsquedas por company + idempotency
            $table->index(['company_id', 'idempotency_key'], 'payments_company_idempotency_idx');
        });
    }

    public function down(): void
    {
        Schema::table('payments', function (Blueprint $table) {
            $table->dropIndex('payments_company_idempotency_idx');
            $table->dropColumn(['idempotency_key', 'device_id', 'offline_created_at']);
        });
    }
};
