<?php

namespace Database\Seeders;

use App\Models\TaxGroup;
use App\Models\TaxRate;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class TaxSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Tax Rates (25 items)
        $taxRates = [
            ['name' => 'VAT Standard', 'code' => 'VAT20', 'rate' => 20.00, 'type' => 'percentage', 'description' => 'Standard VAT rate', 'is_active' => true],
            ['name' => 'VAT Reduced', 'code' => 'VAT5', 'rate' => 5.00, 'type' => 'percentage', 'description' => 'Reduced VAT rate for essentials', 'is_active' => true],
            ['name' => 'VAT Zero', 'code' => 'VAT0', 'rate' => 0.00, 'type' => 'percentage', 'description' => 'Zero-rated VAT for exports', 'is_active' => true],
            ['name' => 'GST Standard', 'code' => 'GST18', 'rate' => 18.00, 'type' => 'percentage', 'description' => 'Standard GST rate', 'is_active' => true],
            ['name' => 'GST Reduced', 'code' => 'GST12', 'rate' => 12.00, 'type' => 'percentage', 'description' => 'Reduced GST rate', 'is_active' => true],
            ['name' => 'GST Low', 'code' => 'GST5', 'rate' => 5.00, 'type' => 'percentage', 'description' => 'Low GST rate', 'is_active' => true],
            ['name' => 'Service Tax', 'code' => 'SVC15', 'rate' => 15.00, 'type' => 'percentage', 'description' => 'Service tax', 'is_active' => true],
            ['name' => 'Education Cess', 'code' => 'EDUC2', 'rate' => 2.00, 'type' => 'percentage', 'description' => 'Education cess', 'is_active' => true],
            ['name' => 'Higher Education Cess', 'code' => 'HEDUC1', 'rate' => 1.00, 'type' => 'percentage', 'description' => 'Higher education cess', 'is_active' => true],
            ['name' => 'Luxury Tax', 'code' => 'LUX28', 'rate' => 28.00, 'type' => 'percentage', 'description' => 'Luxury goods tax', 'is_active' => true],
            ['name' => 'Automobile Cess', 'code' => 'AUTO22', 'rate' => 22.00, 'type' => 'percentage', 'description' => 'Automobile cess', 'is_active' => true],
            ['name' => 'Sales Tax', 'code' => 'SALES10', 'rate' => 10.00, 'type' => 'percentage', 'description' => 'General sales tax', 'is_active' => true],
            ['name' => 'State Tax', 'code' => 'STATE8', 'rate' => 8.00, 'type' => 'percentage', 'description' => 'State level tax', 'is_active' => true],
            ['name' => 'Local Tax', 'code' => 'LOCAL5', 'rate' => 5.00, 'type' => 'percentage', 'description' => 'Local municipal tax', 'is_active' => true],
            ['name' => 'Import Duty', 'code' => 'IMP10', 'rate' => 10.00, 'type' => 'percentage', 'description' => 'Basic import duty', 'is_active' => true],
            ['name' => 'Export Duty', 'code' => 'EXP0', 'rate' => 0.00, 'type' => 'percentage', 'description' => 'Export duty exempt', 'is_active' => true],
            ['name' => 'Stamp Duty', 'code' => 'STAMP5', 'rate' => 5.00, 'type' => 'percentage', 'description' => 'Stamp duty for documents', 'is_active' => true],
            ['name' => 'TCS', 'code' => 'TCS1', 'rate' => 1.00, 'type' => 'percentage', 'description' => 'Tax Collected at Source', 'is_active' => true],
            ['name' => 'IGST', 'code' => 'IGST18', 'rate' => 18.00, 'type' => 'percentage', 'description' => 'Integrated GST', 'is_active' => true],
            ['name' => 'CGST', 'code' => 'CGST9', 'rate' => 9.00, 'type' => 'percentage', 'description' => 'Central GST', 'is_active' => true],
            ['name' => 'SGST', 'code' => 'SGST9', 'rate' => 9.00, 'type' => 'percentage', 'description' => 'State GST', 'is_active' => true],
            ['name' => 'Excise Duty', 'code' => 'EXCISE12', 'rate' => 12.00, 'type' => 'percentage', 'description' => 'Excise duty', 'is_active' => true],
            ['name' => 'Fixed Service Fee', 'code' => 'FEE50', 'rate' => 50.00, 'type' => 'fixed', 'description' => 'Fixed service fee', 'is_active' => true],
            ['name' => 'Processing Fee', 'code' => 'PROC25', 'rate' => 25.00, 'type' => 'fixed', 'description' => 'Processing fee', 'is_active' => true],
            ['name' => 'Environmental Levy', 'code' => 'ENV3', 'rate' => 3.00, 'type' => 'percentage', 'description' => 'Environmental protection levy', 'is_active' => true],
        ];

        $taxRateIds = [];
        foreach ($taxRates as $rate) {
            $taxRate = TaxRate::updateOrCreate(
                ['code' => $rate['code']],
                $rate
            );
            $taxRateIds[$rate['code']] = $taxRate->id;
        }

        // Tax Groups (22 items)
        $taxGroups = [
            ['name' => 'Standard VAT', 'code' => 'TG-VAT20', 'description' => 'Default VAT for goods', 'is_active' => true, 'rates' => ['VAT20']],
            ['name' => 'Service + Cess', 'code' => 'TG-SVC', 'description' => 'Service with education cess', 'is_active' => true, 'rates' => ['SVC15', 'EDUC2']],
            ['name' => 'Zero Rated', 'code' => 'TG-ZERO', 'description' => 'Exports and essentials', 'is_active' => true, 'rates' => ['VAT0']],
            ['name' => 'Reduced Rate', 'code' => 'TG-REDUCED', 'description' => 'Books and print media', 'is_active' => true, 'rates' => ['VAT5']],
            ['name' => 'Luxury Goods', 'code' => 'TG-LUXURY', 'description' => 'Premium products', 'is_active' => true, 'rates' => ['LUX28', 'EDUC2']],
            ['name' => 'Food & Beverages', 'code' => 'TG-FOOD', 'description' => 'Restaurant services', 'is_active' => true, 'rates' => ['GST5']],
            ['name' => 'Digital Services', 'code' => 'TG-DIGITAL', 'description' => 'Software and SaaS', 'is_active' => true, 'rates' => ['GST18']],
            ['name' => 'Healthcare', 'code' => 'TG-HEALTH', 'description' => 'Medical supplies', 'is_active' => true, 'rates' => ['VAT0']],
            ['name' => 'Education', 'code' => 'TG-EDU', 'description' => 'Educational materials', 'is_active' => true, 'rates' => ['VAT0']],
            ['name' => 'Real Estate', 'code' => 'TG-REALTY', 'description' => 'Property transactions', 'is_active' => true, 'rates' => ['GST12', 'STAMP5']],
            ['name' => 'Telecom Services', 'code' => 'TG-TELECOM', 'description' => 'Communication services', 'is_active' => true, 'rates' => ['GST18']],
            ['name' => 'Financial Services', 'code' => 'TG-FINANCE', 'description' => 'Banking and insurance', 'is_active' => true, 'rates' => ['GST18']],
            ['name' => 'Entertainment', 'code' => 'TG-ENTERTAIN', 'description' => 'Movies and events', 'is_active' => false, 'rates' => ['LUX28', 'LOCAL5']],
            ['name' => 'Automotive', 'code' => 'TG-AUTO', 'description' => 'Vehicles and parts', 'is_active' => true, 'rates' => ['LUX28', 'AUTO22']],
            ['name' => 'Textile', 'code' => 'TG-TEXTILE', 'description' => 'Clothing and fabrics', 'is_active' => true, 'rates' => ['GST5']],
            ['name' => 'Agriculture', 'code' => 'TG-AGRI', 'description' => 'Farm products', 'is_active' => true, 'rates' => ['VAT0']],
            ['name' => 'Jewelry', 'code' => 'TG-JEWEL', 'description' => 'Gold and precious metals', 'is_active' => true, 'rates' => ['GST5']],
            ['name' => 'Import Composite', 'code' => 'TG-IMPORT', 'description' => 'Imported goods', 'is_active' => true, 'rates' => ['IMP10', 'IGST18']],
            ['name' => 'Export Incentive', 'code' => 'TG-EXPORT', 'description' => 'Export transactions', 'is_active' => true, 'rates' => ['EXP0']],
            ['name' => 'Construction', 'code' => 'TG-CONST', 'description' => 'Building materials', 'is_active' => true, 'rates' => ['LUX28']],
            ['name' => 'Hospitality', 'code' => 'TG-HOTEL', 'description' => 'Hotels and tourism', 'is_active' => true, 'rates' => ['GST12']],
            ['name' => 'E-commerce', 'code' => 'TG-ECOM', 'description' => 'Online marketplace', 'is_active' => true, 'rates' => ['TCS1', 'GST18']],
        ];

        foreach ($taxGroups as $groupData) {
            $rates = $groupData['rates'];
            unset($groupData['rates']);

            $group = TaxGroup::updateOrCreate(
                ['code' => $groupData['code']],
                $groupData
            );

            // Attach rates to group
            $rateIdsToSync = [];
            foreach ($rates as $index => $rateCode) {
                if (isset($taxRateIds[$rateCode])) {
                    $rateIdsToSync[$taxRateIds[$rateCode]] = ['sort_order' => $index];
                }
            }

            // Sync the rates via pivot table
            DB::table('tax_group_rates')->where('tax_group_id', $group->id)->delete();
            foreach ($rateIdsToSync as $rateId => $pivot) {
                DB::table('tax_group_rates')->insert([
                    'tax_group_id' => $group->id,
                    'tax_rate_id' => $rateId,
                    'sort_order' => $pivot['sort_order'],
                    'created_at' => now(),
                    'updated_at' => now(),
                ]);
            }
        }
    }
}
