<?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();

        // order_status 코드 그룹 (수주 상태)
        $orderStatuses = [
            ['code' => 'DRAFT', 'name' => '임시저장', 'sort_order' => 1],
            ['code' => 'CONFIRMED', 'name' => '확정', 'sort_order' => 2],
            ['code' => 'IN_PROGRESS', 'name' => '진행중', 'sort_order' => 3],
            ['code' => 'COMPLETED', 'name' => '완료', 'sort_order' => 4],
            ['code' => 'CANCELLED', 'name' => '취소', 'sort_order' => 5],
        ];

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

        // order_type 코드 그룹 (수주 유형)
        $orderTypes = [
            ['code' => 'ORDER', 'name' => '수주', 'sort_order' => 1],
            ['code' => 'PURCHASE', 'name' => '발주', 'sort_order' => 2],
        ];

        foreach ($orderTypes as $item) {
            DB::table('common_codes')->updateOrInsert(
                ['code_group' => 'order_type', 'code' => $item['code']],
                [
                    'code_group' => 'order_type',
                    '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', 'order_status')->delete();
        DB::table('common_codes')->where('code_group', 'order_type')->delete();
    }
};
