<?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('sync_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->uuid('op_uuid')->index();      // idempotency key of the operation
            $table->string('entity');
            $table->string('op');
            $table->string('status');              // applied | duplicate | conflict | error
            $table->unsignedBigInteger('server_id')->nullable();
            $table->text('error')->nullable();
            $table->timestamps();

            // One processed result per (entity, op, op_uuid).
            $table->unique(['entity', 'op', 'op_uuid']);
        });
    }

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