/**
 * Template-Kategorisierung als Code-Registry.
 *
 * Source-of-Truth bei aktiver Migration 137/138 ist die DB.
 * Diese Registry dient als Fallback und für Frontend-only Filter,
 * wenn DB-Felder noch nicht verfügbar sind (z.B. lokale Dev-DB ohne Migration).
 *
 * Synchron halten mit migrations/138_template_backfill_categories.sql.
 */

export type LifecycleStage = 'pre_founding' | 'founding' | 'startup' | 'kmu' | 'konzern'

export type FunctionalCategory =
  | 'founding_legal'
  | 'employment'
  | 'investor_funding'
  | 'customer_b2b'
  | 'customer_b2c'
  | 'data_protection'
  | 'it_security'
  | 'ai_governance'
  | 'internal_policy'
  | 'public_facing'
  | 'compliance_process'
  | 'finance_tax'
  | 'vendor_supplier'

export interface TemplateCategorization {
  lifecycle_stage: LifecycleStage[]
  functional_category: FunctionalCategory
}

export const TEMPLATE_CATEGORIES: Record<string, TemplateCategorization> = {
  // Founding Legal
  gesellschafterliste:        { lifecycle_stage: ['pre_founding', 'founding'], functional_category: 'founding_legal' },
  gf_bestellungsbeschluss:    { lifecycle_stage: ['founding'], functional_category: 'founding_legal' },
  hrb_anmeldung:              { lifecycle_stage: ['founding'], functional_category: 'founding_legal' },
  ip_assignment_agreement:    { lifecycle_stage: ['pre_founding', 'founding', 'startup'], functional_category: 'founding_legal' },
  articles_of_association:    { lifecycle_stage: ['founding', 'startup', 'kmu', 'konzern'], functional_category: 'founding_legal' },
  sha:                        { lifecycle_stage: ['founding', 'startup', 'kmu', 'konzern'], functional_category: 'founding_legal' },
  geschaeftsordnung_gf:       { lifecycle_stage: ['founding', 'startup', 'kmu', 'konzern'], functional_category: 'founding_legal' },

  // Investor / Funding
  term_sheet:                 { lifecycle_stage: ['pre_founding', 'startup'], functional_category: 'investor_funding' },
  convertible_loan_agreement: { lifecycle_stage: ['pre_founding', 'startup'], functional_category: 'investor_funding' },
  subscription_agreement:     { lifecycle_stage: ['startup', 'kmu'], functional_category: 'investor_funding' },
  esop_plan:                  { lifecycle_stage: ['startup', 'kmu'], functional_category: 'investor_funding' },
  cap_table:                  { lifecycle_stage: ['founding', 'startup', 'kmu', 'konzern'], functional_category: 'investor_funding' },

  // Employment
  managing_director_employment_contract: { lifecycle_stage: ['founding', 'startup', 'kmu', 'konzern'], functional_category: 'employment' },
  employment_contract_de:                { lifecycle_stage: ['founding', 'startup', 'kmu', 'konzern'], functional_category: 'employment' },
  nda:                                   { lifecycle_stage: ['founding', 'startup', 'kmu', 'konzern'], functional_category: 'employment' },
  offboarding_policy:                    { lifecycle_stage: ['founding', 'startup', 'kmu', 'konzern'], functional_category: 'employment' },

  // Customer B2B
  agb:                       { lifecycle_stage: ['startup', 'kmu', 'konzern'], functional_category: 'customer_b2b' },
  sla:                       { lifecycle_stage: ['startup', 'kmu', 'konzern'], functional_category: 'customer_b2b' },
  dpa:                       { lifecycle_stage: ['startup', 'kmu', 'konzern'], functional_category: 'customer_b2b' },
  data_processing_agreement: { lifecycle_stage: ['startup', 'kmu', 'konzern'], functional_category: 'customer_b2b' },
  cloud_service_agreement:   { lifecycle_stage: ['startup', 'kmu', 'konzern'], functional_category: 'customer_b2b' },
  terms_of_service:          { lifecycle_stage: ['startup', 'kmu', 'konzern'], functional_category: 'customer_b2b' },

  // Public-facing
  impressum:                 { lifecycle_stage: ['founding', 'startup', 'kmu', 'konzern'], functional_category: 'public_facing' },

  // AI Governance
  ai_usage_policy:           { lifecycle_stage: ['startup', 'kmu', 'konzern'], functional_category: 'ai_governance' },

  // Whistleblower nur ab KMU (>=50 MA)
  whistleblower_policy:      { lifecycle_stage: ['kmu', 'konzern'], functional_category: 'internal_policy' },
}

/**
 * Notartermin-Bundle: alle Dokumente die für die Gründung benötigt werden.
 * Investor-Dokumente sind separat (term_sheet, convertible_loan_agreement, etc.).
 */
export const NOTARY_BUNDLE_DOCUMENTS: string[] = [
  'articles_of_association',          // Satzung — notariell beurkundet
  'gesellschafterliste',              // Pflicht § 40 GmbHG
  'gf_bestellungsbeschluss',          // Bestellung Geschäftsführer
  'hrb_anmeldung',                    // HRB-Anmeldung
  'sha',                              // optional parallel
  'geschaeftsordnung_gf',             // intern, nach Notar
  'managing_director_employment_contract', // GF-Dienstverträge
  'ip_assignment_agreement',          // Gründer-IP sichern
]

export function getDocumentsForStage(stage: LifecycleStage): string[] {
  return Object.entries(TEMPLATE_CATEGORIES)
    .filter(([, cat]) => cat.lifecycle_stage.includes(stage))
    .map(([docType]) => docType)
}

export function getDocumentsForCategory(category: FunctionalCategory): string[] {
  return Object.entries(TEMPLATE_CATEGORIES)
    .filter(([, cat]) => cat.functional_category === category)
    .map(([docType]) => docType)
}

export const LIFECYCLE_STAGE_LABELS: Record<LifecycleStage, string> = {
  pre_founding: 'Vor-Gründung (Term Sheet, IP-Sicherung)',
  founding:     'Gründung (Notar)',
  startup:      'Startup (0-3 Jahre, <25 MA)',
  kmu:          'KMU (3+ Jahre, 25-250 MA)',
  konzern:      'Konzern (250+ MA)',
}

export const FUNCTIONAL_CATEGORY_LABELS: Record<FunctionalCategory, string> = {
  founding_legal:    'Gründungsrechtliches',
  employment:        'Arbeitsverträge',
  investor_funding:  'Investor & Funding',
  customer_b2b:      'Kunden-Verträge (B2B)',
  customer_b2c:      'Kunden-Verträge (B2C)',
  data_protection:   'Datenschutz (DSGVO)',
  it_security:       'IT-Sicherheit',
  ai_governance:     'KI-Governance',
  internal_policy:   'Interne Richtlinien',
  public_facing:     'Öffentlich (Website)',
  compliance_process:'Compliance-Prozesse',
  finance_tax:       'Finanzen & Steuern',
  vendor_supplier:   'Lieferanten',
}
