import { FMResult } from '@/lib/types'

export interface AnnualPLTableProps {
  results: FMResult[]
  lang: 'de' | 'en'
}

export type AccountingStandard = 'hgb' | 'usgaap'

export interface AnnualRow {
  year: number
  revenue: number
  cogs: number
  grossProfit: number
  grossMarginPct: number
  personnel: number
  marketing: number
  infra: number
  totalOpex: number
  ebitda: number
  ebitdaMarginPct: number
  customers: number
  employees: number
}

export interface MonthlyRow {
  month: number
  monthInYear: number
  revenue: number
  cogs: number
  grossProfit: number
  grossMarginPct: number
  personnel: number
  marketing: number
  infra: number
  totalCosts: number
  ebitda: number
  ebitdaMarginPct: number
  customers: number
  employees: number
  mrr: number
  cashBalance: number
}

export interface LineItem {
  label: string
  key: keyof AnnualRow
  monthKey: keyof MonthlyRow
  isBold?: boolean
  isNegative?: boolean
  isSeparator?: boolean
  isPercent?: boolean
}

export function fmt(v: number): string {
  if (Math.abs(v) >= 1_000_000) return `${(v / 1_000_000).toFixed(1)}M`
  if (Math.abs(v) >= 1_000) return `${(v / 1_000).toFixed(0)}k`
  return Math.round(v).toLocaleString('de-DE')
}

export function fmtMonth(v: number): string {
  return Math.round(v).toLocaleString('de-DE')
}

export const MONTH_NAMES_DE = ['Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez']
export const MONTH_NAMES_EN = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

export function getLineItems(lang: 'de' | 'en', standard: AccountingStandard): LineItem[] {
  const de = lang === 'de'
  const hgb = standard === 'hgb'

  return [
    {
      label: hgb
        ? (de ? 'Umsatzerloese' : 'Revenue (Umsatzerloese)')
        : (de ? 'Revenue' : 'Revenue'),
      key: 'revenue' as keyof AnnualRow,
      monthKey: 'revenue' as keyof MonthlyRow,
      isBold: true,
    },
    {
      label: hgb
        ? (de ? '- Herstellungskosten' : '- Cost of Production')
        : (de ? '- COGS' : '- Cost of Goods Sold'),
      key: 'cogs' as keyof AnnualRow,
      monthKey: 'cogs' as keyof MonthlyRow,
      isNegative: true,
    },
    {
      label: hgb
        ? (de ? '= Rohertrag' : '= Gross Profit')
        : (de ? '= Gross Profit' : '= Gross Profit'),
      key: 'grossProfit' as keyof AnnualRow,
      monthKey: 'grossProfit' as keyof MonthlyRow,
      isBold: true,
      isSeparator: true,
    },
    {
      label: hgb
        ? (de ? '  Rohertragsmarge' : '  Gross Margin')
        : (de ? '  Gross Margin' : '  Gross Margin'),
      key: 'grossMarginPct' as keyof AnnualRow,
      monthKey: 'grossMarginPct' as keyof MonthlyRow,
      isPercent: true,
    },
    {
      label: hgb
        ? (de ? '- Personalaufwand' : '- Personnel Expenses')
        : (de ? '- Personnel' : '- Personnel'),
      key: 'personnel' as keyof AnnualRow,
      monthKey: 'personnel' as keyof MonthlyRow,
      isNegative: true,
    },
    {
      label: hgb
        ? (de ? '- Vertrieb & Marketing' : '- Sales & Marketing')
        : (de ? '- Sales & Marketing' : '- Sales & Marketing'),
      key: 'marketing' as keyof AnnualRow,
      monthKey: 'marketing' as keyof MonthlyRow,
      isNegative: true,
    },
    {
      label: hgb
        ? (de ? '- sonstige betriebl. Aufwendungen' : '- Other Operating Expenses')
        : (de ? '- Infrastructure' : '- Infrastructure'),
      key: 'infra' as keyof AnnualRow,
      monthKey: 'infra' as keyof MonthlyRow,
      isNegative: true,
    },
    {
      label: hgb
        ? (de ? '= Betriebsaufwand gesamt' : '= Total Operating Expenses')
        : (de ? '= Total OpEx' : '= Total OpEx'),
      key: 'totalOpex' as keyof AnnualRow,
      monthKey: 'totalCosts' as keyof MonthlyRow,
      isBold: true,
      isSeparator: true,
      isNegative: true,
    },
    {
      label: hgb
        ? (de ? 'Betriebsergebnis (EBITDA)' : 'Operating Result (EBITDA)')
        : 'EBITDA',
      key: 'ebitda' as keyof AnnualRow,
      monthKey: 'ebitda' as keyof MonthlyRow,
      isBold: true,
      isSeparator: true,
    },
    {
      label: hgb
        ? (de ? '  EBITDA-Marge' : '  EBITDA Margin')
        : (de ? '  EBITDA Margin' : '  EBITDA Margin'),
      key: 'ebitdaMarginPct' as keyof AnnualRow,
      monthKey: 'ebitdaMarginPct' as keyof MonthlyRow,
      isPercent: true,
    },
    {
      label: hgb
        ? (de ? 'Kunden (Stichtag)' : 'Customers (Reporting Date)')
        : (de ? 'Kunden (Jahresende)' : 'Customers (Year End)'),
      key: 'customers' as keyof AnnualRow,
      monthKey: 'customers' as keyof MonthlyRow,
    },
    {
      label: hgb
        ? (de ? 'Mitarbeiter (VZAe)' : 'Employees (FTE)')
        : (de ? 'Mitarbeiter' : 'Employees'),
      key: 'employees' as keyof AnnualRow,
      monthKey: 'employees' as keyof MonthlyRow,
    },
  ]
}
