export interface TemplateAccount {
  code: string;
  name_ar: string;
  name_en: string;
  classification: 'assets' | 'liabilities' | 'equity' | 'revenue' | 'expenses';
  nature: 'debit' | 'credit';
  account_type: 'header' | 'detail';
  children?: TemplateAccount[];
}

export interface AccountTemplate {
  id: string;
  name_ar: string;
  name_en: string;
  icon: string;
  description_ar: string;
  description_en: string;
  accounts: TemplateAccount[];
}

// ─── Shared base structure ───

const ASSETS_CURRENT_BASE: TemplateAccount[] = [
  { code: '1101', name_ar: 'النقد في الصندوق', name_en: 'Cash on Hand', classification: 'assets', nature: 'debit', account_type: 'detail' },
  { code: '1102', name_ar: 'النقد في البنك', name_en: 'Cash in Bank', classification: 'assets', nature: 'debit', account_type: 'detail' },
  { code: '1103', name_ar: 'الذمم المدينة', name_en: 'Accounts Receivable', classification: 'assets', nature: 'debit', account_type: 'detail' },
  { code: '1104', name_ar: 'أوراق القبض', name_en: 'Notes Receivable', classification: 'assets', nature: 'debit', account_type: 'detail' },
  { code: '1106', name_ar: 'مصروفات مدفوعة مقدماً', name_en: 'Prepaid Expenses', classification: 'assets', nature: 'debit', account_type: 'detail' },
  { code: '1107', name_ar: 'ضريبة القيمة المضافة المدفوعة', name_en: 'Input VAT', classification: 'assets', nature: 'debit', account_type: 'detail' },
];

const ASSETS_NONCURRENT_BASE: TemplateAccount[] = [
  { code: '1201', name_ar: 'الأراضي', name_en: 'Land', classification: 'assets', nature: 'debit', account_type: 'detail' },
  { code: '1202', name_ar: 'المباني', name_en: 'Buildings', classification: 'assets', nature: 'debit', account_type: 'detail' },
  { code: '1203', name_ar: 'الآلات والمعدات', name_en: 'Machinery & Equipment', classification: 'assets', nature: 'debit', account_type: 'detail' },
  { code: '1204', name_ar: 'الأثاث والتجهيزات', name_en: 'Furniture & Fixtures', classification: 'assets', nature: 'debit', account_type: 'detail' },
  { code: '1205', name_ar: 'وسائل النقل', name_en: 'Vehicles', classification: 'assets', nature: 'debit', account_type: 'detail' },
  { code: '1206', name_ar: 'أجهزة الحاسب', name_en: 'Computer Equipment', classification: 'assets', nature: 'debit', account_type: 'detail' },
  { code: '1207', name_ar: 'مجمع الإهلاك', name_en: 'Accumulated Depreciation', classification: 'assets', nature: 'credit', account_type: 'detail' },
];

const LIABILITIES_CURRENT: TemplateAccount[] = [
  { code: '2101', name_ar: 'الذمم الدائنة', name_en: 'Accounts Payable', classification: 'liabilities', nature: 'credit', account_type: 'detail' },
  { code: '2102', name_ar: 'أوراق الدفع', name_en: 'Notes Payable', classification: 'liabilities', nature: 'credit', account_type: 'detail' },
  { code: '2103', name_ar: 'القروض قصيرة الأجل', name_en: 'Short-term Loans', classification: 'liabilities', nature: 'credit', account_type: 'detail' },
  { code: '2104', name_ar: 'مصروفات مستحقة', name_en: 'Accrued Expenses', classification: 'liabilities', nature: 'credit', account_type: 'detail' },
  { code: '2105', name_ar: 'إيرادات مقبوضة مقدماً', name_en: 'Unearned Revenue', classification: 'liabilities', nature: 'credit', account_type: 'detail' },
  { code: '2106', name_ar: 'ضريبة القيمة المضافة المحصلة', name_en: 'Output VAT', classification: 'liabilities', nature: 'credit', account_type: 'detail' },
  { code: '2107', name_ar: 'ضريبة القيمة المضافة المستحقة', name_en: 'VAT Payable', classification: 'liabilities', nature: 'credit', account_type: 'detail' },
];

