/**
 * variables.js — Catalog of variables available to flow templates.
 *
 * Lists the `{{path.to.value}}` expressions that the runtime can resolve when
 * it interpolates a step's payload (SMS body, email subject, webhook URL, etc.)
 * or evaluates a JSONLogic/expression condition.
 *
 * The paths are keyed by the template's `applies_to` target (Customer,
 * Quotation, Service Contract, …) because the runtime builds the context
 * object differently per doctype:
 *
 *   - A Customer-scoped flow has `context.customer = <Customer doc>`
 *   - A Service Contract-scoped flow has `context.contract = <Service Contract doc>`
 *     plus `context.customer` auto-joined from `contract.customer`
 *   - And so on
 *
 * ⚠️  If you extend this catalog, mirror the change server-side in
 *     `services/targo-hub/lib/flow-context.js` so the runtime actually
 *     populates the new path.
 *
 * Performance: constant-time lookup, tables are < 20 entries. Safe to import
 * anywhere in the editor.
 */

// Always-available regardless of applies_to.
export const COMMON_VARIABLES = [
  { label: 'Maintenant (ISO 8601)',   path: 'now',           hint: 'Timestamp au démarrage du flow' },
  { label: 'ID du run',               path: 'flow.run_id',   hint: 'Identifiant unique du flow run' },
  { label: 'Nom du template',         path: 'flow.template', hint: 'Flow Template de référence' },
]

// Per-applies_to domain variables. Keep the strings exactly as the runtime
// exposes them (context.<root>.<field>) to avoid silent mismatches.
export const VARIABLE_CATALOGS = {
  Customer: [
    { label: 'ID client (doc)',        path: 'customer.name',           hint: 'ex: CUST-00042' },
    { label: 'Nom commercial',         path: 'customer.customer_name',  hint: 'Jean Tremblay / Acme Inc.' },
    { label: 'Type',                   path: 'customer.customer_type',  hint: 'Individual | Company' },
    { label: 'Groupe',                 path: 'customer.customer_group' },
    { label: 'Email',                  path: 'customer.email_id' },
    { label: 'Téléphone principal',    path: 'customer.primary_phone' },
    { label: 'Mobile',                 path: 'customer.mobile_no' },
    { label: 'Adresse principale',     path: 'customer.primary_address' },
    { label: 'Langue',                 path: 'customer.language',       hint: 'fr | en' },
    { label: 'Territoire',             path: 'customer.territory' },
  ],
  Quotation: [
    { label: 'ID devis',               path: 'quotation.name' },
    { label: 'Client (ID)',            path: 'quotation.customer' },
    { label: 'Nom du client',          path: 'quotation.customer_name' },
    { label: 'Total HT',               path: 'quotation.total' },
    { label: 'Total TTC',              path: 'quotation.grand_total' },
    { label: 'Statut',                 path: 'quotation.status' },
    { label: 'Date transaction',       path: 'quotation.transaction_date' },
    { label: 'Valide jusqu\'au',       path: 'quotation.valid_till' },
    { label: 'Email du client',        path: 'customer.email_id' },
    { label: 'Téléphone du client',    path: 'customer.primary_phone' },
  ],
  'Service Contract': [
    { label: 'ID contrat',             path: 'contract.name' },
    { label: 'Client (ID)',            path: 'contract.customer' },
    { label: 'Nom du client',          path: 'contract.customer_name' },
    { label: 'Plan',                   path: 'contract.plan' },
    { label: 'Abonnement (ID)',        path: 'contract.subscription' },
    { label: 'Date de début',          path: 'contract.start_date' },
    { label: 'Date de fin',            path: 'contract.end_date' },
    { label: 'Mensualité',             path: 'contract.monthly_price' },
    { label: 'Statut',                 path: 'contract.status' },
    { label: 'Email du client',        path: 'customer.email_id' },
    { label: 'Téléphone du client',    path: 'customer.primary_phone' },
    { label: 'Adresse d\'installation', path: 'contract.service_address' },
  ],
  Issue: [
    { label: 'ID ticket',              path: 'issue.name' },
    { label: 'Sujet',                  path: 'issue.subject' },
    { label: 'Description',            path: 'issue.description' },
    { label: 'Priorité',               path: 'issue.priority' },
    { label: 'Statut',                 path: 'issue.status' },
    { label: 'Type',                   path: 'issue.issue_type' },
    { label: 'Client (ID)',            path: 'issue.customer' },
    { label: 'Nom du client',          path: 'issue.customer_name' },
    { label: 'Date d\'ouverture',      path: 'issue.opening_date' },
    { label: 'Email du client',        path: 'customer.email_id' },
  ],
  Subscription: [
    { label: 'ID abonnement',          path: 'subscription.name' },
    { label: 'Client (ID)',            path: 'subscription.customer' },
    { label: 'Date de début',          path: 'subscription.start_date' },
    { label: 'Date de fin',            path: 'subscription.end_date' },
    { label: 'Plan',                   path: 'subscription.plan' },
    { label: 'Statut',                 path: 'subscription.status' },
  ],
}

/**
 * Return the flat list of variables available for the given applies_to target.
 * If applies_to is null/unknown, returns just the COMMON_VARIABLES.
 */
export function getVariables (appliesTo) {
  const specific = VARIABLE_CATALOGS[appliesTo] || []
  return [...specific, ...COMMON_VARIABLES]
}

/**
 * List of applies_to keys we know about — used by the editor to warn users
 * when they haven't picked one yet.
 */
export function hasVariables (appliesTo) {
  return Boolean(VARIABLE_CATALOGS[appliesTo])
}
