export interface EDMTemplateVersion {
  id: string;
  template_id: string;
  active: 0 | 1;
  name: string;
  html_content: string;
  plain_content: string;
  subject: string;
  updated_at: string;
  editor?: string;
  thumbnail_url?: string;
  generate_plain_content?: boolean;
}

export interface EDMTemplate {
  id: string;
  name: string;
  generation: "dynamic" | "legacy";
  updated_at: string;
  versions: EDMTemplateVersion[];
  /** Local-store only; absent while a template is still owned by SendGrid. */
  folder_id?: string | null;
  /** Where it came FROM: "local" if authored here, "sendgrid" if backfilled. */
  source?: string;
  /**
   * Where it IS, and therefore where an edit or delete goes.
   *
   * Not the same as `source`: a backfilled template reads "sendgrid" there
   * while living in GCS. Use this for anything that acts on the template.
   */
  store?: "gcs" | "sendgrid";
}

export interface CreateEDMInput {
  name: string;
  subject: string;
  html_content: string;
  plain_content?: string;
  active?: 0 | 1;
  generate_plain_content?: boolean;
}

export interface UpdateEDMInput {
  name?: string;
  subject?: string;
  html_content?: string;
  plain_content?: string;
  active?: 0 | 1;
  generate_plain_content?: boolean;
  folder_id?: string | null;
}

// ── authoring workflow ───────────────────────────────────────────────────────

/** A template row as it appears inside a folder listing. */
export interface EDMFolderTemplate {
  id: string;
  name: string;
  source: string;
  folder_id: string | null;
  updated_at: string;
  active_version_id: string | null;
  subject: string | null;
  version_number: number | null;
}

/**
 * An image stored under a folder's images/ prefix.
 *
 * The filename keeps a short content hash, so two different files both called
 * hero.png in the same folder cannot overwrite one another — and the loser of
 * that race would be a live image in delivered mail.
 *
 * `usedBy` is derived from template manifests, so it is a lower bound: a
 * hand-edited template can reference an image the manifest never recorded.
 * Treat an empty usedBy as "probably unused", not "safe to delete".
 */
export interface EDMFolderAsset {
  original: string;
  path: string;
  url: string;
  sha256: string;
  usedBy: string[];
}

export interface EDMBrowseResult {
  folder: EDMFolder | null;
  breadcrumb: { id: string; name: string }[];
  folders: EDMFolder[];
  templates: EDMFolderTemplate[];
  assets: EDMFolderAsset[];
}

export interface EDMFolder {
  id: string;
  parent_id: string | null;
  name: string;
  sort_order: number;
  created_at: string;
  updated_at: string;
}

/** Where in the document an image reference was found. */
export type ImageRefKind =
  "img-src" | "img-srcset" | "background-attr" | "css-url" | "vml-src";

export interface ImageRefLocation {
  url: string;
  kind: ImageRefKind;
}

export interface UnknownHelper {
  name: string;
  count: number;
  /** `{{#foo a b}}` can only be a helper call; `{{#foo}}` might be a data field. */
  hasArguments: boolean;
}

export interface ImageScanReport {
  total: number;
  /** Relative paths with no file behind them — blocks publishing. */
  unresolved: ImageRefLocation[];
  /** Inline base64; works, but counts against Gmail's 102 KB clip threshold. */
  dataUris: number;
  insecure: ImageRefLocation[];
  localhost: ImageRefLocation[];
  external: string[];
  /** Images only — a template with clean images can still have a bad helper. */
  ok: boolean;
  /** Handlebars block helpers the renderer does not implement. */
  helpers?: UnknownHelper[];
}

export interface EDMAssetEntry {
  original: string;
  path: string;
  url: string;
  sha256: string;
}

export interface EDMResolveResult {
  html: string;
  manifest: EDMAssetEntry[];
  unresolved: string[];
  report: ImageScanReport;
}

export interface EDMImportResult extends EDMResolveResult {
  html_path: string;
  warnings: string[];
  image_count: number;
  suggested_subject: string;
  /** How many .html files the archive holds. >1 means single import dropped some. */
  html_count: number;
  other_html_paths: string[];
}

export interface BatchImportEntryResult {
  htmlPath: string;
  name: string;
  folderPath: string[];
  status: "created" | "failed";
  templateId?: string;
  /** Created but left unpublished because an image reference is broken. */
  needsAttention?: boolean;
  unresolved?: string[];
  message?: string;
}

export interface BatchImportResult {
  created: number;
  failed: number;
  needsAttention: number;
  foldersCreated: number;
  entries: BatchImportEntryResult[];
  warnings: string[];
}

/** Where new EDMs get stored. Existing ones are unaffected — see EDMStorageTarget. */
export type EDMStorageTargetValue = "gcs" | "sendgrid";

export interface EDMStorageTarget {
  /** In force right now. */
  target: EDMStorageTargetValue;
  /** "console" = a super admin chose it; "env" = nobody has, server default applies. */
  source: "console" | "env";
  envDefault: EDMStorageTargetValue;
  /** Whether the caller may change it. */
  canChange: boolean;
}
