// AI-instruction block shared by all four Copilot-Export builders (Demo-067
// C, KAR-982). Conceptual template: lib/assessment-export.ts's
// exportForAISummary (Markdown "## Anweisung für KI" + aiInstruction text) —
// same idea (a short block telling an LLM what the document is and how to
// use it), generalised across formats (DOCX/XLSX, not just Markdown) and
// modules (Fabrikanalyse/QAF/Wertstrom/Projekt, not just Fabrikanalyse).
//
// Pure, format-agnostic — returns plain strings. The DOCX/XLSX builders each
// render this content with their own primitives (Paragraph/TextRun vs.
// worksheet rows).
//
// Portability (ADR 010/015, scripts/check-forbidden-strings.mjs): this
// module is product core, not a customer adapter — it must never name a
// specific customer. Glossary wording is generic ("Consultant",
// "Lieferantenentwicklungs-Projekt"), not customer-specific, even though the
// concepts (LSC/QAF/VSM/FA) originate from docs/GLOSSARY.md.

import type { AiInstructionBlockContent, AiInstructionBlockInput, GlossaryEntry } from './types'

/** Fixed glossary — same four terms the task calls out (LSC/QAF/VSM/FA),
 * worded generically. Source of truth for the underlying concepts:
 * docs/GLOSSARY.md (not reused verbatim — that doc is written for vendor
 * engineers and freely names the pilot customer; this wording is the
 * customer-generic equivalent for product-core code). */
export const COPILOT_EXPORT_GLOSSARY: readonly GlossaryEntry[] = [
  {
    term: 'LSC',
    definition:
      'Lean Shop-floor Cycle – die strukturierte Vor-Ort-Erfassung von Zykluszeiten, Schichtleistung und Verbesserungsmaßnahmen an einzelnen Prozessschritten/Stationen eines Werks.',
  },
  {
    term: 'QAF',
    definition:
      'Qualitäts-Anforderungs-Formular – ein Dokument, mit dem bewertet wird, ob der Fertigungsprozess eines Lieferanten die Qualitäts- und Kostenanforderungen erfüllt. Zwei QAF-Versionen (z. B. Angebotsstand vs. aktueller Stand) werden Feld für Feld verglichen.',
  },
  {
    term: 'VSM / Wertstrom',
    definition:
      'Value Stream Map – die visuelle Darstellung des Material- und Informationsflusses durch ein Produktionssystem, vom Rohmaterial bis zum Fertigprodukt, inklusive wertschöpfender (VA) und nicht wertschöpfender Zeitanteile.',
  },
  {
    term: 'FA / Fabrikanalyse',
    definition:
      'Eine werksweite, strukturierte Bewertung (Assessment) über mehrere Kategorien und Unterkategorien hinweg – im Unterschied zur Einzelprozess-Messung (LSC).',
  },
]

const HEADING = 'Hinweis für die KI-gestützte Weiterverarbeitung'
const USAGE_HEADING = 'Vorschläge für die Weiterarbeit'
const GLOSSARY_HEADING = 'Glossar'

/**
 * The fictional-data disclaimer — only rendered when the source record's
 * `is_demo` flag is true. Doubles as an explicit LLM instruction (not just a
 * human-readable notice), so a consultant pasting this into a chat tool
 * can't accidentally have the model treat fabricated numbers as a real
 * supplier's data.
 */
export function fictionalDataDisclaimer(isDemo: boolean): string | null {
  if (!isDemo) return null
  return (
    'Demo-Projekt mit fiktiven Beispieldaten. Dieses Projekt dient ausschließlich zu ' +
    'Vorführzwecken – alle enthaltenen Firmen-, Personen- und Kennzahlen-Angaben sind frei ' +
    'erfunden und bilden keinen realen Fertigungsstandort ab. Für die KI-Weiterverarbeitung: ' +
    'Ergebnisse aus diesem Dokument dürfen nicht als Aussage über einen tatsächlichen ' +
    'Lieferanten zitiert oder weiterverwendet werden.'
  )
}

/** Assemble the full AI-instruction block for one export. */
export function buildAiInstructionBlock(input: AiInstructionBlockInput): AiInstructionBlockContent {
  return {
    heading: `${HEADING} — ${input.exportTitle}`,
    aboutText: input.aboutText,
    usageHeading: USAGE_HEADING,
    usageHints: input.usageHints,
    glossaryHeading: GLOSSARY_HEADING,
    glossary: COPILOT_EXPORT_GLOSSARY,
    disclaimer: fictionalDataDisclaimer(input.isDemo),
  }
}

/**
 * Feature-flag gate (`config/profiles` `features.copilotExports`) — pure
 * predicate so it is testable without booting a composition profile. Every
 * copilot-export server action calls this first with the resolved flag
 * value (`getProfile().features.copilotExports`), same spot other flags
 * gate at (e.g. app/api/v1/assignments/[id]/route.ts's workModeCapture
 * check) — resolving the profile itself stays outside this DB/env-free
 * module (ADR 019 "no DB/network/FS access in the module itself").
 */
export function copilotExportsGate(enabled: boolean): { ok: true } | { ok: false; error: string } {
  if (!enabled) {
    return { ok: false, error: 'Copilot-Exporte sind für dieses Deployment nicht aktiviert.' }
  }
  return { ok: true }
}