const LIABILITIES_LONGTERM: TemplateAccount[] = [
  { code: '2201', name_ar: 'القروض طويلة الأجل', name_en: 'Long-term Loans', classification: 'liabilities', nature: 'credit', account_type: 'detail' },
  { code: '2202', name_ar: 'التزامات أخرى طويلة الأجل', name_en: 'Other Long-term Obligations', classification: 'liabilities', nature: 'credit', account_type: 'detail' },
];

const EQUITY_BASE: TemplateAccount[] = [
  { code: '31', name_ar: 'رأس المال', name_en: 'Capital', classification: 'equity', nature: 'credit', account_type: 'detail' },
  { code: '32', name_ar: 'الاحتياطيات', name_en: 'Reserves', classification: 'equity', nature: 'credit', account_type: 'detail' },
  { code: '33', name_ar: 'الأرباح المحتجزة', name_en: 'Retained Earnings', classification: 'equity', nature: 'credit', account_type: 'detail' },
  { code: '34', name_ar: 'أرباح / خسائر العام', name_en: 'Current Year P&L', classification: 'equity', nature: 'credit', account_type: 'detail' },
];

const GA_EXPENSES: TemplateAccount[] = [
  { code: '5201', name_ar: 'الرواتب والأجور', name_en: 'Salaries & Wages', classification: 'expenses', nature: 'debit', account_type: 'detail' },
  { code: '5202', name_ar: 'الإيجارات', name_en: 'Rent Expense', classification: 'expenses', nature: 'debit', account_type: 'detail' },
  { code: '5203', name_ar: 'الكهرباء والماء', name_en: 'Utilities', classification: 'expenses', nature: 'debit', account_type: 'detail' },
  { code: '5204', name_ar: 'الاتصالات والإنترنت', name_en: 'Communications', classification: 'expenses', nature: 'debit', account_type: 'detail' },
  { code: '5205', name_ar: 'اللوازم المكتبية', name_en: 'Office Supplies', classification: 'expenses', nature: 'debit', account_type: 'detail' },
  { code: '5206', name_ar: 'مصاريف الصيانة', name_en: 'Maintenance', classification: 'expenses', nature: 'debit', account_type: 'detail' },
  { code: '5207', name_ar: 'الإهلاك', name_en: 'Depreciation', classification: 'expenses', nature: 'debit', account_type: 'detail' },
  { code: '5208', name_ar: 'التأمين', name_en: 'Insurance', classification: 'expenses', nature: 'debit', account_type: 'detail' },
  { code: '5209', name_ar: 'مصاريف النقل', name_en: 'Transportation', classification: 'expenses', nature: 'debit', account_type: 'detail' },
  { code: '5210', name_ar: 'مصاريف الضيافة', name_en: 'Hospitality', classification: 'expenses', nature: 'debit', account_type: 'detail' },
  { code: '5211', name_ar: 'الرسوم والاشتراكات', name_en: 'Fees & Subscriptions', classification: 'expenses', nature: 'debit', account_type: 'detail' },
];

const SELLING_EXPENSES: TemplateAccount[] = [
  { code: '5301', name_ar: 'مصاريف الإعلان والتسويق', name_en: 'Advertising & Marketing', classification: 'expenses', nature: 'debit', account_type: 'detail' },
  { code: '5302', name_ar: 'عمولات المبيعات', name_en: 'Sales Commissions', classification: 'expenses', nature: 'debit', account_type: 'detail' },
];

const OTHER_EXPENSES: TemplateAccount[] = [
  { code: '5401', name_ar: 'مصاريف بنكية', name_en: 'Bank Charges', classification: 'expenses', nature: 'debit', account_type: 'detail' },
  { code: '5402', name_ar: 'مصاريف التمويل', name_en: 'Finance Costs', classification: 'expenses', nature: 'debit', account_type: 'detail' },
  { code: '5403', name_ar: 'خسائر متنوعة', name_en: 'Miscellaneous Losses', classification: 'expenses', nature: 'debit', account_type: 'detail' },
];

