<?php
namespace ComptaFlowUK\Services;

defined('ABSPATH') || exit;

class ChartOfAccounts
{
    private static $accounts = [
        
        // ═══════════════════════════════════════════════════════════════════
        // 0000-0999 : FIXED ASSETS
        // ═══════════════════════════════════════════════════════════════════
        
        // Intangible Assets
        '0010' => ['code' => '0010', 'label' => 'Goodwill', 'category' => 'Intangible Assets', 'keywords' => ['goodwill', 'business purchase', 'acquisition']],
        '0011' => ['code' => '0011', 'label' => 'Goodwill Amortisation', 'category' => 'Intangible Assets', 'keywords' => ['goodwill', 'amortisation', 'write-off']],
        '0020' => ['code' => '0020', 'label' => 'Patents & Licences', 'category' => 'Intangible Assets', 'keywords' => ['patent', 'licence', 'license', 'trademark', 'brand', 'intellectual property', 'ip']],
        '0021' => ['code' => '0021', 'label' => 'Patents & Licences Amortisation', 'category' => 'Intangible Assets', 'keywords' => ['patent', 'licence', 'amortisation']],
        '0030' => ['code' => '0030', 'label' => 'Development Costs', 'category' => 'Intangible Assets', 'keywords' => ['development', 'r&d', 'research', 'software development']],
        '0031' => ['code' => '0031', 'label' => 'Development Costs Amortisation', 'category' => 'Intangible Assets', 'keywords' => ['development', 'amortisation']],
        '0040' => ['code' => '0040', 'label' => 'Website & Software', 'category' => 'Intangible Assets', 'keywords' => ['website', 'software', 'app', 'domain', 'saas', 'digital']],
        '0041' => ['code' => '0041', 'label' => 'Website & Software Amortisation', 'category' => 'Intangible Assets', 'keywords' => ['website', 'software', 'amortisation']],
        
        // Tangible Assets - Property
        '0100' => ['code' => '0100', 'label' => 'Freehold Property', 'category' => 'Property', 'keywords' => ['property', 'building', 'freehold', 'premises', 'office', 'land']],
        '0101' => ['code' => '0101', 'label' => 'Freehold Property Depreciation', 'category' => 'Property', 'keywords' => ['property', 'building', 'depreciation']],
        '0110' => ['code' => '0110', 'label' => 'Leasehold Property', 'category' => 'Property', 'keywords' => ['leasehold', 'lease', 'rent', 'premises']],
        '0111' => ['code' => '0111', 'label' => 'Leasehold Property Depreciation', 'category' => 'Property', 'keywords' => ['leasehold', 'depreciation']],
        '0120' => ['code' => '0120', 'label' => 'Leasehold Improvements', 'category' => 'Property', 'keywords' => ['improvements', 'fit-out', 'renovation', 'refurbishment']],
        '0121' => ['code' => '0121', 'label' => 'Leasehold Improvements Depreciation', 'category' => 'Property', 'keywords' => ['improvements', 'depreciation']],
        
        // Tangible Assets - Plant & Machinery
        '0200' => ['code' => '0200', 'label' => 'Plant & Machinery', 'category' => 'Plant & Machinery', 'keywords' => ['plant', 'machinery', 'equipment', 'industrial', 'manufacturing']],
        '0201' => ['code' => '0201', 'label' => 'Plant & Machinery Depreciation', 'category' => 'Plant & Machinery', 'keywords' => ['plant', 'machinery', 'depreciation']],
        '0210' => ['code' => '0210', 'label' => 'Tools & Small Equipment', 'category' => 'Plant & Machinery', 'keywords' => ['tools', 'equipment', 'small tools', 'hand tools']],
        '0211' => ['code' => '0211', 'label' => 'Tools & Small Equipment Depreciation', 'category' => 'Plant & Machinery', 'keywords' => ['tools', 'depreciation']],
        
        // Tangible Assets - Vehicles
        '0300' => ['code' => '0300', 'label' => 'Motor Vehicles', 'category' => 'Motor Vehicles', 'keywords' => ['vehicle', 'car', 'van', 'truck', 'lorry', 'motor', 'automobile']],
        '0301' => ['code' => '0301', 'label' => 'Motor Vehicles Depreciation', 'category' => 'Motor Vehicles', 'keywords' => ['vehicle', 'car', 'depreciation']],
        
        // Tangible Assets - Office & IT
        '0400' => ['code' => '0400', 'label' => 'Office Equipment', 'category' => 'Office Equipment', 'keywords' => ['office', 'furniture', 'desk', 'chair', 'cabinet', 'shelving']],
        '0401' => ['code' => '0401', 'label' => 'Office Equipment Depreciation', 'category' => 'Office Equipment', 'keywords' => ['office', 'furniture', 'depreciation']],
        '0410' => ['code' => '0410', 'label' => 'Computer Equipment', 'category' => 'Computer Equipment', 'keywords' => ['computer', 'laptop', 'pc', 'mac', 'server', 'printer', 'monitor', 'it', 'hardware']],
        '0411' => ['code' => '0411', 'label' => 'Computer Equipment Depreciation', 'category' => 'Computer Equipment', 'keywords' => ['computer', 'laptop', 'depreciation']],
        
        // Investments
        '0500' => ['code' => '0500', 'label' => 'Investments', 'category' => 'Investments', 'keywords' => ['investment', 'shares', 'securities', 'holdings']],
        '0501' => ['code' => '0501', 'label' => 'Investments - Provision for Diminution', 'category' => 'Investments', 'keywords' => ['investment', 'provision', 'write-down']],
        
        // ═══════════════════════════════════════════════════════════════════
        // 1000-1999 : CURRENT ASSETS
        // ═══════════════════════════════════════════════════════════════════
        
        // Stock
        '1000' => ['code' => '1000', 'label' => 'Stock', 'category' => 'Stock', 'keywords' => ['stock', 'inventory', 'goods', 'raw materials']],
        '1001' => ['code' => '1001', 'label' => 'Work in Progress', 'category' => 'Stock', 'keywords' => ['wip', 'work in progress', 'unfinished']],
        '1002' => ['code' => '1002', 'label' => 'Finished Goods', 'category' => 'Stock', 'keywords' => ['finished goods', 'completed', 'ready']],
        
        // Trade Debtors
        '1100' => ['code' => '1100', 'label' => 'Trade Debtors', 'category' => 'Debtors', 'keywords' => ['debtor', 'receivable', 'customer', 'client', 'owed', 'invoice']],
        '1101' => ['code' => '1101', 'label' => 'Other Debtors', 'category' => 'Debtors', 'keywords' => ['other debtor', 'receivable', 'deposit', 'advance']],
        '1102' => ['code' => '1102', 'label' => 'Bad Debt Provision', 'category' => 'Debtors', 'keywords' => ['bad debt', 'provision', 'doubtful', 'write-off']],
        '1103' => ['code' => '1103', 'label' => 'Prepayments', 'category' => 'Debtors', 'keywords' => ['prepayment', 'advance payment', 'paid in advance']],
        '1104' => ['code' => '1104', 'label' => 'Accrued Income', 'category' => 'Debtors', 'keywords' => ['accrued', 'income', 'earned', 'unbilled']],
        
        // Bank & Cash
        '1200' => ['code' => '1200', 'label' => 'Current Account', 'category' => 'Bank & Cash', 'keywords' => ['bank', 'current account', 'business account', 'main account']],
        '1210' => ['code' => '1210', 'label' => 'Savings Account', 'category' => 'Bank & Cash', 'keywords' => ['savings', 'deposit', 'reserve']],
        '1220' => ['code' => '1220', 'label' => 'Building Society Account', 'category' => 'Bank & Cash', 'keywords' => ['building society', 'savings']],
        '1230' => ['code' => '1230', 'label' => 'Petty Cash', 'category' => 'Bank & Cash', 'keywords' => ['petty cash', 'cash', 'cash box', 'float']],
        '1240' => ['code' => '1240', 'label' => 'Credit Card Account', 'category' => 'Bank & Cash', 'keywords' => ['credit card', 'visa', 'mastercard', 'amex']],
        '1250' => ['code' => '1250', 'label' => 'PayPal / Stripe Account', 'category' => 'Bank & Cash', 'keywords' => ['paypal', 'stripe', 'payment processor', 'online payments', 'merchant']],
        '1260' => ['code' => '1260', 'label' => 'Cash in Transit', 'category' => 'Bank & Cash', 'keywords' => ['cash in transit', 'undeposited', 'clearing']],
        
        // HMRC
        '1300' => ['code' => '1300', 'label' => 'VAT Receivable (Input Tax)', 'category' => 'HMRC', 'keywords' => ['vat', 'input', 'tax', 'hmrc', 'receivable', 'reclaim']],
        '1310' => ['code' => '1310', 'label' => 'Corporation Tax Receivable', 'category' => 'HMRC', 'keywords' => ['corporation tax', 'ct', 'tax refund']],
        '1320' => ['code' => '1320', 'label' => 'Income Tax Receivable', 'category' => 'HMRC', 'keywords' => ['income tax', 'self assessment', 'tax refund', 'paye']],
        
        // ═══════════════════════════════════════════════════════════════════
        // 2000-2999 : CURRENT LIABILITIES
        // ═══════════════════════════════════════════════════════════════════
        
        // Trade Creditors
        '2100' => ['code' => '2100', 'label' => 'Trade Creditors', 'category' => 'Creditors', 'keywords' => ['creditor', 'payable', 'supplier', 'vendor', 'bill', 'owed to']],
        '2101' => ['code' => '2101', 'label' => 'Other Creditors', 'category' => 'Creditors', 'keywords' => ['other creditor', 'payable', 'liability']],
        '2102' => ['code' => '2102', 'label' => 'Accruals', 'category' => 'Creditors', 'keywords' => ['accrual', 'accrued expense', 'liability']],
        '2103' => ['code' => '2103', 'label' => 'Deferred Income', 'category' => 'Creditors', 'keywords' => ['deferred', 'unearned', 'advance', 'deposit received']],
        
        // Tax Liabilities
        '2200' => ['code' => '2200', 'label' => 'Output VAT', 'category' => 'Tax Liabilities', 'keywords' => ['vat', 'output', 'tax', 'hmrc', 'collected', 'payable']],
        '2201' => ['code' => '2201', 'label' => 'Input VAT', 'category' => 'Tax Liabilities', 'keywords' => ['vat', 'input', 'tax', 'purchase', 'reclaim']],
        '2202' => ['code' => '2202', 'label' => 'VAT Liability', 'category' => 'Tax Liabilities', 'keywords' => ['vat', 'liability', 'hmrc', 'control', 'net', 'due']],
        '2203' => ['code' => '2203', 'label' => 'VAT Refund Due', 'category' => 'Tax Liabilities', 'keywords' => ['vat', 'refund', 'hmrc', 'credit', 'owed']],
        '2204' => ['code' => '2204', 'label' => 'VAT Payments on Account', 'category' => 'Tax Liabilities', 'keywords' => ['vat', 'payment', 'account', 'advance', 'instalment']],
        '2210' => ['code' => '2210', 'label' => 'PAYE / NI Payable', 'category' => 'Tax Liabilities', 'keywords' => ['paye', 'national insurance', 'ni', 'tax', 'payroll']],
        '2211' => ['code' => '2211', 'label' => 'Student Loan Deductions', 'category' => 'Tax Liabilities', 'keywords' => ['student loan', 'deduction', 'slc']],
        '2220' => ['code' => '2220', 'label' => 'Corporation Tax Payable', 'category' => 'Tax Liabilities', 'keywords' => ['corporation tax', 'ct', 'payable']],
        '2230' => ['code' => '2230', 'label' => 'Income Tax Payable', 'category' => 'Tax Liabilities', 'keywords' => ['income tax', 'self assessment', 'payable']],
        '2240' => ['code' => '2240', 'label' => 'CIS Tax Deductions', 'category' => 'Tax Liabilities', 'keywords' => ['cis', 'construction industry', 'subcontractor']],
        
        // Payroll Liabilities
        '2300' => ['code' => '2300', 'label' => 'Net Wages Payable', 'category' => 'Payroll Liabilities', 'keywords' => ['wages', 'salary', 'payroll', 'net pay']],
        '2310' => ['code' => '2310', 'label' => 'Pension Fund Payable', 'category' => 'Payroll Liabilities', 'keywords' => ['pension', 'workplace pension', 'auto enrolment', 'nest']],
        
        // Loans
        '2400' => ['code' => '2400', 'label' => 'Bank Loan (Short Term)', 'category' => 'Loans', 'keywords' => ['bank loan', 'short term', 'overdraft', 'facility']],
        '2410' => ['code' => '2410', 'label' => 'Bank Loan (Long Term)', 'category' => 'Loans', 'keywords' => ['bank loan', 'long term', 'mortgage', 'commercial loan']],
        '2420' => ['code' => '2420', 'label' => 'Hire Purchase / Finance Lease', 'category' => 'Loans', 'keywords' => ['hire purchase', 'hp', 'finance lease', 'pcp']],
        '2430' => ['code' => '2430', 'label' => 'Bounce Back Loan', 'category' => 'Loans', 'keywords' => ['bounce back', 'bbls', 'covid', 'government loan']],
        '2440' => ['code' => '2440', 'label' => 'Director\'s Loan Account', 'category' => 'Loans', 'keywords' => ['director', 'loan', 'dla', 'shareholder']],
        '2450' => ['code' => '2450', 'label' => 'Other Loans', 'category' => 'Loans', 'keywords' => ['loan', 'borrowing', 'debt', 'finance']],
        
        // ═══════════════════════════════════════════════════════════════════
        // 3000-3999 : CAPITAL & RESERVES
        // ═══════════════════════════════════════════════════════════════════
        
        '3000' => ['code' => '3000', 'label' => 'Share Capital', 'category' => 'Capital', 'keywords' => ['share capital', 'ordinary shares', 'issued capital']],
        '3001' => ['code' => '3001', 'label' => 'Share Premium', 'category' => 'Capital', 'keywords' => ['share premium', 'premium', 'above par']],
        '3050' => ['code' => '3050', 'label' => 'Capital Introduced (Sole Trader)', 'category' => 'Capital', 'keywords' => ['capital', 'introduced', 'sole trader', 'owner', 'proprietor']],
        '3060' => ['code' => '3060', 'label' => 'Drawings (Sole Trader)', 'category' => 'Capital', 'keywords' => ['drawings', 'withdrawal', 'sole trader', 'owner', 'personal']],
        '3100' => ['code' => '3100', 'label' => 'Retained Earnings', 'category' => 'Reserves', 'keywords' => ['retained', 'earnings', 'profit', 'reserves', 'accumulated']],
        '3200' => ['code' => '3200', 'label' => 'Profit & Loss Account', 'category' => 'Reserves', 'keywords' => ['profit', 'loss', 'p&l', 'result', 'current year']],
        '3300' => ['code' => '3300', 'label' => 'Dividends Paid', 'category' => 'Distributions', 'keywords' => ['dividend', 'distribution', 'shareholder']],
        
        // ═══════════════════════════════════════════════════════════════════
        // 4000-4999 : SALES / REVENUE
        // ═══════════════════════════════════════════════════════════════════
        
        '4000' => ['code' => '4000', 'label' => 'Sales - Standard Rate', 'category' => 'Sales', 'keywords' => ['sales', 'revenue', 'income', 'turnover', 'standard rate']],
        '4001' => ['code' => '4001', 'label' => 'Sales - Reduced Rate', 'category' => 'Sales', 'keywords' => ['sales', 'revenue', 'reduced rate', '5%']],
        '4002' => ['code' => '4002', 'label' => 'Sales - Zero Rated', 'category' => 'Sales', 'keywords' => ['sales', 'revenue', 'zero rated', 'export']],
        '4003' => ['code' => '4003', 'label' => 'Sales - Exempt', 'category' => 'Sales', 'keywords' => ['sales', 'revenue', 'exempt', 'no vat']],
        '4009' => ['code' => '4009', 'label' => 'Credit Notes / Discounts Given', 'category' => 'Sales', 'keywords' => ['credit note', 'discount', 'refund', 'return']],
        '4010' => ['code' => '4010', 'label' => 'Sales - Services', 'category' => 'Sales', 'keywords' => ['service', 'consulting', 'freelance', 'professional', 'fees']],
        '4020' => ['code' => '4020', 'label' => 'Sales - Products / Goods', 'category' => 'Sales', 'keywords' => ['product', 'goods', 'merchandise', 'retail', 'wholesale']],
        '4030' => ['code' => '4030', 'label' => 'Sales - Commissions Received', 'category' => 'Sales', 'keywords' => ['commission', 'referral', 'affiliate', 'introduction']],
        '4040' => ['code' => '4040', 'label' => 'Sales - Royalties Received', 'category' => 'Sales', 'keywords' => ['royalty', 'licence', 'copyright', 'intellectual property']],
        '4050' => ['code' => '4050', 'label' => 'Sales - Subscriptions', 'category' => 'Sales', 'keywords' => ['subscription', 'recurring', 'membership', 'saas']],
        '4100' => ['code' => '4100', 'label' => 'Other Income', 'category' => 'Other Income', 'keywords' => ['other income', 'sundry', 'miscellaneous']],
        '4200' => ['code' => '4200', 'label' => 'Rental Income', 'category' => 'Other Income', 'keywords' => ['rental', 'rent', 'letting', 'property income']],
        '4300' => ['code' => '4300', 'label' => 'Insurance Claims Received', 'category' => 'Other Income', 'keywords' => ['insurance', 'claim', 'payout', 'compensation']],
        '4400' => ['code' => '4400', 'label' => 'Government Grants', 'category' => 'Other Income', 'keywords' => ['grant', 'government', 'subsidy', 'innovate uk', 'seis', 'eis']],
        '4900' => ['code' => '4900', 'label' => 'Interest Received', 'category' => 'Other Income', 'keywords' => ['interest', 'bank interest', 'savings interest']],
        '4901' => ['code' => '4901', 'label' => 'Profit on Disposal of Assets', 'category' => 'Other Income', 'keywords' => ['profit', 'disposal', 'sale', 'asset', 'gain']],
        
        // ═══════════════════════════════════════════════════════════════════
        // 5000-5999 : COST OF SALES / DIRECT COSTS
        // ═══════════════════════════════════════════════════════════════════
        
        '5000' => ['code' => '5000', 'label' => 'Cost of Sales - Materials', 'category' => 'Cost of Sales', 'keywords' => ['materials', 'raw materials', 'purchases', 'cost of sales', 'cogs']],
        '5001' => ['code' => '5001', 'label' => 'Cost of Sales - Labour', 'category' => 'Cost of Sales', 'keywords' => ['labour', 'labor', 'direct', 'production', 'contract']],
        '5002' => ['code' => '5002', 'label' => 'Cost of Sales - Subcontractors', 'category' => 'Cost of Sales', 'keywords' => ['subcontractor', 'outsource', 'freelancer', 'contractor', 'cis']],
        '5003' => ['code' => '5003', 'label' => 'Cost of Sales - Other Direct Costs', 'category' => 'Cost of Sales', 'keywords' => ['direct cost', 'production', 'manufacturing']],
        '5009' => ['code' => '5009', 'label' => 'Credit Notes / Discounts Received', 'category' => 'Cost of Sales', 'keywords' => ['credit note', 'discount', 'rebate', 'return']],
        '5100' => ['code' => '5100', 'label' => 'Carriage / Shipping', 'category' => 'Cost of Sales', 'keywords' => ['carriage', 'shipping', 'delivery', 'freight', 'postage', 'courier']],
        '5200' => ['code' => '5200', 'label' => 'Stock Adjustments', 'category' => 'Cost of Sales', 'keywords' => ['stock', 'adjustment', 'inventory', 'write-off', 'obsolete']],
        
        // ═══════════════════════════════════════════════════════════════════
        // 6000-6999 : OVERHEADS / EXPENSES
        // ═══════════════════════════════════════════════════════════════════
        
        // Staff Costs
        '6000' => ['code' => '6000', 'label' => 'Wages & Salaries', 'category' => 'Staff Costs', 'keywords' => ['wages', 'salary', 'salaries', 'payroll', 'pay', 'staff']],
        '6001' => ['code' => '6001', 'label' => 'Directors Remuneration', 'category' => 'Staff Costs', 'keywords' => ['director', 'remuneration', 'salary', 'fees']],
        '6010' => ['code' => '6010', 'label' => 'Employer\'s NI', 'category' => 'Staff Costs', 'keywords' => ['national insurance', 'ni', 'employer', 'nic', 'contributions']],
        '6020' => ['code' => '6020', 'label' => 'Employer\'s Pension Contributions', 'category' => 'Staff Costs', 'keywords' => ['pension', 'employer', 'auto enrolment', 'nest', 'workplace pension']],
        '6030' => ['code' => '6030', 'label' => 'Staff Training', 'category' => 'Staff Costs', 'keywords' => ['training', 'course', 'education', 'cpd', 'professional development']],
        '6040' => ['code' => '6040', 'label' => 'Staff Welfare & Benefits', 'category' => 'Staff Costs', 'keywords' => ['welfare', 'benefits', 'staff', 'employee', 'perks']],
        '6050' => ['code' => '6050', 'label' => 'Recruitment Costs', 'category' => 'Staff Costs', 'keywords' => ['recruitment', 'hiring', 'agency', 'job advert']],
        
        // Premises Costs
        '6100' => ['code' => '6100', 'label' => 'Rent', 'category' => 'Premises Costs', 'keywords' => ['rent', 'office rent', 'premises', 'lease']],
        '6110' => ['code' => '6110', 'label' => 'Business Rates', 'category' => 'Premises Costs', 'keywords' => ['rates', 'business rates', 'council', 'local authority']],
        '6120' => ['code' => '6120', 'label' => 'Water Rates', 'category' => 'Premises Costs', 'keywords' => ['water', 'rates', 'utility']],
        '6130' => ['code' => '6130', 'label' => 'Electricity', 'category' => 'Premises Costs', 'keywords' => ['electricity', 'electric', 'power', 'energy']],
        '6140' => ['code' => '6140', 'label' => 'Gas', 'category' => 'Premises Costs', 'keywords' => ['gas', 'heating', 'energy']],
        '6150' => ['code' => '6150', 'label' => 'Buildings Insurance', 'category' => 'Premises Costs', 'keywords' => ['buildings insurance', 'property insurance']],
        '6160' => ['code' => '6160', 'label' => 'Repairs & Maintenance', 'category' => 'Premises Costs', 'keywords' => ['repair', 'maintenance', 'fix', 'plumber', 'electrician', 'building work']],
        '6170' => ['code' => '6170', 'label' => 'Cleaning', 'category' => 'Premises Costs', 'keywords' => ['cleaning', 'cleaner', 'janitorial']],
        '6180' => ['code' => '6180', 'label' => 'Service Charges', 'category' => 'Premises Costs', 'keywords' => ['service charge', 'management', 'communal']],
        '6190' => ['code' => '6190', 'label' => 'Use of Home as Office', 'category' => 'Premises Costs', 'keywords' => ['home office', 'working from home', 'use of home', 'flat rate']],
        
        // General Admin
        '6200' => ['code' => '6200', 'label' => 'Office Supplies & Stationery', 'category' => 'General Admin', 'keywords' => ['stationery', 'office supplies', 'paper', 'ink', 'toner', 'pens']],
        '6210' => ['code' => '6210', 'label' => 'Postage & Packaging', 'category' => 'General Admin', 'keywords' => ['postage', 'stamps', 'packaging', 'royal mail', 'courier']],
        '6220' => ['code' => '6220', 'label' => 'Telephone & Internet', 'category' => 'General Admin', 'keywords' => ['telephone', 'phone', 'mobile', 'internet', 'broadband', 'wifi', 'data']],
        '6230' => ['code' => '6230', 'label' => 'Computer Software & Subscriptions', 'category' => 'General Admin', 'keywords' => ['software', 'subscription', 'saas', 'cloud', 'microsoft', 'adobe', 'xero', 'slack']],
        '6240' => ['code' => '6240', 'label' => 'Printing & Copying', 'category' => 'General Admin', 'keywords' => ['printing', 'copying', 'photocopying']],
        '6250' => ['code' => '6250', 'label' => 'Books & Publications', 'category' => 'General Admin', 'keywords' => ['books', 'publication', 'magazine', 'journal', 'reference']],
        
        // Professional Fees
        '6300' => ['code' => '6300', 'label' => 'Accountancy Fees', 'category' => 'Professional Fees', 'keywords' => ['accountant', 'accountancy', 'bookkeeper', 'audit', 'tax return']],
        '6310' => ['code' => '6310', 'label' => 'Legal Fees', 'category' => 'Professional Fees', 'keywords' => ['legal', 'solicitor', 'lawyer', 'barrister', 'contract']],
        '6320' => ['code' => '6320', 'label' => 'Consultancy Fees', 'category' => 'Professional Fees', 'keywords' => ['consultant', 'consultancy', 'advisory', 'professional']],
        '6330' => ['code' => '6330', 'label' => 'Professional Subscriptions', 'category' => 'Professional Fees', 'keywords' => ['subscription', 'membership', 'professional body', 'icaew', 'acca', 'cima']],
        '6340' => ['code' => '6340', 'label' => 'Companies House Filing Fees', 'category' => 'Professional Fees', 'keywords' => ['companies house', 'filing', 'annual return', 'confirmation statement']],
        
        // Marketing & Advertising
        '6400' => ['code' => '6400', 'label' => 'Advertising', 'category' => 'Marketing', 'keywords' => ['advertising', 'advert', 'ad', 'promotion', 'google ads', 'facebook ads']],
        '6410' => ['code' => '6410', 'label' => 'Marketing & PR', 'category' => 'Marketing', 'keywords' => ['marketing', 'pr', 'public relations', 'branding', 'campaign']],
        '6420' => ['code' => '6420', 'label' => 'Website Hosting & Maintenance', 'category' => 'Marketing', 'keywords' => ['hosting', 'website', 'domain', 'maintenance', 'web']],
        '6430' => ['code' => '6430', 'label' => 'Entertainment', 'category' => 'Marketing', 'keywords' => ['entertainment', 'client', 'hospitality', 'gifts']],
        
        // Travel & Subsistence
        '6500' => ['code' => '6500', 'label' => 'Travel - UK', 'category' => 'Travel', 'keywords' => ['travel', 'train', 'bus', 'tube', 'taxi', 'uber', 'transport', 'uk']],
        '6510' => ['code' => '6510', 'label' => 'Travel - Overseas', 'category' => 'Travel', 'keywords' => ['travel', 'overseas', 'international', 'flight', 'airline']],
        '6520' => ['code' => '6520', 'label' => 'Hotels & Accommodation', 'category' => 'Travel', 'keywords' => ['hotel', 'accommodation', 'airbnb', 'lodging', 'b&b']],
        '6530' => ['code' => '6530', 'label' => 'Subsistence / Meals', 'category' => 'Travel', 'keywords' => ['meals', 'food', 'subsistence', 'lunch', 'dinner', 'restaurant']],
        '6540' => ['code' => '6540', 'label' => 'Mileage / Fuel', 'category' => 'Travel', 'keywords' => ['mileage', 'fuel', 'petrol', 'diesel', 'ev', 'charging', '45p']],
        '6550' => ['code' => '6550', 'label' => 'Parking & Tolls', 'category' => 'Travel', 'keywords' => ['parking', 'toll', 'congestion charge', 'ulez']],
        
        // Vehicle Costs (business vehicles)
        '6600' => ['code' => '6600', 'label' => 'Vehicle Insurance', 'category' => 'Vehicle Costs', 'keywords' => ['vehicle insurance', 'car insurance', 'motor insurance']],
        '6610' => ['code' => '6610', 'label' => 'Vehicle Tax (Road Tax)', 'category' => 'Vehicle Costs', 'keywords' => ['road tax', 'vehicle tax', 'ved', 'dvla']],
        '6620' => ['code' => '6620', 'label' => 'Vehicle Repairs & Servicing', 'category' => 'Vehicle Costs', 'keywords' => ['vehicle repair', 'servicing', 'mot', 'garage', 'tyres']],
        '6630' => ['code' => '6630', 'label' => 'Vehicle Hire / Leasing', 'category' => 'Vehicle Costs', 'keywords' => ['vehicle hire', 'leasing', 'car lease', 'contract hire']],
        
        // Insurance
        '6700' => ['code' => '6700', 'label' => 'Business Insurance', 'category' => 'Insurance', 'keywords' => ['business insurance', 'public liability', 'professional indemnity', 'pi']],
        '6710' => ['code' => '6710', 'label' => 'Employers Liability Insurance', 'category' => 'Insurance', 'keywords' => ['employers liability', 'el', 'compulsory']],
        '6720' => ['code' => '6720', 'label' => 'Contents Insurance', 'category' => 'Insurance', 'keywords' => ['contents', 'equipment', 'insurance']],
        
        // Finance Charges
        '6800' => ['code' => '6800', 'label' => 'Bank Charges', 'category' => 'Finance Charges', 'keywords' => ['bank charge', 'bank fees', 'service charge', 'monthly fee']],
        '6810' => ['code' => '6810', 'label' => 'Bank Interest Paid', 'category' => 'Finance Charges', 'keywords' => ['interest', 'bank interest', 'loan interest', 'overdraft interest']],
        '6820' => ['code' => '6820', 'label' => 'Credit Card Charges', 'category' => 'Finance Charges', 'keywords' => ['credit card', 'interest', 'charges', 'fees']],
        '6830' => ['code' => '6830', 'label' => 'Payment Processing Fees', 'category' => 'Finance Charges', 'keywords' => ['stripe', 'paypal', 'processing', 'merchant', 'transaction fee']],
        '6840' => ['code' => '6840', 'label' => 'Foreign Exchange Losses', 'category' => 'Finance Charges', 'keywords' => ['foreign exchange', 'fx', 'currency', 'loss']],
        '6850' => ['code' => '6850', 'label' => 'Hire Purchase Interest', 'category' => 'Finance Charges', 'keywords' => ['hire purchase', 'hp', 'interest', 'finance charge']],
        
        // Depreciation & Amortisation
        '6900' => ['code' => '6900', 'label' => 'Depreciation', 'category' => 'Depreciation', 'keywords' => ['depreciation', 'write-off', 'capital allowance']],
        '6910' => ['code' => '6910', 'label' => 'Amortisation', 'category' => 'Depreciation', 'keywords' => ['amortisation', 'amortization', 'intangible']],
        '6920' => ['code' => '6920', 'label' => 'Loss on Disposal of Assets', 'category' => 'Depreciation', 'keywords' => ['loss', 'disposal', 'sale', 'asset', 'write-off']],
        
        // Other Expenses
        '6950' => ['code' => '6950', 'label' => 'Bad Debts Written Off', 'category' => 'Other Expenses', 'keywords' => ['bad debt', 'write-off', 'irrecoverable']],
        '6960' => ['code' => '6960', 'label' => 'Donations & Charity', 'category' => 'Other Expenses', 'keywords' => ['donation', 'charity', 'gift aid']],
        '6970' => ['code' => '6970', 'label' => 'Fines & Penalties', 'category' => 'Other Expenses', 'keywords' => ['fine', 'penalty', 'hmrc', 'late payment', 'surcharge']],
        '6980' => ['code' => '6980', 'label' => 'Sundry Expenses', 'category' => 'Other Expenses', 'keywords' => ['sundry', 'miscellaneous', 'other', 'general']],
        
        // ═══════════════════════════════════════════════════════════════════
        // 7000-7999 : OVERHEADS (CONTINUED) - Extended range
        // ═══════════════════════════════════════════════════════════════════
        
        '7000' => ['code' => '7000', 'label' => 'Personal Pension (Sole Trader)', 'category' => 'Sole Trader Expenses', 'keywords' => ['personal pension', 'sole trader', 'sipp', 'retirement']],
        '7010' => ['code' => '7010', 'label' => 'National Insurance (Self-Employed)', 'category' => 'Sole Trader Expenses', 'keywords' => ['class 2', 'class 4', 'national insurance', 'self employed']],
        '7020' => ['code' => '7020', 'label' => 'Personal Tax (Sole Trader)', 'category' => 'Sole Trader Expenses', 'keywords' => ['income tax', 'self assessment', 'personal tax']],
        
        // ═══════════════════════════════════════════════════════════════════
        // 8000-9999 : TAX & PROVISIONS
        // ═══════════════════════════════════════════════════════════════════
        
        '8000' => ['code' => '8000', 'label' => 'Corporation Tax', 'category' => 'Taxation', 'keywords' => ['corporation tax', 'ct', 'company tax']],
        '8010' => ['code' => '8010', 'label' => 'Deferred Tax', 'category' => 'Taxation', 'keywords' => ['deferred tax', 'timing difference']],
        '8100' => ['code' => '8100', 'label' => 'Provisions', 'category' => 'Provisions', 'keywords' => ['provision', 'contingency', 'liability']],
        
        '9998' => ['code' => '9998', 'label' => 'Suspense Account', 'category' => 'Control Accounts', 'keywords' => ['suspense', 'unallocated', 'unknown', 'clearing']],
        '9999' => ['code' => '9999', 'label' => 'Mispostings Account', 'category' => 'Control Accounts', 'keywords' => ['misposting', 'error', 'correction', 'adjustment']],
    ];

