<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    /**
     * 견적서 옵션용 공통코드 추가
     * - material_type: 제품 종류
     * - painting_type: 도장 옵션
     * - motor_type: 모터 옵션
     * - controller_type: 제어기 옵션
     * - width_construction_cost: 가로시공비 옵션
     * - height_construction_cost: 세로시공비 옵션
     */
    public function up(): void
    {
        $now = now();

        $codes = [
            // 제품 종류 (material_type)
            ['code_group' => 'material_type', 'code' => 'screen', 'name' => '스크린', 'sort_order' => 1],
            ['code_group' => 'material_type', 'code' => 'slat', 'name' => '슬랫', 'sort_order' => 2],
            ['code_group' => 'material_type', 'code' => 'bending', 'name' => '벤딩', 'sort_order' => 3],
            ['code_group' => 'material_type', 'code' => 'jointbar', 'name' => '조인트바', 'sort_order' => 4],

            // 도장 옵션 (painting_type)
            ['code_group' => 'painting_type', 'code' => '0', 'name' => '직접입력', 'sort_order' => 1, 'attributes' => json_encode(['price' => 0])],
            ['code_group' => 'painting_type', 'code' => 'painting_a', 'name' => '도장A', 'sort_order' => 2, 'attributes' => json_encode(['price' => 50000])],
            ['code_group' => 'painting_type', 'code' => 'painting_b', 'name' => '도장B', 'sort_order' => 3, 'attributes' => json_encode(['price' => 80000])],

            // 모터 옵션 (motor_type)
            ['code_group' => 'motor_type', 'code' => 'motor_300k', 'name' => '모터 300,000', 'sort_order' => 1, 'attributes' => json_encode(['price' => 300000])],
            ['code_group' => 'motor_type', 'code' => 'motor_500k', 'name' => '모터 500,000', 'sort_order' => 2, 'attributes' => json_encode(['price' => 500000])],

            // 제어기 옵션 (controller_type)
            ['code_group' => 'controller_type', 'code' => 'ctrl_150k', 'name' => '제어기 150,000', 'sort_order' => 1, 'attributes' => json_encode(['price' => 150000])],
            ['code_group' => 'controller_type', 'code' => 'ctrl_250k', 'name' => '제어기 250,000', 'sort_order' => 2, 'attributes' => json_encode(['price' => 250000])],

            // 가로시공비 옵션 (width_construction_cost)
            ['code_group' => 'width_construction_cost', 'code' => 'w_3_4m', 'name' => '3.01~4.0M', 'sort_order' => 1, 'attributes' => json_encode(['price' => 300000])],
            ['code_group' => 'width_construction_cost', 'code' => 'w_4_5m', 'name' => '4.01~5.0M', 'sort_order' => 2, 'attributes' => json_encode(['price' => 400000])],
            ['code_group' => 'width_construction_cost', 'code' => 'w_5_6m', 'name' => '5.01~6.0M', 'sort_order' => 3, 'attributes' => json_encode(['price' => 500000])],
            ['code_group' => 'width_construction_cost', 'code' => 'w_6_7m', 'name' => '6.01~7.0M', 'sort_order' => 4, 'attributes' => json_encode(['price' => 600000])],

            // 세로시공비 옵션 (height_construction_cost)
            ['code_group' => 'height_construction_cost', 'code' => 'h_3_4m', 'name' => '3.51~4.5M', 'sort_order' => 1, 'attributes' => json_encode(['price' => 5000])],
            ['code_group' => 'height_construction_cost', 'code' => 'h_4_5m', 'name' => '4.51~5.5M', 'sort_order' => 2, 'attributes' => json_encode(['price' => 8000])],
            ['code_group' => 'height_construction_cost', 'code' => 'h_5_6m', 'name' => '5.51~6.5M', 'sort_order' => 3, 'attributes' => json_encode(['price' => 10000])],
        ];

        foreach ($codes as $code) {
            DB::table('common_codes')->insert(array_merge($code, [
                'tenant_id' => null, // 전역 코드 (모든 테넌트에서 사용)
                'is_active' => true,
                'created_at' => $now,
                'updated_at' => $now,
            ]));
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        $codeGroups = [
            'material_type',
            'painting_type',
            'motor_type',
            'controller_type',
            'width_construction_cost',
            'height_construction_cost',
        ];

        DB::table('common_codes')
            ->whereIn('code_group', $codeGroups)
            ->delete();
    }
};