// ──────────────── COMMERCIAL TEMPLATE ────────────────

const COMMERCIAL_TEMPLATE: TemplateAccount[] = [
  {
    code: '1', name_ar: 'الأصول', name_en: 'Assets', classification: 'assets', nature: 'debit', account_type: 'header',
    children: [
      {
        code: '11', name_ar: 'الأصول المتداولة', name_en: 'Current Assets', classification: 'assets', nature: 'debit', account_type: 'header',
        children: [
          ...ASSETS_CURRENT_BASE,
          { code: '1105', name_ar: 'المخزون', name_en: 'Inventory', classification: 'assets', nature: 'debit', account_type: 'detail' },
        ],
      },
      {
        code: '12', name_ar: 'الأصول غير المتداولة', name_en: 'Non-Current Assets', classification: 'assets', nature: 'debit', account_type: 'header',
        children: ASSETS_NONCURRENT_BASE,
      },
    ],
  },
  {
    code: '2', name_ar: 'الخصوم', name_en: 'Liabilities', classification: 'liabilities', nature: 'credit', account_type: 'header',
    children: [
      { code: '21', name_ar: 'الخصوم المتداولة', name_en: 'Current Liabilities', classification: 'liabilities', nature: 'credit', account_type: 'header', children: LIABILITIES_CURRENT },
      { code: '22', name_ar: 'الخصوم طويلة الأجل', name_en: 'Long-term Liabilities', classification: 'liabilities', nature: 'credit', account_type: 'header', children: LIABILITIES_LONGTERM },
    ],
  },
  {
    code: '3', name_ar: 'حقوق الملكية', name_en: 'Equity', classification: 'equity', nature: 'credit', account_type: 'header',
    children: EQUITY_BASE,
  },
  {
    code: '4', name_ar: 'الإيرادات', name_en: 'Revenue', classification: 'revenue', nature: 'credit', account_type: 'header',
    children: [
      { code: '41', name_ar: 'إيرادات المبيعات', name_en: 'Sales Revenue', classification: 'revenue', nature: 'credit', account_type: 'detail' },
      { code: '42', name_ar: 'خصم مبيعات', name_en: 'Sales Discounts', classification: 'revenue', nature: 'debit', account_type: 'detail' },
      { code: '43', name_ar: 'مردودات المبيعات', name_en: 'Sales Returns', classification: 'revenue', nature: 'debit', account_type: 'detail' },
      { code: '44', name_ar: 'إيرادات أخرى', name_en: 'Other Revenue', classification: 'revenue', nature: 'credit', account_type: 'detail' },
    ],
  },
  {
    code: '5', name_ar: 'المصروفات', name_en: 'Expenses', classification: 'expenses', nature: 'debit', account_type: 'header',
    children: [
      {
        code: '51', name_ar: 'تكلفة المبيعات', name_en: 'Cost of Goods Sold', classification: 'expenses', nature: 'debit', account_type: 'header',
        children: [
          { code: '5101', name_ar: 'تكلفة البضاعة المباعة', name_en: 'COGS', classification: 'expenses', nature: 'debit', account_type: 'detail' },
          { code: '5102', name_ar: 'خصم مشتريات', name_en: 'Purchase Discounts', classification: 'expenses', nature: 'credit', account_type: 'detail' },
          { code: '5103', name_ar: 'مردودات مشتريات', name_en: 'Purchase Returns', classification: 'expenses', nature: 'credit', account_type: 'detail' },
        ],
      },
      { code: '52', name_ar: 'المصروفات الإدارية والعمومية', name_en: 'G&A Expenses', classification: 'expenses', nature: 'debit', account_type: 'header', children: GA_EXPENSES },
      { code: '53', name_ar: 'مصاريف البيع والتسويق', name_en: 'Selling & Marketing', classification: 'expenses', nature: 'debit', account_type: 'header', children: SELLING_EXPENSES },
      { code: '54', name_ar: 'المصروفات الأخرى', name_en: 'Other Expenses', classification: 'expenses', nature: 'debit', account_type: 'header', children: OTHER_EXPENSES },
    ],
  },
];

