<?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('nota_eventos', function (Blueprint $table) {
            $table->id();
            $table->foreignId('company_id')->constrained('companies')->cascadeOnDelete();
            $table->foreignId('nota_fiscal_id')->nullable()->constrained('notas_fiscais')->cascadeOnDelete();

            $table->string('chave', 44);
            $table->unsignedBigInteger('nsu')->nullable();
            $table->string('tipo_evento', 6);              // 110111, 210200, ...
            $table->unsignedInteger('sequencia')->default(1);
            $table->string('descricao')->nullable();
            $table->timestamp('data_evento')->nullable();
            $table->string('protocolo', 20)->nullable();
            $table->string('xml_path')->nullable();

            $table->timestamps();

            $table->unique(['company_id', 'chave', 'tipo_evento', 'sequencia'], 'nota_eventos_unico');
            $table->index(['company_id', 'chave']);
        });
    }

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