<?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('company_id')->constrained('companies')->cascadeOnDelete();

            $table->string('origem', 20)->default('cron');   // cron|manual|api
            $table->string('status', 20)->default('running'); // running|success|empty|error
            $table->unsignedBigInteger('nsu_inicial')->default(0);
            $table->unsignedBigInteger('nsu_final')->default(0);
            $table->unsignedBigInteger('max_nsu')->default(0);
            $table->unsignedInteger('docs_novos')->default(0);
            $table->unsignedInteger('eventos_novos')->default(0);
            $table->unsignedInteger('requisicoes')->default(0);
            $table->string('cstat', 5)->nullable();
            $table->text('mensagem')->nullable();
            $table->unsignedInteger('duracao_ms')->nullable();

            $table->timestamps();

            $table->index(['company_id', 'created_at']);
        });
    }

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