<?php

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

return new class extends Migration
{
    public function up(): void
    {
        $now = now();

        $categories = [
            ['code' => 'incoming_inspection', 'name' => '수입검사', 'sort_order' => 1],
            ['code' => 'quality_inspection', 'name' => '품질검사', 'sort_order' => 2],
            ['code' => 'outgoing_inspection', 'name' => '출하검사', 'sort_order' => 3],
        ];

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

    public function down(): void
    {
        DB::table('common_codes')
            ->where('code_group', 'document_category')
            ->whereNull('tenant_id')
            ->delete();
    }
};
