<?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('esign_audit_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->onDelete('cascade')->comment('테넌트 ID');
            $table->foreignId('contract_id')->constrained('esign_contracts')->onDelete('cascade')->comment('계약 ID');
            $table->foreignId('signer_id')->nullable()->constrained('esign_signers')->nullOnDelete()->comment('서명자 ID');
            $table->string('action', 50)->comment('액션 (created, sent, viewed, otp_sent, authenticated, signed, rejected, completed, cancelled, reminded)');
            $table->string('ip_address', 45)->nullable()->comment('IP 주소');
            $table->string('user_agent', 500)->nullable()->comment('User Agent');
            $table->json('metadata')->nullable()->comment('추가 메타데이터');
            $table->timestamp('created_at')->useCurrent()->comment('생성일시');

            $table->index('contract_id', 'idx_esign_audit_contract');
            $table->index(['contract_id', 'action'], 'idx_esign_audit_contract_action');
        });
    }

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