<?php

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

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

        $methods = [
            ['code' => 'visual', 'name' => '육안검사', 'sort_order' => 1],
            ['code' => 'check', 'name' => '체크검사', 'sort_order' => 2],
            ['code' => 'mill_sheet', 'name' => '공급업체 밀시트', 'sort_order' => 3],
            ['code' => 'certified_agency', 'name' => '공인시험기관', 'sort_order' => 4],
            ['code' => 'substitute_cert', 'name' => '공급업체 성적서 대체', 'sort_order' => 5],
            ['code' => 'other', 'name' => '기타', 'sort_order' => 6],
        ];

        foreach ($methods as $item) {
            DB::table('common_codes')->updateOrInsert(
                ['code_group' => 'inspection_method', 'code' => $item['code'], 'tenant_id' => null],
                [
                    'code_group' => 'inspection_method',
                    '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', 'inspection_method')
            ->whereNull('tenant_id')
            ->delete();
    }
};