// ──────────────── MANUFACTURING TEMPLATE ────────────────

const MANUFACTURING_TEMPLATE: TemplateAccount[] = [
  {
    code: '1', name_ar: 'الأصول', name_en: 'Assets', classification: 'assets', nature: 'debit', account_type: 'header',
    children: [
      {
        code: '11', name_ar: 'الأصول المتداولة', name_en: 'Current Assets', classification: 'assets', nature: 'debit', account_type: 'header',
        children: [
          ...ASSETS_CURRENT_BASE,
          { code: '1105', name_ar: 'مواد أولية', name_en: 'Raw Materials', classification: 'assets', nature: 'debit', account_type: 'detail' },
          { code: '1108', name_ar: 'إنتاج تحت التشغيل', name_en: 'Work in Progress', classification: 'assets', nature: 'debit', account_type: 'detail' },
          { code: '1109', name_ar: 'منتجات تامة الصنع', name_en: 'Finished Goods', classification: 'assets', nature: 'debit', account_type: 'detail' },
          { code: '1110', name_ar: 'قطع غيار ومستلزمات', name_en: 'Spare Parts & Supplies', classification: 'assets', nature: 'debit', account_type: 'detail' },
        ],
      },
      {
        code: '12', name_ar: 'الأصول غير المتداولة', name_en: 'Non-Current Assets', classification: 'assets', nature: 'debit', account_type: 'header',
        children: [
          ...ASSETS_NONCURRENT_BASE,
          { code: '1208', name_ar: 'معدات المصنع', name_en: 'Factory Equipment', classification: 'assets', nature: 'debit', account_type: 'detail' },
        ],
      },
    ],
  },
  {
    code: '2', name_ar: 'الخصوم', name_en: 'Liabilities', classification: 'liabilities', nature: 'credit', account_type: 'header',
    children: [
      { code: '21', name_ar: 'الخصوم المتداولة', name_en: 'Current Liabilities', classification: 'liabilities', nature: 'credit', account_type: 'header', children: LIABILITIES_CURRENT },
      { code: '22', name_ar: 'الخصوم طويلة الأجل', name_en: 'Long-term Liabilities', classification: 'liabilities', nature: 'credit', account_type: 'header', children: LIABILITIES_LONGTERM },
    ],
  },
  {
    code: '3', name_ar: 'حقوق الملكية', name_en: 'Equity', classification: 'equity', nature: 'credit', account_type: 'header',
    children: EQUITY_BASE,
  },
  {
    code: '4', name_ar: 'الإيرادات', name_en: 'Revenue', classification: 'revenue', nature: 'credit', account_type: 'header',
    children: [
      { code: '41', name_ar: 'إيرادات المبيعات', name_en: 'Sales Revenue', classification: 'revenue', nature: 'credit', account_type: 'detail' },
      { code: '42', name_ar: 'خصم مبيعات', name_en: 'Sales Discounts', classification: 'revenue', nature: 'debit', account_type: 'detail' },
      { code: '43', name_ar: 'مردودات المبيعات', name_en: 'Sales Returns', classification: 'revenue', nature: 'debit', account_type: 'detail' },
      { code: '44', name_ar: 'إيرادات صناعية أخرى', name_en: 'Other Industrial Revenue', classification: 'revenue', nature: 'credit', account_type: 'detail' },
    ],
  },
  {
    code: '5', name_ar: 'المصروفات', name_en: 'Expenses', classification: 'expenses', nature: 'debit', account_type: 'header',
    children: [
      {
        code: '51', name_ar: 'تكلفة الإنتاج', name_en: 'Production Costs', classification: 'expenses', nature: 'debit', account_type: 'header',
        children: [
          { code: '5101', name_ar: 'المواد الأولية المستخدمة', name_en: 'Raw Materials Used', classification: 'expenses', nature: 'debit', account_type: 'detail' },
          { code: '5102', name_ar: 'أجور الإنتاج المباشرة', name_en: 'Direct Production Labor', classification: 'expenses', nature: 'debit', account_type: 'detail' },
          { code: '5103', name_ar: 'مصاريف التصنيع غير المباشرة', name_en: 'Manufacturing Overhead', classification: 'expenses', nature: 'debit', account_type: 'detail' },
          { code: '5104', name_ar: 'صيانة المصنع', name_en: 'Factory Maintenance', classification: 'expenses', nature: 'debit', account_type: 'detail' },
          { code: '5105', name_ar: 'كهرباء ووقود المصنع', name_en: 'Factory Utilities & Fuel', classification: 'expenses', nature: 'debit', account_type: 'detail' },
          { code: '5106', name_ar: 'إهلاك معدات المصنع', name_en: 'Factory Equipment Depreciation', classification: 'expenses', nature: 'debit', account_type: 'detail' },
        ],
      },
      { code: '52', name_ar: 'المصروفات الإدارية والعمومية', name_en: 'G&A Expenses', classification: 'expenses', nature: 'debit', account_type: 'header', children: GA_EXPENSES },
      { code: '53', name_ar: 'مصاريف البيع والتسويق', name_en: 'Selling & Marketing', classification: 'expenses', nature: 'debit', account_type: 'header', children: SELLING_EXPENSES },
      { code: '54', name_ar: 'المصروفات الأخرى', name_en: 'Other Expenses', classification: 'expenses', nature: 'debit', account_type: 'header', children: OTHER_EXPENSES },
    ],
  },
];

