<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class IncomeTaxBracketSeeder extends Seeder
{
    public function run(): void
    {
        $taxYear = 2024;

        // 기존 해당 연도 데이터 삭제 후 재생성
        DB::table('income_tax_brackets')->where('tax_year', $taxYear)->delete();

        // JSON 데이터 로드 (format: [[salary_from, salary_to, [tax1..tax11]], ...])
        $jsonPath = database_path('seeders/data/income_tax_2024.json');
        $data = json_decode(file_get_contents($jsonPath), true);

        $records = [];
        $now = now();

        foreach ($data as $row) {
            [$salaryFrom, $salaryTo, $taxes] = $row;

            for ($fc = 1; $fc <= 11; $fc++) {
                $records[] = [
                    'tax_year' => $taxYear,
                    'salary_from' => $salaryFrom,
                    'salary_to' => $salaryTo,
                    'family_count' => $fc,
                    'tax_amount' => $taxes[$fc - 1],
                    'created_at' => $now,
                    'updated_at' => $now,
                ];
            }
        }

        // 500건씩 청크 삽입
        foreach (array_chunk($records, 500) as $chunk) {
            DB::table('income_tax_brackets')->insert($chunk);
        }

        $this->command->info("Income tax brackets seeded: {$taxYear}년 ".count($records).'건');
    }
}
