<?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('document_templates', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->string('name', 100)->comment('양식명');
            $table->string('category', 50)->nullable()->comment('분류 (품질, 생산 등)');
            $table->string('title', 200)->nullable()->comment('문서 제목');
            $table->string('company_name', 100)->nullable()->comment('회사명');
            $table->string('company_address', 255)->nullable()->comment('회사 주소');
            $table->string('company_contact', 100)->nullable()->comment('회사 연락처');
            $table->string('footer_remark_label', 50)->default('부적합 내용')->comment('비고 라벨');
            $table->string('footer_judgement_label', 50)->default('종합판정')->comment('판정 라벨');
            $table->json('footer_judgement_options')->nullable()->comment('판정 옵션 (적합/부적합 등)');
            $table->boolean('is_active')->default(true)->comment('활성 여부');
            $table->timestamps();
            $table->softDeletes();

            $table->index(['tenant_id', 'category']);
            $table->index(['tenant_id', 'is_active']);
        });

        // 결재라인
        Schema::create('document_template_approval_lines', function (Blueprint $table) {
            $table->id();
            $table->foreignId('template_id')->constrained('document_templates')->cascadeOnDelete();
            $table->string('name', 50)->comment('결재 단계명 (작성, 검토, 승인)');
            $table->string('dept', 50)->nullable()->comment('부서');
            $table->string('role', 50)->nullable()->comment('직책/담당자');
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->timestamps();

            $table->index(['template_id', 'sort_order']);
        });

        // 기본 필드 (품명, LOT NO 등)
        Schema::create('document_template_basic_fields', function (Blueprint $table) {
            $table->id();
            $table->foreignId('template_id')->constrained('document_templates')->cascadeOnDelete();
            $table->string('label', 50)->comment('필드 라벨');
            $table->string('field_type', 20)->default('text')->comment('필드 타입 (text, date 등)');
            $table->string('default_value', 255)->nullable()->comment('기본값');
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->timestamps();

            $table->index(['template_id', 'sort_order']);
        });

        // 검사 기준서 섹션
        Schema::create('document_template_sections', function (Blueprint $table) {
            $table->id();
            $table->foreignId('template_id')->constrained('document_templates')->cascadeOnDelete();
            $table->string('title', 100)->comment('섹션 제목 (가이드레일, 연기차단재 등)');
            $table->string('image_path', 255)->nullable()->comment('도해 이미지 경로');
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->timestamps();

            $table->index(['template_id', 'sort_order']);
        });

        // 검사 기준서 섹션 항목
        Schema::create('document_template_section_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('section_id')->constrained('document_template_sections')->cascadeOnDelete();
            $table->string('category', 50)->nullable()->comment('구분 (겉모양, 치수 등)');
            $table->string('item', 100)->comment('검사항목');
            $table->string('standard', 255)->nullable()->comment('검사기준');
            $table->string('method', 50)->nullable()->comment('검사방법');
            $table->string('frequency', 50)->nullable()->comment('검사주기');
            $table->string('regulation', 100)->nullable()->comment('관련규정');
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->timestamps();

            $table->index(['section_id', 'sort_order']);
        });

        // 검사 데이터 테이블 컬럼 설정
        Schema::create('document_template_columns', function (Blueprint $table) {
            $table->id();
            $table->foreignId('template_id')->constrained('document_templates')->cascadeOnDelete();
            $table->string('label', 50)->comment('컬럼명');
            $table->string('width', 20)->default('100px')->comment('컬럼 너비');
            $table->string('column_type', 20)->default('text')->comment('타입 (text, check, measurement, select, complex)');
            $table->string('group_name', 50)->nullable()->comment('그룹명 (상단 헤더 병합용)');
            $table->json('sub_labels')->nullable()->comment('서브라벨 배열');
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->timestamps();

            $table->index(['template_id', 'sort_order']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('document_template_columns');
        Schema::dropIfExists('document_template_section_items');
        Schema::dropIfExists('document_template_sections');
        Schema::dropIfExists('document_template_basic_fields');
        Schema::dropIfExists('document_template_approval_lines');
        Schema::dropIfExists('document_templates');
    }
};