// ──────────────── SERVICES TEMPLATE ────────────────

const SERVICES_TEMPLATE: TemplateAccount[] = [
  {
    code: '1', name_ar: 'الأصول', name_en: 'Assets', classification: 'assets', nature: 'debit', account_type: 'header',
    children: [
      {
        code: '11', name_ar: 'الأصول المتداولة', name_en: 'Current Assets', classification: 'assets', nature: 'debit', account_type: 'header',
        children: [
          ...ASSETS_CURRENT_BASE,
          { code: '1105', name_ar: 'عهد ومقدمات', name_en: 'Advances & Deposits', classification: 'assets', nature: 'debit', account_type: 'detail' },
        ],
      },
      {
        code: '12', name_ar: 'الأصول غير المتداولة', name_en: 'Non-Current Assets', classification: 'assets', nature: 'debit', account_type: 'header',
        children: ASSETS_NONCURRENT_BASE,
      },
    ],
  },
  {
    code: '2', name_ar: 'الخصوم', name_en: 'Liabilities', classification: 'liabilities', nature: 'credit', account_type: 'header',
    children: [
      { code: '21', name_ar: 'الخصوم المتداولة', name_en: 'Current Liabilities', classification: 'liabilities', nature: 'credit', account_type: 'header', children: LIABILITIES_CURRENT },
      { code: '22', name_ar: 'الخصوم طويلة الأجل', name_en: 'Long-term Liabilities', classification: 'liabilities', nature: 'credit', account_type: 'header', children: LIABILITIES_LONGTERM },
    ],
  },
  {
    code: '3', name_ar: 'حقوق الملكية', name_en: 'Equity', classification: 'equity', nature: 'credit', account_type: 'header',
    children: EQUITY_BASE,
  },
  {
    code: '4', name_ar: 'الإيرادات', name_en: 'Revenue', classification: 'revenue', nature: 'credit', account_type: 'header',
    children: [
      { code: '41', name_ar: 'إيرادات الخدمات', name_en: 'Service Revenue', classification: 'revenue', nature: 'credit', account_type: 'detail' },
      { code: '42', name_ar: 'إيرادات الاستشارات', name_en: 'Consulting Revenue', classification: 'revenue', nature: 'credit', account_type: 'detail' },
      { code: '43', name_ar: 'إيرادات العقود', name_en: 'Contract Revenue', classification: 'revenue', nature: 'credit', account_type: 'detail' },
      { code: '44', name_ar: 'إيرادات أخرى', name_en: 'Other Revenue', classification: 'revenue', nature: 'credit', account_type: 'detail' },
    ],
  },
  {
    code: '5', name_ar: 'المصروفات', name_en: 'Expenses', classification: 'expenses', nature: 'debit', account_type: 'header',
    children: [
      {
        code: '51', name_ar: 'تكلفة الخدمات', name_en: 'Cost of Services', classification: 'expenses', nature: 'debit', account_type: 'header',
        children: [
          { code: '5101', name_ar: 'أجور مباشرة للمشاريع', name_en: 'Direct Project Labor', classification: 'expenses', nature: 'debit', account_type: 'detail' },
          { code: '5102', name_ar: 'مصاريف مشاريع مباشرة', name_en: 'Direct Project Costs', classification: 'expenses', nature: 'debit', account_type: 'detail' },
          { code: '5103', name_ar: 'مقاولين من الباطن', name_en: 'Subcontractors', classification: 'expenses', nature: 'debit', account_type: 'detail' },
        ],
      },
      { code: '52', name_ar: 'المصروفات الإدارية والعمومية', name_en: 'G&A Expenses', classification: 'expenses', nature: 'debit', account_type: 'header', children: GA_EXPENSES },
      { code: '53', name_ar: 'مصاريف البيع والتسويق', name_en: 'Selling & Marketing', classification: 'expenses', nature: 'debit', account_type: 'header', children: SELLING_EXPENSES },
      { code: '54', name_ar: 'المصروفات الأخرى', name_en: 'Other Expenses', classification: 'expenses', nature: 'debit', account_type: 'header', children: OTHER_EXPENSES },
    ],
  },
];

