/**
 * Finanzplan Types — mirrors the fp_* database tables.
 * Monthly values stored as Record<string, number> with keys m1..m60.
 * Annual values stored with keys y2026..y2030.
 */

export type MonthlyValues = Record<string, number> // m1..m60
export type AnnualValues = Record<string, number>  // y2026..y2030

export const MONTHS = 60
export const START_YEAR = 2026
export const END_YEAR = 2030
export const FOUNDING_MONTH = 8 // August 2026 = m8

// Month index (1-based) to year/month
export function monthToDate(m: number): { year: number; month: number } {
  const year = START_YEAR + Math.floor((m - 1) / 12)
  const month = ((m - 1) % 12) + 1
  return { year, month }
}

// Year/month to month index (1-based)
export function dateToMonth(year: number, month: number): number {
  return (year - START_YEAR) * 12 + month
}

export function emptyMonthly(): MonthlyValues {
  const v: MonthlyValues = {}
  for (let m = 1; m <= MONTHS; m++) v[`m${m}`] = 0
  return v
}

export function sumMonthly(values: MonthlyValues, fromM: number, toM: number): number {
  let s = 0
  for (let m = fromM; m <= toM; m++) s += values[`m${m}`] || 0
  return s
}

export function annualSums(values: MonthlyValues): AnnualValues {
  const r: AnnualValues = {}
  for (let y = START_YEAR; y <= END_YEAR; y++) {
    const startM = dateToMonth(y, 1)
    const endM = dateToMonth(y, 12)
    r[`y${y}`] = sumMonthly(values, startM, endM)
  }
  return r
}

// --- DB row types ---

export interface FPScenario {
  id: string
  name: string
  description?: string
  is_default: boolean
}

export interface FPPersonalkosten {
  id: number
  scenario_id: string
  person_name: string
  person_nr?: string
  position?: string
  start_date?: string
  end_date?: string
  brutto_monthly: number
  annual_raise_pct: number
  ag_sozial_pct: number
  values_brutto: MonthlyValues
  values_sozial: MonthlyValues
  values_total: MonthlyValues
  sort_order: number
}

export interface FPBetrieblicheAufwendungen {
  id: number
  scenario_id: string
  category: string
  row_label: string
  row_index: number
  is_editable: boolean
  is_sum_row: boolean
  formula_desc?: string
  values: MonthlyValues
  sort_order: number
}

export interface FPInvestitionen {
  id: number
  scenario_id: string
  item_name: string
  category?: string
  purchase_amount: number
  purchase_date?: string
  afa_years?: number
  afa_end_date?: string
  values_invest: MonthlyValues
  values_afa: MonthlyValues
  sort_order: number
}

export interface FPLiquiditaet {
  id: number
  scenario_id: string
  row_label: string
  row_type: string
  is_editable: boolean
  formula_desc?: string
  values: MonthlyValues
  sort_order: number
}

export interface FPGuV {
  id: number
  scenario_id: string
  row_label: string
  row_index: number
  is_sum_row: boolean
  formula_desc?: string
  values: AnnualValues
  sort_order: number
}

export interface FPKunden {
  id: number
  scenario_id: string
  segment_name: string
  segment_index: number
  row_label: string
  row_index: number
  percentage?: number
  formula_type?: string
  is_editable: boolean
  values: MonthlyValues
  sort_order: number
}

export interface FPUmsatzerloese {
  id: number
  scenario_id: string
  section: string  // 'revenue', 'quantity', 'price'
  row_label: string
  row_index: number
  is_editable: boolean
  values: MonthlyValues
  sort_order: number
}

export interface FPMaterialaufwand {
  id: number
  scenario_id: string
  section: string  // 'cost', 'quantity', 'unit_cost'
  row_label: string
  row_index: number
  is_editable: boolean
  values: MonthlyValues
  sort_order: number
}

// --- Compute result ---

export interface FPComputeResult {
  personalkosten: {
    total_brutto: MonthlyValues
    total_sozial: MonthlyValues
    total: MonthlyValues
    positions: FPPersonalkosten[]
    headcount: MonthlyValues
  }
  investitionen: {
    total_invest: MonthlyValues
    total_afa: MonthlyValues
    items: FPInvestitionen[]
  }
  umsatzerloese: {
    total: MonthlyValues
  }
  materialaufwand: {
    total: MonthlyValues
  }
  betriebliche: {
    total_sonstige: MonthlyValues
    total_gesamt: MonthlyValues
  }
  liquiditaet: {
    rows: FPLiquiditaet[]
    endstand: MonthlyValues
  }
  guv: AnnualValues[]
}

export interface SheetMeta {
  name: string
  label_de: string
  label_en: string
  rows: number
  editable_rows: number
}

export const SHEET_LIST: SheetMeta[] = [
  { name: 'kunden', label_de: 'Kunden', label_en: 'Customers', rows: 0, editable_rows: 0 },
  { name: 'umsatzerloese', label_de: 'Umsatzerlöse', label_en: 'Revenue', rows: 0, editable_rows: 0 },
  { name: 'materialaufwand', label_de: 'Materialaufwand', label_en: 'Material Costs', rows: 0, editable_rows: 0 },
  { name: 'personalkosten', label_de: 'Personalkosten', label_en: 'Personnel', rows: 0, editable_rows: 0 },
  { name: 'betriebliche', label_de: 'Betriebliche Aufwendungen', label_en: 'Operating Expenses', rows: 0, editable_rows: 0 },
  { name: 'investitionen', label_de: 'Investitionen', label_en: 'Investments', rows: 0, editable_rows: 0 },
  { name: 'liquiditaet', label_de: 'Liquidität', label_en: 'Cash Flow', rows: 0, editable_rows: 0 },
  { name: 'guv', label_de: 'GuV', label_en: 'P&L', rows: 0, editable_rows: 0 },
]
