import { useState } from 'react';
import { aiPolicy, trainingPlan, worksAgreement } from '../engine/templatesCompany';
import { approvalRequest, auditChecklist, reportingPolicy } from '../engine/templatesInternal';
import type { AISystem, CompanyProfile } from '../engine/types';
import { useI18n } from '../i18n/context';
import { printDocument } from '../print/print';
import { downloadText, todayISO } from './common';

interface TemplatesViewProps {
  readonly systems: readonly AISystem[];
  readonly profile: CompanyProfile;
}

type TplKind = 'policy' | 'reporting' | 'approval' | 'audit' | 'training' | 'worksAgreement';

/** Unternehmensweite Vorlagen: KI-Richtlinie, Schulungsplan, Betriebsvereinbarung. */
export function TemplatesView({ systems, profile }: TemplatesViewProps): JSX.Element {
  const { t, locale, pack } = useI18n();
  const today = todayISO();
  const [preview, setPreview] = useState<TplKind | null>(null);

  const templates: Record<
    TplKind,
    { label: string; desc: string; file: string; render: () => string }
  > = {
    policy: {
      label: t.tplPolicy,
      desc: t.tplPolicyDesc,
      file: `ki-richtlinie-${today}.md`,
      render: () => aiPolicy(pack, today, profile, locale),
    },
    reporting: {
      label: t.tplReporting,
      desc: t.tplReportingDesc,
      file: `melde-richtlinie-${today}.md`,
      render: () => reportingPolicy(pack, today, profile, locale),
    },
    approval: {
      label: t.tplApproval,
      desc: t.tplApprovalDesc,
      file: `freigabeantrag-ki-${today}.md`,
      render: () => approvalRequest(pack, today, profile, locale),
    },
    audit: {
      label: t.tplAudit,
      desc: t.tplAuditDesc,
      file: `audit-checkliste-ki-${today}.md`,
      render: () => auditChecklist(pack, today, profile, locale),
    },
    training: {
      label: t.tplTraining,
      desc: t.tplTrainingDesc,
      file: `schulungsplan-${today}.md`,
      render: () => trainingPlan(systems, pack, today, profile, locale),
    },
    worksAgreement: {
      label: t.tplWorksAgreement,
      desc: t.tplWorksAgreementDesc,
      file: `betriebsvereinbarung-ki-${today}.md`,
      render: () => worksAgreement(pack, today, profile, locale),
    },
  };

  return (
    <div>
      <h1>{t.templatesTitle}</h1>
      <p className="sub">{t.templatesSub}</p>

      {(Object.keys(templates) as TplKind[]).map((kind) => (
        <div key={kind} className="panel">
          <div className="panel-head">
            <div>
              <h2 style={{ margin: 0 }}>{templates[kind].label}</h2>
              <p className="hint" style={{ margin: '4px 0 0' }}>
                {templates[kind].desc}
              </p>
            </div>
            <div className="toolbar" style={{ margin: 0 }}>
              <button className="ghost" onClick={() => setPreview(preview === kind ? null : kind)}>
                {preview === kind ? t.docHide : t.docShow}
              </button>
              <button
                className="primary"
                onClick={() =>
                  printDocument(templates[kind].file.replace(/\.md$/, ''), templates[kind].render(), profile)
                }
              >
                {t.docPrint}
              </button>
              <button
                className="ghost"
                onClick={() => downloadText(templates[kind].file, templates[kind].render())}
              >
                .md
              </button>
            </div>
          </div>
          {preview === kind && <pre className="doc-preview">{templates[kind].render()}</pre>}
        </div>
      ))}

      <p className="disclaimer">{t.templatesHint}</p>
    </div>
  );
}
