<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        $now = now();

        // client_type 코드 그룹
        $clientTypes = [
            ['code' => 'PURCHASE', 'name' => '매입', 'sort_order' => 1],
            ['code' => 'SALES', 'name' => '매출', 'sort_order' => 2],
            ['code' => 'BOTH', 'name' => '매입매출', 'sort_order' => 3],
        ];

        foreach ($clientTypes as $item) {
            DB::table('common_codes')->updateOrInsert(
                ['code_group' => 'client_type', 'code' => $item['code']],
                [
                    'code_group' => 'client_type',
                    'code' => $item['code'],
                    'name' => $item['name'],
                    'sort_order' => $item['sort_order'],
                    'is_active' => true,
                    'created_at' => $now,
                    'updated_at' => $now,
                ]
            );
        }

        // bad_debt_progress 코드 그룹 (악성채권 진행상태)
        $badDebtProgress = [
            ['code' => 'NEGOTIATING', 'name' => '협의중', 'sort_order' => 1],
            ['code' => 'LITIGATION', 'name' => '소송중', 'sort_order' => 2],
            ['code' => 'RECOVERED', 'name' => '회수완료', 'sort_order' => 3],
            ['code' => 'WRITTEN_OFF', 'name' => '대손처리', 'sort_order' => 4],
        ];

        foreach ($badDebtProgress as $item) {
            DB::table('common_codes')->updateOrInsert(
                ['code_group' => 'bad_debt_progress', 'code' => $item['code']],
                [
                    'code_group' => 'bad_debt_progress',
                    'code' => $item['code'],
                    'name' => $item['name'],
                    'sort_order' => $item['sort_order'],
                    'is_active' => true,
                    'created_at' => $now,
                    'updated_at' => $now,
                ]
            );
        }

        // product_category 코드 그룹 (견적 제품 카테고리)
        $productCategories = [
            ['code' => 'SCREEN', 'name' => '스크린', 'sort_order' => 1],
            ['code' => 'STEEL', 'name' => '스틸', 'sort_order' => 2],
        ];

        foreach ($productCategories as $item) {
            DB::table('common_codes')->updateOrInsert(
                ['code_group' => 'product_category', 'code' => $item['code']],
                [
                    'code_group' => 'product_category',
                    'code' => $item['code'],
                    'name' => $item['name'],
                    'sort_order' => $item['sort_order'],
                    'is_active' => true,
                    'created_at' => $now,
                    'updated_at' => $now,
                ]
            );
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        DB::table('common_codes')->where('code_group', 'client_type')->delete();
        DB::table('common_codes')->where('code_group', 'bad_debt_progress')->delete();
        DB::table('common_codes')->where('code_group', 'product_category')->delete();
    }
};
