<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\TaxType;
use App\Models\TaxName;

class TaxTypeSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Helper to get tax name id by code (matching TaxNameSeeder codes)
        $getTaxNameId = function ($code) {
            $taxName = TaxName::where('code', $code)->first();
            return $taxName ? $taxName->id : null;
        };

        $data = [
            [
                'code' => '1',
                'tax_name_id' => $getTaxNameId('1'),
                'category' => 'PPN_12',
                'tax_base_type' => 'formula',
                'tax_base_formula' => '11/12',
                'rate' => 12,
                'status' => true,
                'description' => 'Pajak Pertambahan Nilai 12%',
                'updated_date' => now(),
            ],
            [
                'code' => '2',
                'tax_name_id' => $getTaxNameId('1'),
                'category' => 'PPN_0',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 0,
                'status' => true,
                'description' => 'Pajak Pertambahan Nilai 0%',
                'updated_date' => now(),
            ],
            [
                'code' => '3',
                'tax_name_id' => $getTaxNameId('1'),
                'category' => 'PPN_1,2',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 1.2,
                'status' => true,
                'description' => 'Pajak Pertambahan Nilai 1,2%',
                'updated_date' => now(),
            ],
            [
                'code' => '4',
                'tax_name_id' => $getTaxNameId('1'),
                'category' => 'PPN_1,62',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 1.62,
                'status' => true,
                'description' => 'Pajak Pertambahan Nilai 1,62%',
                'updated_date' => now(),
            ],
            [
                'code' => '5',
                'tax_name_id' => $getTaxNameId('2'),
                'category' => 'PPH23_2',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 2,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 23 2%',
                'updated_date' => now(),
            ],
            [
                'code' => '6',
                'tax_name_id' => $getTaxNameId('2'),
                'category' => 'PPH23_4',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 4,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 23 4%',
                'updated_date' => now(),
            ],
            [
                'code' => '7',
                'tax_name_id' => $getTaxNameId('2'),
                'category' => 'PPH23_15',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 15,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 23 15%',
                'updated_date' => now(),
            ],
            [
                'code' => '8',
                'tax_name_id' => $getTaxNameId('2'),
                'category' => 'PPH23_30',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 30,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 23 30%',
                'updated_date' => now(),
            ],
            [
                'code' => '9',
                'tax_name_id' => $getTaxNameId('3'),
                'category' => 'PPH22_1,5',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 1.5,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 22 1,5%',
                'updated_date' => now(),
            ],
            [
                'code' => '10',
                'tax_name_id' => $getTaxNameId('3'),
                'category' => 'PPH22_3',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 3,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 22 3%',
                'updated_date' => now(),
            ],
            [
                'code' => '11',
                'tax_name_id' => $getTaxNameId('3'),
                'category' => 'PPH22_2,5',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 2.5,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 22 2,5%',
                'updated_date' => now(),
            ],
            [
                'code' => '12',
                'tax_name_id' => $getTaxNameId('3'),
                'category' => 'PPH22_7,5',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 7.5,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 22 7,5%',
                'updated_date' => now(),
            ],
            [
                'code' => '13',
                'tax_name_id' => $getTaxNameId('3'),
                'category' => 'PPH22_0,25',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 0.25,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 22 0,25%',
                'updated_date' => now(),
            ],
            [
                'code' => '14',
                'tax_name_id' => $getTaxNameId('3'),
                'category' => 'PPH22_0,45',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 0.45,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 22 0,45%',
                'updated_date' => now(),
            ],
            [
                'code' => '15',
                'tax_name_id' => $getTaxNameId('4'),
                'category' => 'PPH4(2)_0',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 0,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 4(2) 0%',
                'updated_date' => now(),
            ],
            [
                'code' => '16',
                'tax_name_id' => $getTaxNameId('4'),
                'category' => 'PPH4(2)_0,1',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 0.1,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 4(2) 0,1%',
                'updated_date' => now(),
            ],
            [
                'code' => '17',
                'tax_name_id' => $getTaxNameId('4'),
                'category' => 'PPH4(2)_0,5',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 0.5,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 4(2) 0,5%',
                'updated_date' => now(),
            ],
            [
                'code' => '18',
                'tax_name_id' => $getTaxNameId('4'),
                'category' => 'PPH4(2)_1',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 1,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 4(2) 1%',
                'updated_date' => now(),
            ],
            [
                'code' => '19',
                'tax_name_id' => $getTaxNameId('4'),
                'category' => 'PPH4(2)_1,75',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 1.75,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 4(2) 1,75%',
                'updated_date' => now(),
            ],
            [
                'code' => '20',
                'tax_name_id' => $getTaxNameId('4'),
                'category' => 'PPH4(2)_2,5',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 2.5,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 4(2) 2,5%',
                'updated_date' => now(),
            ],
            [
                'code' => '21',
                'tax_name_id' => $getTaxNameId('4'),
                'category' => 'PPH4(2)_2,65',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 2.65,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 4(2) 2,65%',
                'updated_date' => now(),
            ],
            [
                'code' => '22',
                'tax_name_id' => $getTaxNameId('4'),
                'category' => 'PPH4(2)_3,5',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 3.5,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 4(2) 3,5%',
                'updated_date' => now(),
            ],
            [
                'code' => '23',
                'tax_name_id' => $getTaxNameId('4'),
                'category' => 'PPH4(2)_4',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 4,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 4(2) 4%',
                'updated_date' => now(),
            ],
            [
                'code' => '24',
                'tax_name_id' => $getTaxNameId('4'),
                'category' => 'PPH4(2)_6',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 6,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 4(2) 6%',
                'updated_date' => now(),
            ],
            [
                'code' => '25',
                'tax_name_id' => $getTaxNameId('4'),
                'category' => 'PPH4(2)_10',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 10,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 4(2) 10%',
                'updated_date' => now(),
            ],
            [
                'code' => '26',
                'tax_name_id' => $getTaxNameId('4'),
                'category' => 'PPH4(2)_20',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 20,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 4(2) 20%',
                'updated_date' => now(),
            ],
            [
                'code' => '27',
                'tax_name_id' => $getTaxNameId('4'),
                'category' => 'PPH4(2)_25',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 25,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 4(2) 25%',
                'updated_date' => now(),
            ],
            [
                'code' => '28',
                'tax_name_id' => $getTaxNameId('5'),
                'category' => 'PPH21_5',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 5,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 21 5%',
                'updated_date' => now(),
            ],
            [
                'code' => '29',
                'tax_name_id' => $getTaxNameId('5'),
                'category' => 'PPH21_15',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 15,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 21 15%',
                'updated_date' => now(),
            ],
            [
                'code' => '30',
                'tax_name_id' => $getTaxNameId('5'),
                'category' => 'PPH21_25',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 25,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 21 25%',
                'updated_date' => now(),
            ],
            [
                'code' => '31',
                'tax_name_id' => $getTaxNameId('5'),
                'category' => 'PPH21_30',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 30,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 21 30%',
                'updated_date' => now(),
            ],
            [
                'code' => '32',
                'tax_name_id' => $getTaxNameId('5'),
                'category' => 'PPH21_35',
                'tax_base_type' => 'price',
                'tax_base_formula' => null,
                'rate' => 35,
                'status' => true,
                'description' => 'Pajak Penghasilan Pasal 21 35%',
                'updated_date' => now(),
            ],
        ];

        foreach ($data as $item) {
            // Pastikan tax_name_id valid sebelum insert
            if ($item['tax_name_id']) {
                TaxType::firstOrCreate(
                    ['code' => $item['code']],
                    $item
                );
            }
        }
    }
}
