<?php

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

/**
 * 품목 파일 필드 추가
 *
 * 프론트엔드 요구사항 (BACKEND_DEVELOPMENT_ROADMAP_V2.md):
 * - 절곡도 (bending_diagram)
 * - 시방서 (specification_file, specification_file_name)
 * - 인정서 (certification_file, certification_file_name)
 * - 인증 정보 (certification_number, certification_start_date, certification_end_date)
 *
 * 파일 저장 방식:
 * - Storage::putFile('items/{code}/{type}', $file)
 * - URL 저장: Storage::url($path) 또는 relative path
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('products', function (Blueprint $table) {
            // ================================================
            // 절곡도 (Bending Diagram)
            // ================================================
            if (! Schema::hasColumn('products', 'bending_diagram')) {
                $table->string('bending_diagram', 255)
                    ->nullable()
                    ->after('part_type')
                    ->comment('절곡도 파일 경로 (이미지 URL)');
            }

            if (! Schema::hasColumn('products', 'bending_details')) {
                $table->json('bending_details')
                    ->nullable()
                    ->after('bending_diagram')
                    ->comment('절곡 상세 정보 (BendingDetail[])');
            }

            // ================================================
            // 시방서 (Specification File)
            // ================================================
            if (! Schema::hasColumn('products', 'specification_file')) {
                $table->string('specification_file', 255)
                    ->nullable()
                    ->after('bending_details')
                    ->comment('시방서 파일 경로');
            }

            if (! Schema::hasColumn('products', 'specification_file_name')) {
                $table->string('specification_file_name', 255)
                    ->nullable()
                    ->after('specification_file')
                    ->comment('시방서 원본 파일명');
            }

            // ================================================
            // 인정서 (Certification File)
            // ================================================
            if (! Schema::hasColumn('products', 'certification_file')) {
                $table->string('certification_file', 255)
                    ->nullable()
                    ->after('specification_file_name')
                    ->comment('인정서 파일 경로');
            }

            if (! Schema::hasColumn('products', 'certification_file_name')) {
                $table->string('certification_file_name', 255)
                    ->nullable()
                    ->after('certification_file')
                    ->comment('인정서 원본 파일명');
            }

            // ================================================
            // 인증 정보 (Certification Info)
            // ================================================
            if (! Schema::hasColumn('products', 'certification_number')) {
                $table->string('certification_number', 50)
                    ->nullable()
                    ->after('certification_file_name')
                    ->comment('인증번호');
            }

            if (! Schema::hasColumn('products', 'certification_start_date')) {
                $table->date('certification_start_date')
                    ->nullable()
                    ->after('certification_number')
                    ->comment('인증 시작일');
            }

            if (! Schema::hasColumn('products', 'certification_end_date')) {
                $table->date('certification_end_date')
                    ->nullable()
                    ->after('certification_start_date')
                    ->comment('인증 종료일');
            }
        });
    }

    public function down(): void
    {
        Schema::table('products', function (Blueprint $table) {
            $columns = [
                'certification_end_date',
                'certification_start_date',
                'certification_number',
                'certification_file_name',
                'certification_file',
                'specification_file_name',
                'specification_file',
                'bending_details',
                'bending_diagram',
            ];

            foreach ($columns as $column) {
                if (Schema::hasColumn('products', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
