'use client'

/**
 * Lifecycle-Phasen-Filter für den Document-Generator.
 *
 * Zeigt 5 Phasen-Tabs (Pre-Founding, Founding, Startup, KMU, Konzern) und
 * filtert die angezeigten Templates entsprechend ihres `lifecycle_stage`-Arrays.
 *
 * Phasen-Definitionen synchron zu lib/sdk/founding/template-categories.ts
 */

import {
  LIFECYCLE_STAGE_LABELS,
  type LifecycleStage,
  TEMPLATE_CATEGORIES,
} from '@/lib/sdk/founding/template-categories'

interface Props {
  activeStage: LifecycleStage | 'all'
  onChange: (stage: LifecycleStage | 'all') => void
  /** Template-Counts pro Stage (optional, sonst aus Code-Registry berechnet) */
  countsByStage?: Record<string, number>
}

const STAGE_ORDER: (LifecycleStage | 'all')[] = [
  'all',
  'pre_founding',
  'founding',
  'startup',
  'kmu',
  'konzern',
]

const STAGE_ICONS: Record<LifecycleStage | 'all', string> = {
  all: '📚',
  pre_founding: '🌱',
  founding: '⚖️',
  startup: '🚀',
  kmu: '🏢',
  konzern: '🏛️',
}

const STAGE_HINTS: Record<LifecycleStage, string> = {
  pre_founding: 'Vor dem Notartermin — Term Sheet, IP-Sicherung, Wandeldarlehen',
  founding: 'Für den Notartermin — Satzung, Gesellschafterliste, HRB-Anmeldung',
  startup: '0–3 Jahre, <25 Mitarbeiter — Arbeitsverträge, AVV, Datenschutz',
  kmu: '3+ Jahre, 25–250 MA — ISMS, Whistleblower, vollständige TOM',
  konzern: '250+ MA — Konzern-Compliance, ISO 27001',
}

export function LifecycleFilter({ activeStage, onChange, countsByStage }: Props) {
  const counts = countsByStage || computeCountsFromRegistry()

  return (
    <div className="mb-6" data-testid="lifecycle-filter">
      <div className="flex items-center gap-2 mb-2">
        <h3 className="text-sm font-semibold text-gray-700">Phase Deines Unternehmens</h3>
        <span className="text-xs text-gray-500">— filtert Dokumente nach Lifecycle</span>
      </div>
      <div className="flex flex-wrap gap-2">
        {STAGE_ORDER.map(stage => {
          const isAll = stage === 'all'
          const count = isAll
            ? Object.values(counts).reduce((s, c) => s + c, 0)
            : (counts[stage] || 0)
          const label = isAll ? 'Alle' : LIFECYCLE_STAGE_LABELS[stage as LifecycleStage].split(' (')[0]
          const isActive = activeStage === stage
          return (
            <button
              key={stage}
              type="button"
              data-testid={`stage-tab-${stage}`}
              onClick={() => onChange(stage)}
              className={`px-3 py-2 rounded-lg border text-sm font-medium transition ${
                isActive
                  ? 'bg-purple-600 text-white border-purple-600 shadow-sm'
                  : 'bg-white text-gray-700 border-gray-200 hover:border-purple-300 hover:bg-purple-50'
              }`}
            >
              <span className="mr-1.5">{STAGE_ICONS[stage]}</span>
              {label}
              <span className={`ml-2 px-1.5 py-0.5 text-xs rounded-full ${
                isActive ? 'bg-white/20' : 'bg-gray-100 text-gray-600'
              }`}>
                {count}
              </span>
            </button>
          )
        })}
      </div>
      {activeStage !== 'all' && (
        <p className="mt-2 text-sm text-gray-500" data-testid="stage-hint">
          {STAGE_HINTS[activeStage as LifecycleStage]}
        </p>
      )}
    </div>
  )
}

function computeCountsFromRegistry(): Record<string, number> {
  const counts: Record<string, number> = {
    pre_founding: 0, founding: 0, startup: 0, kmu: 0, konzern: 0,
  }
  for (const cat of Object.values(TEMPLATE_CATEGORIES)) {
    for (const stage of cat.lifecycle_stage) {
      counts[stage] = (counts[stage] || 0) + 1
    }
  }
  return counts
}

export function filterTemplatesByStage<T extends { document_type?: string; type?: string }>(
  templates: T[],
  stage: LifecycleStage | 'all'
): T[] {
  if (stage === 'all') return templates
  return templates.filter(t => {
    const docType = t.document_type || t.type
    if (!docType) return false
    const cat = TEMPLATE_CATEGORIES[docType]
    if (!cat) return stage === 'startup'  // Fallback: unkategorisierte zeigen wir in Startup
    return cat.lifecycle_stage.includes(stage)
  })
}
