/**
 * Centralized Cache Key Registry
 *
 * Provides standardized key patterns and TTLs for all cached data.
 * This ensures consistent naming conventions and makes it easy to:
 * - Find all cache keys for a domain
 * - Understand cache structure at a glance
 * - Audit cache invalidation patterns
 *
 * Naming convention: {domain}:{scope}:{identifier}
 * Examples:
 * - orders:account:001ABC → All orders for account
 * - orders:detail:ORDER123 → Single order details
 * - user:profile:user-456 → User profile
 */

/**
 * Cache key builder functions
 * Each domain has its own namespace to prevent collisions
 */
export const CacheKeys = {
  // ============================================================================
  // Orders Domain
  // ============================================================================
  orders: {
    /** Orders list for a Salesforce account */
    accountOrders: (sfAccountId: string) => `orders:account:${sfAccountId}`,

    /** Single order details */
    orderDetails: (orderId: string) => `orders:detail:${orderId}`,

    /** Order validation result (temporary) */
    validationResult: (orderId: string) => `orders:validation:${orderId}`,
  },

  // ============================================================================
  // User Domain
  // ============================================================================
  user: {
    /** User profile data */
    profile: (userId: string) => `user:profile:${userId}`,

    /** User dashboard summary */
    dashboardSummary: (userId: string) => `user:dashboard:${userId}`,
  },

  // ============================================================================
  // Services Domain (Subscriptions)
  // ============================================================================
  services: {
    /** All services for a WHMCS client */
    clientServices: (whmcsClientId: number) => `services:client:${whmcsClientId}`,

    /** Single service/subscription details */
    serviceDetails: (serviceId: number) => `services:detail:${serviceId}`,

    /** SIM usage data */
    simUsage: (subscriptionId: number) => `services:sim:usage:${subscriptionId}`,

    /** Internet service details */
    internetDetails: (subscriptionId: number) => `services:internet:${subscriptionId}`,
  },

  // ============================================================================
  // Billing Domain
  // ============================================================================
  billing: {
    /** Invoices list for a WHMCS client */
    clientInvoices: (whmcsClientId: number) => `billing:invoices:${whmcsClientId}`,

    /** Single invoice details */
    invoiceDetails: (invoiceId: number) => `billing:invoice:${invoiceId}`,

    /** Payment methods for a WHMCS client */
    paymentMethods: (whmcsClientId: number) => `billing:payments:${whmcsClientId}`,
  },

  // ============================================================================
  // ID Mappings Domain
  // ============================================================================
  mappings: {
    /** User to external IDs mapping */
    byUserId: (userId: string) => `mappings:user:${userId}`,

    /** WHMCS client to user mapping */
    byWhmcsClientId: (clientId: number) => `mappings:whmcs:${clientId}`,

    /** Salesforce account to user mapping */
    bySfAccountId: (accountId: string) => `mappings:sf:${accountId}`,
  },

  // ============================================================================
  // Support Domain
  // ============================================================================
  support: {
    /** Support cases for a Salesforce account */
    accountCases: (sfAccountId: string) => `support:cases:${sfAccountId}`,

    /** Single case details */
    caseDetails: (caseId: string) => `support:case:${caseId}`,
  },

  // ============================================================================
  // Catalog Domain
  // ============================================================================
  catalog: {
    /** Products list */
    products: () => `catalog:products`,

    /** Single product details */
    productDetails: (productId: string) => `catalog:product:${productId}`,

    /** Pricebook entries */
    pricebook: (pricebookId: string) => `catalog:pricebook:${pricebookId}`,
  },
} as const;

/**
 * Cache TTL configuration in seconds
 *
 * Categories:
 * - CDC_DRIVEN (null): No TTL - invalidated by Change Data Capture events
 * - SHORT: 60 seconds - frequently changing data
 * - MEDIUM: 300 seconds (5 min) - moderately stable data
 * - LONG: 3600 seconds (1 hour) - rarely changing data
 * - EXTENDED: 86400 seconds (24 hours) - static/reference data
 */
export const CacheTTL = {
  // CDC-driven caches (no expiry, invalidated by events)
  orders: {
    accountOrders: null, // CDC-driven
    orderDetails: null, // CDC-driven
    validationResult: 300, // 5 minutes - temporary validation data
  },

  user: {
    profile: 600, // 10 minutes
    dashboardSummary: 60, // 1 minute - frequently changing
  },

  services: {
    clientServices: 300, // 5 minutes
    serviceDetails: 300, // 5 minutes
    simUsage: 60, // 1 minute - real-time usage data
    internetDetails: 300, // 5 minutes
  },

  billing: {
    clientInvoices: 300, // 5 minutes
    invoiceDetails: 600, // 10 minutes
    paymentMethods: 600, // 10 minutes
  },

  mappings: {
    byUserId: 3600, // 1 hour - rarely changes
    byWhmcsClientId: 3600, // 1 hour
    bySfAccountId: 3600, // 1 hour
  },

  support: {
    accountCases: 60, // 1 minute - case updates are frequent
    caseDetails: 60, // 1 minute
  },

  catalog: {
    products: 3600, // 1 hour
    productDetails: 3600, // 1 hour
    pricebook: 86400, // 24 hours - rarely changes
  },
} as const;

/**
 * Pattern builders for bulk operations (e.g., invalidating all orders for a user)
 */
export const CachePatterns = {
  /** All orders-related keys for an account */
  allAccountOrders: (sfAccountId: string) => `orders:*:${sfAccountId}*`,

  /** All user-related keys */
  allUserKeys: (userId: string) => `user:*:${userId}*`,

  /** All services for a WHMCS client */
  allClientServices: (whmcsClientId: number) => `services:*:${whmcsClientId}*`,

  /** All mappings for a user */
  allUserMappings: (userId: string) => `mappings:*:${userId}*`,
} as const;
