<?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('processes', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
            $table->string('process_code', 20)->comment('공정코드 (P-001)');
            $table->string('process_name', 100)->comment('공정명');
            $table->text('description')->nullable()->comment('공정 설명');
            $table->string('process_type', 20)->default('생산')->comment('공정구분 (생산/검사/포장/조립)');
            $table->string('department', 100)->nullable()->comment('담당부서');
            $table->string('work_log_template', 100)->nullable()->comment('작업일지 양식');
            $table->unsignedInteger('required_workers')->default(1)->comment('필요인원');
            $table->string('equipment_info', 255)->nullable()->comment('설비정보');
            $table->json('work_steps')->nullable()->comment('세부 작업단계 배열');
            $table->text('note')->nullable()->comment('비고');
            $table->boolean('is_active')->default(true)->comment('사용여부');
            $table->unsignedBigInteger('created_by')->nullable()->comment('생성자');
            $table->unsignedBigInteger('updated_by')->nullable()->comment('수정자');
            $table->unsignedBigInteger('deleted_by')->nullable()->comment('삭제자');
            $table->timestamps();
            $table->softDeletes();

            // 인덱스
            $table->index('tenant_id');
            $table->unique(['tenant_id', 'process_code']);
            $table->index(['tenant_id', 'is_active']);
            $table->index(['tenant_id', 'process_type']);
        });

        // 공정 자동 분류 규칙 테이블
        Schema::create('process_classification_rules', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('process_id')->comment('공정 ID');
            $table->string('registration_type', 20)->default('pattern')->comment('등록방식 (pattern/individual)');
            $table->string('rule_type', 20)->comment('규칙유형 (품목코드/품목명/품목구분)');
            $table->string('matching_type', 20)->comment('매칭방식 (startsWith/endsWith/contains/equals)');
            $table->string('condition_value', 255)->comment('조건값');
            $table->unsignedInteger('priority')->default(0)->comment('우선순위');
            $table->string('description', 255)->nullable()->comment('설명');
            $table->boolean('is_active')->default(true)->comment('활성여부');
            $table->timestamps();

            // 외래키
            $table->foreign('process_id')->references('id')->on('processes')->onDelete('cascade');

            // 인덱스
            $table->index('process_id');
            $table->index(['process_id', 'is_active', 'priority']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('process_classification_rules');
        Schema::dropIfExists('processes');
    }
};