    /**
     * Get accounts by nominal code range
     */
    public static function getAccountsByRange($start, $end)
    {
        $accounts = [];
        foreach (self::$accounts as $code => $account) {
            $numCode = intval($code);
            if ($numCode >= $start && $numCode <= $end) {
                $accounts[] = $account;
            }
        }
        return $accounts;
    }

    /**
     * Get accounts by class (for backward compatibility)
     * Maps UK ranges to class-like groupings
     */
    public static function getAccountsByClass($class)
    {
        $ranges = [
            0 => [0, 999],       // Fixed Assets
            1 => [1000, 1999],   // Current Assets
            2 => [2000, 2999],   // Liabilities
            3 => [3000, 3999],   // Capital & Reserves
            4 => [4000, 4999],   // Sales / Revenue
            5 => [5000, 5999],   // Cost of Sales
            6 => [6000, 6999],   // Overheads
            7 => [7000, 7999],   // Overheads (continued)
            8 => [8000, 9999],   // Tax & Provisions
        ];
        
        if (!isset($ranges[$class])) {
            return [];
        }
        
        return self::getAccountsByRange($ranges[$class][0], $ranges[$class][1]);
    }

    /**
     * Get VAT accounts
     */
    public static function getVATAccounts()
    {
        $vatAccounts = [];
        foreach (self::$accounts as $code => $account) {
            if (in_array($code, ['1300', '2200', '2201', '2202', '2203', '2204'])) {
                $vatAccounts[] = $account;
            }
        }
        return $vatAccounts;
    }

    /**
     * Get treasury / bank & cash accounts
     */
    public static function getTreasuryAccounts()
    {
        return self::getAccountsByRange(1200, 1260);
    }

    /**
     * Get an account by its code
     */
    public static function getAccount($code)
    {
        return isset(self::$accounts[$code]) ? self::$accounts[$code] : null;
    }

    /**
     * Get all accounts
     */
    public static function getAllAccounts()
    {
        return self::$accounts;
    }

    /**
     * Get unique categories
     */
    public static function getCategories()
    {
        $categories = [];
        foreach (self::$accounts as $account) {
            $categories[] = $account['category'];
        }
        return array_unique($categories);
    }

    /**
     * Get expense accounts (5000-7999)
     */
    public static function getExpenseAccounts()
    {
        return self::getAccountsByRange(5000, 7999);
    }

    /**
     * Get revenue accounts (4000-4999)
     */
    public static function getRevenueAccounts()
    {
        return self::getAccountsByRange(4000, 4999);
    }

}
