// Plain-English names for where imported history came from.
//
// Entries created by a restore carry journal_entries.source (surfaced by
// GET /api/c/entries/:id as `source`, and set from the import batch's own
// source by the post function in db/migrations/0003_import_layer.sql):
//
//   'quickbooks_journal'   the QuickBooks Desktop Journal restore
//   'universal:<slug>'     the restore-from-other-software wizard, where
//                          <slug> is the preset key the user picked
//                          (server/src/universalImport.ts's sanitizeSlug)
//
// The UI must never assume everyone came from QuickBooks — a Sage
// BusinessWorks user drilling into her own migrated history has to see Sage,
// not QuickBooks. So the label is DERIVED from the stored source, never
// guessed: a slug we recognize names the product, and anything else (the two
// generic presets, the 'import' default, a slug from a future preset, a null
// source) falls back to the honest "your old software". Nothing here invents
// a product name the user didn't pick.
//
// Keys track tools/universal-presets.ts's UNIVERSAL_PRESETS and the source
// cards in web/src/setup/UniversalWizard.tsx; the generic-* keys are
// deliberately absent (they mean "something else", which is exactly the
// fallback). Covered by tests/web/import-source.test.ts.

const QUICKBOOKS_SOURCE = "quickbooks_journal";
const UNIVERSAL_PREFIX = "universal:";

/** Product names for the presets that identify a specific product. */
const UNIVERSAL_SOURCE_NAMES: Record<string, string> = {
  "sage-businessworks": "Sage BusinessWorks",
  "sage-50": "Sage 50",
  xero: "Xero",
  wave: "Wave",
  freshbooks: "FreshBooks",
};

/** What we call the software when its export didn't identify a product. */
export const UNKNOWN_SOURCE_NAME = "your old software";

/**
 * The software an entry's history came from, as a person would say it:
 * "QuickBooks", "Sage BusinessWorks", or "your old software".
 */
export function importSourceName(source: string | null | undefined): string {
  if (source === QUICKBOOKS_SOURCE) return "QuickBooks";
  if (source && source.startsWith(UNIVERSAL_PREFIX)) {
    return UNIVERSAL_SOURCE_NAMES[source.slice(UNIVERSAL_PREFIX.length)] ?? UNKNOWN_SOURCE_NAME;
  }
  return UNKNOWN_SOURCE_NAME;
}

/**
 * Provenance badge for an entry that came in on an import batch —
 * "Imported from Sage BusinessWorks — general-ledger-2019.csv".
 */
export function importedFromLabel(source: string | null | undefined, filename: string): string {
  return `Imported from ${importSourceName(source)} — ${filename}`;
}
