<?php

namespace Database\Seeders;

use App\Models\Setting;
use App\Models\TaxRule;
use Illuminate\Database\Seeder;

class TaxSeeder extends Seeder
{
    public function run(): void
    {
        // Default tax settings
        $settings = [
            ['key'=>'tax_enabled',          'value'=>'1',                   'group'=>'tax','type'=>'boolean','label'=>'Tax Support',                'description'=>'Enable the taxation engine.'],
            ['key'=>'tax_calculation_type', 'value'=>'exclusive',           'group'=>'tax','type'=>'select', 'label'=>'Tax Calculation Type',       'description'=>'Exclusive = tax added on top. Inclusive = tax included in price.'],
            ['key'=>'tax_id_label',         'value'=>'Tax ID/VAT/GST',      'group'=>'tax','type'=>'text',   'label'=>'Tax ID Label',               'description'=>'Label shown on invoices for tax identification number.'],
            ['key'=>'tax_discount_logic',   'value'=>'tax_after_discount',  'group'=>'tax','type'=>'select', 'label'=>'Discount Logic',             'description'=>'Whether to apply tax before or after discounts.'],
            ['key'=>'tax_home_country',     'value'=>'India',               'group'=>'tax','type'=>'text',   'label'=>'Home Country',               'description'=>'Your business home country for tax purposes.'],
            ['key'=>'tax_exempt_countries', 'value'=>'',                    'group'=>'tax','type'=>'textarea','label'=>'Tax Exempt Countries',      'description'=>'Comma-separated list of countries exempt from tax.'],
            ['key'=>'tax_exempt_if_tax_id', 'value'=>'0',                   'group'=>'tax','type'=>'boolean','label'=>'Exempt if Tax ID Provided',  'description'=>'Automatically exempt customers who provide a valid Tax ID.'],
            ['key'=>'tax_vat_gst_mode',     'value'=>'0',                   'group'=>'tax','type'=>'boolean','label'=>'VAT/GST Mode',               'description'=>'Enable VAT/GST reverse-charge mode for B2B transactions.'],
            ['key'=>'tax_domains',          'value'=>'1',                   'group'=>'tax','type'=>'boolean','label'=>'Tax Domains',                'description'=>'Apply tax to domain-based products.'],
            ['key'=>'tax_billable',         'value'=>'1',                   'group'=>'tax','type'=>'boolean','label'=>'Tax Billable',               'description'=>'Mark all invoices as taxable by default.'],
            ['key'=>'tax_late_fees',        'value'=>'1',                   'group'=>'tax','type'=>'boolean','label'=>'Tax Late Fees',              'description'=>'Apply tax to late payment fees.'],
        ];

        foreach ($settings as $s) {
            Setting::updateOrCreate(['key' => $s['key']], $s);
        }

        // Demo tax rules
        $rules = [
            ['name'=>'GST',  'country'=>'India','state'=>null,  'level'=>'Level 1','rate'=>18.000,'is_active'=>true],
            ['name'=>'CGST', 'country'=>'India','state'=>'Delhi','level'=>'Level 1','rate'=>9.000,'is_active'=>true],
            ['name'=>'SGST', 'country'=>'India','state'=>'Delhi','level'=>'Level 1','rate'=>9.000,'is_active'=>true],
        ];

        foreach ($rules as $r) {
            TaxRule::updateOrCreate(['name' => $r['name'], 'country' => $r['country'], 'state' => $r['state']], $r);
        }
    }
}
