<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * Phase 1.2: item_details 테이블 생성
 *
 * items 테이블의 확장 필드 (1:1 관계)
 * - Products 전용 필드 (is_sellable, is_purchasable 등)
 * - Materials 전용 필드 (is_inspection 등)
 */
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('item_details', function (Blueprint $table) {
            $table->id()->comment('ID');
            $table->foreignId('item_id')->comment('품목 ID');

            // Products 전용 필드
            $table->boolean('is_sellable')->default(true)->comment('판매 가능');
            $table->boolean('is_purchasable')->default(false)->comment('구매 가능');
            $table->boolean('is_producible')->default(false)->comment('생산 가능');
            $table->integer('safety_stock')->nullable()->comment('안전 재고');
            $table->integer('lead_time')->nullable()->comment('리드타임(일)');
            $table->boolean('is_variable_size')->default(false)->comment('가변 크기 여부');
            $table->string('product_category', 50)->nullable()->comment('제품 카테고리');
            $table->string('part_type', 50)->nullable()->comment('부품 타입');

            // 파일 필드 (Products)
            $table->string('bending_diagram')->nullable()->comment('벤딩 도면');
            $table->json('bending_details')->nullable()->comment('벤딩 상세');
            $table->string('specification_file')->nullable()->comment('규격서 파일');
            $table->string('specification_file_name')->nullable()->comment('규격서 파일명');
            $table->string('certification_file')->nullable()->comment('인증서 파일');
            $table->string('certification_file_name')->nullable()->comment('인증서 파일명');
            $table->string('certification_number')->nullable()->comment('인증 번호');
            $table->date('certification_start_date')->nullable()->comment('인증 시작일');
            $table->date('certification_end_date')->nullable()->comment('인증 종료일');

            // Materials 전용 필드
            $table->string('is_inspection', 1)->default('N')->comment('검사 여부');
            $table->string('item_name')->nullable()->comment('품명');
            $table->string('specification')->nullable()->comment('규격');
            $table->text('search_tag')->nullable()->comment('검색 태그');
            $table->text('remarks')->nullable()->comment('비고');

            $table->timestamps();

            // 유니크 제약
            $table->unique('item_id', 'uq_item_details_item_id');

            // 외래키
            $table->foreign('item_id')->references('id')->on('items')->cascadeOnDelete();
        });
    }

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