<?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_sign_fields', 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')->constrained('esign_signers')->onDelete('cascade')->comment('서명자 ID');
            $table->unsignedSmallInteger('page_number')->comment('페이지 번호');
            $table->decimal('position_x', 8, 2)->comment('X 좌표 (%)');
            $table->decimal('position_y', 8, 2)->comment('Y 좌표 (%)');
            $table->decimal('width', 8, 2)->comment('너비 (%)');
            $table->decimal('height', 8, 2)->comment('높이 (%)');
            $table->enum('field_type', ['signature', 'stamp', 'text', 'date', 'checkbox'])->default('signature')->comment('필드 유형');
            $table->string('field_label', 100)->nullable()->comment('필드 라벨');
            $table->text('field_value')->nullable()->comment('필드 값');
            $table->boolean('is_required')->default(true)->comment('필수 여부');
            $table->unsignedSmallInteger('sort_order')->default(0)->comment('정렬 순서');
            $table->timestamps();

            $table->index('contract_id', 'idx_esign_fields_contract');
            $table->index('signer_id', 'idx_esign_fields_signer');
        });
    }

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