import type { DataRecord } from '@/components/applicant/lib/templateTypes';
import { buildDocxTemplateExportBundle } from '@/components/applicant/initiative-draft/buildDocxTemplateExportBundle';
import { buildTemplateDataFromDraft } from '@/components/applicant/initiative-draft/draftToTemplateData';
import type { InitiativeDraft } from '@/components/applicant/initiative-draft/types';
import { buildOfficialBieuMauFromDraft } from '@/components/applicant/initiative-draft/mapDraftToOfficialBieuMau';

/**
 * Flat JSON — same shape as {@link buildTemplateDataFromDraft} / TemplateFiller upload (.json one row).
 */
export function downloadFlatTemplateDataJson(
  draft: InitiativeDraft,
  contribution: Record<string, unknown> | null | undefined,
  filenameBase: string,
): void {
  const data = buildTemplateDataFromDraft(draft, contribution) as DataRecord;
  const body = JSON.stringify(data, null, 2);
  const blob = new Blob([body], { type: 'application/json;charset=utf-8' });
  const url = URL.createObjectURL(blob);
  const safe = filenameBase.replace(/[^a-zA-Z0-9_\-\u00C0-\u024F]+/g, '-').trim() || 'mau-docx';
  const a = document.createElement('a');
  a.href = url;
  a.download = `${safe}-template-data.json`;
  a.click();
  setTimeout(() => URL.revokeObjectURL(url), 1000);
}

/**
 * Full bundle: `templateData` (flat) + `application` / `report` / `contribution` trees for review & tooling.
 */
export function downloadDocxTemplateBundleJson(
  draft: InitiativeDraft,
  contribution: Record<string, unknown> | null | undefined,
  filenameBase: string,
  caseId?: string,
): void {
  const bundle = buildDocxTemplateExportBundle(draft, contribution, { caseId });
  const body = JSON.stringify(bundle, null, 2);
  const blob = new Blob([body], { type: 'application/json;charset=utf-8' });
  const url = URL.createObjectURL(blob);
  const safe = filenameBase.replace(/[^a-zA-Z0-9_\-\u00C0-\u024F]+/g, '-').trim() || 'mau-docx';
  const a = document.createElement('a');
  a.href = url;
  a.download = `${safe}-docx-template.json`;
  a.click();
  setTimeout(() => URL.revokeObjectURL(url), 1000);
}

/**
 * Download JSON matching `public/assets/bieu_mau_sang_kien_template.json` shape
 * (Vietnamese section keys) — for be01 / API persistence.
 */
export function downloadOfficialBieuMauJson(
  draft: InitiativeDraft,
  contribution: Record<string, unknown> | null | undefined,
  filenameBase: string,
): void {
  const data = buildOfficialBieuMauFromDraft(draft, contribution);
  const body = JSON.stringify(data, null, 2);
  const blob = new Blob([body], { type: 'application/json;charset=utf-8' });
  const url = URL.createObjectURL(blob);
  const safe = filenameBase.replace(/[^a-zA-Z0-9_\-\u00C0-\u024F]+/g, '-').trim() || 'bieu-mau';
  const a = document.createElement('a');
  a.href = url;
  a.download = `${safe}-bieu-mau-chinh-thuc.json`;
  a.click();
  setTimeout(() => URL.revokeObjectURL(url), 1000);
}

/** @deprecated Use {@link downloadFlatTemplateDataJson} or {@link downloadDocxTemplateBundleJson}. */
export function downloadMergedTemplateJson(
  draft: InitiativeDraft,
  contribution: Record<string, unknown> | null | undefined,
  filenameBase: string,
  caseId?: string,
): void {
  void caseId;
  downloadDocxTemplateBundleJson(draft, contribution, filenameBase, caseId);
}
