<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('terminal_sync_logs', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('terminal_id')->comment('终端ID');
            $table->enum('status', ['pending', 'syncing', 'synced', 'failed'])
                  ->default('pending')
                  ->comment('同步状态');
            $table->json('config_snapshot')->nullable()->comment('配置快照');
            $table->timestamp('synced_at')->nullable()->comment('同步时间');
            $table->text('error_message')->nullable()->comment('错误信息');
            $table->timestamps();

            // 添加外键约束
            $table->foreign('terminal_id')
                  ->references('id')
                  ->on('terminals')
                  ->onDelete('cascade');

            // 添加索引
            $table->index('status', 'idx_terminal_sync_logs_status');
            $table->index('synced_at', 'idx_terminal_sync_logs_synced_at');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('terminal_sync_logs');
    }
};