// ──────────────── EMPTY TEMPLATE ────────────────

const EMPTY_TEMPLATE: TemplateAccount[] = [
  { code: '1', name_ar: 'الأصول', name_en: 'Assets', classification: 'assets', nature: 'debit', account_type: 'header', children: [] },
  { code: '2', name_ar: 'الخصوم', name_en: 'Liabilities', classification: 'liabilities', nature: 'credit', account_type: 'header', children: [] },
  { code: '3', name_ar: 'حقوق الملكية', name_en: 'Equity', classification: 'equity', nature: 'credit', account_type: 'header', children: [] },
  { code: '4', name_ar: 'الإيرادات', name_en: 'Revenue', classification: 'revenue', nature: 'credit', account_type: 'header', children: [] },
  { code: '5', name_ar: 'المصروفات', name_en: 'Expenses', classification: 'expenses', nature: 'debit', account_type: 'header', children: [] },
];

// ──────────────── EXPORTED TEMPLATES ────────────────

export const ACCOUNT_TEMPLATES: AccountTemplate[] = [
  {
    id: 'commercial',
    name_ar: 'تجاري',
    name_en: 'Commercial',
    icon: 'pi pi-shopping-cart',
    description_ar: 'شجرة حسابات شاملة للأنشطة التجارية والتجزئة مع حسابات المخزون والمبيعات والمشتريات',
    description_en: 'Complete chart of accounts for commercial and retail activities with inventory, sales and purchasing',
    accounts: COMMERCIAL_TEMPLATE,
  },
  {
    id: 'manufacturing',
    name_ar: 'صناعي',
    name_en: 'Manufacturing',
    icon: 'pi pi-cog',
    description_ar: 'شجرة حسابات للأنشطة الصناعية مع حسابات المواد الأولية والإنتاج والتصنيع',
    description_en: 'Chart of accounts for manufacturing activities with raw materials, production and factory costs',
    accounts: MANUFACTURING_TEMPLATE,
  },
  {
    id: 'services',
    name_ar: 'خدمي',
    name_en: 'Services',
    icon: 'pi pi-briefcase',
    description_ar: 'شجرة حسابات للأنشطة الخدمية والاستشارية بدون مخزون',
    description_en: 'Chart of accounts for service and consulting activities without inventory',
    accounts: SERVICES_TEMPLATE,
  },
  {
    id: 'empty',
    name_ar: 'فارغ',
    name_en: 'Empty',
    icon: 'pi pi-file',
    description_ar: 'شجرة حسابات فارغة تحتوي فقط على التصنيفات الرئيسية الخمسة',
    description_en: 'Empty chart with only the 5 main classification headers',
    accounts: EMPTY_TEMPLATE,
  },
];
