/**
 * Central registry of rate-limit policies.
 *
 * Every limited endpoint must reference a scope from this map. Hardcoded
 * `max` / `windowMs` literals at call sites are forbidden — change a policy
 * here once, applies everywhere.
 *
 * Adding a scope:
 *   1. Add an entry below with positive `max` and `windowMs`.
 *   2. Reference via `applyRateLimitFor(request, '<scope>')`.
 *   3. Update tests in `tests/unit/security/policies.test.ts`.
 */

export const RateLimitPolicies = {
  // --- Auth surfaces -------------------------------------------------------
  'auth.register': { max: 5, windowMs: 60_000 },
  'auth.login': { max: 10, windowMs: 60_000 },
  'auth.attach-phone': { max: 5, windowMs: 60_000 },
  'auth.magic-link': { max: 5, windowMs: 60_000 },
  'auth.firebase-verify': { max: 5, windowMs: 60_000 },
  'auth.session-read': { max: 240, windowMs: 60_000 },
  'auth.refresh': { max: 10, windowMs: 60_000 },
  'auth.verify-send': { max: 3, windowMs: 60_000 },
  'auth.verify-send-ip': { max: 10, windowMs: 3_600_000 },
  'auth.onboarding': { max: 5, windowMs: 60_000 },

  // --- Address services ----------------------------------------------------
  'address.reverse-geocode': { max: 10, windowMs: 60_000 },
  'address.geocode': { max: 20, windowMs: 60_000 },
  'address.streets': { max: 30, windowMs: 60_000 },

  // --- Social sharing ---
  'share.generate': { max: 10, windowMs: 60_000 },

  // --- Cart / checkout -----------------------------------------------------
  'cart.mutation': { max: 30, windowMs: 60_000 },
  'checkout.pending': { max: 10, windowMs: 60_000 },
  'checkout.confirm': { max: 5, windowMs: 60_000 },
  'checkout.intent': { max: 10, windowMs: 60_000 },
  'checkout.intent-guest': { max: 10, windowMs: 60_000 },
  'checkout.finalize': { max: 20, windowMs: 60_000 },
  'checkout.cart': { max: 5, windowMs: 60_000 },

  // --- Uploads -------------------------------------------------------------
  'uploads.complete': { max: 10, windowMs: 60_000 },

  // --- Cases ---------------------------------------------------------------
  'cases.create': { max: 5, windowMs: 300_000 },

  // --- Personal deals ------------------------------------------------------
  'personal-deals.request': { max: 3, windowMs: 300_000 },

  // --- Refunds -------------------------------------------------------------
  'refunds.request': { max: 3, windowMs: 3_600_000 },

  // --- Admin ---------------------------------------------------------------
  'admin.refund': { max: 5, windowMs: 60_000 },
  'admin.rearm': { max: 10, windowMs: 60_000 },
  'admin.search': { max: 10, windowMs: 1_000 },
  'admin.reconcile': { max: 1, windowMs: 300_000 },
  'admin.campaign.send': { max: 2, windowMs: 300_000 },
  'admin.returns.override': { max: 5, windowMs: 60_000 },

  // --- Returns / RMA -------------------------------------------------------
  'returns.open': { max: 3, windowMs: 3600_000 },
  'returns.tracking': { max: 10, windowMs: 60_000 },
  'returns.cancel': { max: 5, windowMs: 60_000 },

  // --- Vendor onboarding ---------------------------------------------------
  'vendor.register': { max: 5, windowMs: 60_000 },
  'vendor.upgrade': { max: 3, windowMs: 60_000 },
  'vendor.stripe-onboard': { max: 10, windowMs: 60_000 },
  'vendor.stripe-session': { max: 10, windowMs: 60_000 },
  'vendor.buy-label': { max: 10, windowMs: 60_000 },

  // --- Vendor invoicing ----------------------------------------------------
  'vendor.invoicing.test-connection': { max: 5, windowMs: 60_000 },
  'vendor.invoicing.settings': { max: 10, windowMs: 60_000 },

  // --- Purchase messaging --------------------------------------------------
  'purchases.message': { max: 20, windowMs: 60_000 },

  // --- Translation ---------------------------------------------------------
  'translation.report': { max: 20, windowMs: 3_600_000 },

  // --- Wishlist ------------------------------------------------------------
  'wishlist.merge': { max: 5, windowMs: 60_000 },

  // --- Me ------------------------------------------------------------------
  'me.preferences': { max: 10, windowMs: 60_000 },

  // Avatar
  'user.avatar.gravatar-check': { max: 20, windowMs: 60_000 },

  // --- Public surfaces -----------------------------------------------------
  'public.search': { max: 30, windowMs: 60_000 },
  'public.suggest': { max: 60, windowMs: 60_000 },
  'public.reports': { max: 10, windowMs: 60_000 },
  'public.cities': { max: 30, windowMs: 60_000 },
  'public.feed': { max: 30, windowMs: 60_000 },
  'public.browse': { max: 30, windowMs: 60_000 },
  'public.hot': { max: 30, windowMs: 60_000 },
  'public.tags': { max: 30, windowMs: 60_000 },
  'public.facets': { max: 30, windowMs: 60_000 },

  // --- Referral / affiliate ------------------------------------------------
  'referral.touch': { max: 30, windowMs: 60_000 },
  'referral.link': { max: 20, windowMs: 60_000 },
  'referral.me': { max: 30, windowMs: 60_000 },
  'referral.withdraw': { max: 5, windowMs: 60_000 },
  'referral.enroll': { max: 5, windowMs: 60_000 },

  // --- Reviews -------------------------------------------------------------
  'reviews.submit': { max: 5, windowMs: 60_000 },

  // --- Contact -------------------------------------------------------------
  'contact.submit': { max: 3, windowMs: 300_000 },

  // --- Push ----------------------------------------------------------------
  'push.subscribe': { max: 10, windowMs: 60_000 },

  // --- Stock watchlist -----------------------------------------------------
  'stock-watch.subscribe': { max: 10, windowMs: 60_000 },

  // --- Guest purchases -----------------------------------------------------
  'purchases.guest': { max: 20, windowMs: 60_000 },

  // --- Affiliate -----------------------------------------------------------
  'affiliate.connect.onboard': { max: 5, windowMs: 300_000 },

  // --- Cart ----------------------------------------------------------------
  'cart.clear': { max: 30, windowMs: 60_000 },

  // --- Cases ---------------------------------------------------------------
  'cases.message': { max: 10, windowMs: 60_000 },
  'cases.decide': { max: 10, windowMs: 60_000 },
  'cases.offer': { max: 10, windowMs: 60_000 },
  'cases.reopen': { max: 10, windowMs: 60_000 },

  // --- Chat ----------------------------------------------------------------
  'chat.message': { max: 20, windowMs: 60_000 },
  'chat.thread.create': { max: 20, windowMs: 60_000 },

  // --- Checkout ------------------------------------------------------------
  'checkout.promo.apply': { max: 5, windowMs: 300_000 },

  // --- Deals batch ---------------------------------------------------------
  'deals.batch': { max: 10, windowMs: 60_000 },

  // --- Favorites -----------------------------------------------------------
  'favorites.toggle': { max: 60, windowMs: 60_000 },
  'favorites.notify': { max: 60, windowMs: 60_000 },

  // --- Group deals ---------------------------------------------------------
  'group-deal.reserve': { max: 10, windowMs: 60_000 },
  'group-deal.cancel': { max: 10, windowMs: 60_000 },
  'group-deal.waitlist': { max: 10, windowMs: 60_000 },

  // --- Me ------------------------------------------------------------------
  'me.recently-viewed': { max: 60, windowMs: 60_000 },

  // --- Notifications -------------------------------------------------------
  'notifications.read': { max: 60, windowMs: 60_000 },
  'notifications.mark-all-read': { max: 60, windowMs: 60_000 },
  'notifications.prefs': { max: 10, windowMs: 60_000 },

  // --- Payment methods -----------------------------------------------------
  'payment-method.create': { max: 10, windowMs: 60_000 },
  'payment-method.delete': { max: 10, windowMs: 60_000 },
  'payment-method.setup-intent': { max: 10, windowMs: 60_000 },

  // --- Purchases -----------------------------------------------------------
  'purchases.cancel': { max: 10, windowMs: 60_000 },
  'purchases.redeem': { max: 10, windowMs: 60_000 },
  'purchases.create': { max: 10, windowMs: 60_000 },

  // --- Push ----------------------------------------------------------------
  'push.unsubscribe': { max: 10, windowMs: 60_000 },

  // --- Returns vendor actions ----------------------------------------------
  'returns.approve': { max: 10, windowMs: 60_000 },
  'returns.deny': { max: 10, windowMs: 60_000 },
  'returns.inspect': { max: 10, windowMs: 60_000 },
  'returns.receipt': { max: 10, windowMs: 60_000 },
  'returns.draft-upload': { max: 10, windowMs: 60_000 },

  // --- Reviews -------------------------------------------------------------
  'reviews.remove': { max: 10, windowMs: 60_000 },
  'reviews.reply': { max: 10, windowMs: 60_000 },

  // --- Support -------------------------------------------------------------
  'support.attachment': { max: 10, windowMs: 60_000 },
  'support.ticket.close': { max: 10, windowMs: 60_000 },
  'support.ticket.message': { max: 20, windowMs: 60_000 },
  'support.ticket.create': { max: 5, windowMs: 300_000 },

  // --- User profile --------------------------------------------------------
  'user.profile.delete-request': { max: 3, windowMs: 3_600_000 },
  'user.profile.freeze': { max: 3, windowMs: 3_600_000 },
  'user.profile.update': { max: 10, windowMs: 60_000 },

  // --- Vendor address / hours ----------------------------------------------
  'vendor.address.update': { max: 10, windowMs: 60_000 },
  'vendor.business-hours': { max: 10, windowMs: 60_000 },

  // --- Vendor deal CRUD ----------------------------------------------------
  'vendor.deal.create': { max: 30, windowMs: 60_000 },
  'vendor.deal.archive': { max: 30, windowMs: 60_000 },
  'vendor.deal.duplicate': { max: 30, windowMs: 60_000 },
  'vendor.deal.edit': { max: 30, windowMs: 60_000 },
  'vendor.deal.pause': { max: 30, windowMs: 60_000 },
  'vendor.deal.quantity': { max: 30, windowMs: 60_000 },
  'vendor.deal.resume': { max: 30, windowMs: 60_000 },
  'vendor.deal.submit': { max: 30, windowMs: 60_000 },

  // --- Vendor draft CRUD ---------------------------------------------------
  'vendor.draft.create': { max: 30, windowMs: 60_000 },
  'vendor.draft.edit': { max: 30, windowMs: 60_000 },
  'vendor.draft.delete': { max: 30, windowMs: 60_000 },
  'vendor.draft.star': { max: 30, windowMs: 60_000 },
  'vendor.draft.submit': { max: 30, windowMs: 60_000 },

  // --- Vendor group deals --------------------------------------------------
  'vendor.group-deal.extend': { max: 5, windowMs: 60_000 },
  'vendor.group-deal.honor': { max: 5, windowMs: 60_000 },
  'vendor.group-deal.relaunch': { max: 5, windowMs: 60_000 },

  // --- Vendor notifications ------------------------------------------------
  'vendor.notifications.settings': { max: 10, windowMs: 60_000 },

  // --- Vendor offers -------------------------------------------------------
  'vendor.offer.create': { max: 10, windowMs: 60_000 },
  'vendor.offer.mutate': { max: 10, windowMs: 60_000 },

  // --- Vendor personal deals -----------------------------------------------
  'vendor.personal-deal.accept': { max: 10, windowMs: 60_000 },
  'vendor.personal-deal.reject': { max: 10, windowMs: 60_000 },

  // --- Vendor profile ------------------------------------------------------
  'vendor.profile.update': { max: 10, windowMs: 60_000 },

  // --- Vendor promo codes --------------------------------------------------
  'vendor.promo-code.create': { max: 10, windowMs: 60_000 },
  'vendor.promo-code.mutate': { max: 10, windowMs: 60_000 },

  // --- Vendor analytics ----------------------------------------------------
  'vendor.analytics.read': { max: 30, windowMs: 60_000 },

  // --- Vendor dashboard ----------------------------------------------------
  'vendor.dashboard.read': { max: 30, windowMs: 60_000 },

  // --- Vendor deals read ---------------------------------------------------
  'vendor.deals.read': { max: 60, windowMs: 60_000 },

  // --- Vendor earnings -----------------------------------------------------
  'vendor.earnings.read': { max: 60, windowMs: 60_000 },

  // --- Vendor group deals read ---------------------------------------------
  'vendor.group-deals.read': { max: 60, windowMs: 60_000 },

  // --- Vendor invoicing read -----------------------------------------------
  'vendor.invoicing.read': { max: 60, windowMs: 60_000 },

  // --- Vendor orders-to-ship -----------------------------------------------
  'vendor.orders-to-ship.read': { max: 60, windowMs: 60_000 },

  // --- Vendor personal deals read ------------------------------------------
  'vendor.personal-deals.read': { max: 60, windowMs: 60_000 },

  // --- Vendor recommendations ----------------------------------------------
  'vendor.recommendations.write': { max: 20, windowMs: 60_000 },

  // --- Vendor shipments ----------------------------------------------------
  'vendor.shipments.read': { max: 60, windowMs: 60_000 },
  'vendor.shipments.write': { max: 20, windowMs: 60_000 },

  // --- Vendor shipping config ----------------------------------------------
  'vendor.shipping-config.write': { max: 20, windowMs: 60_000 },

  // --- Vendor Stripe -------------------------------------------------------
  'vendor.stripe.dashboard-link': { max: 10, windowMs: 60_000 },
  'vendor.stripe.read': { max: 60, windowMs: 60_000 },
  'vendor.stripe.sync': { max: 5, windowMs: 60_000 },
  'vendor.stripe.write': { max: 20, windowMs: 60_000 },

  // --- Wishlist ------------------------------------------------------------
  'wishlist.toggle': { max: 60, windowMs: 60_000 },
  'wishlist.notify': { max: 60, windowMs: 60_000 },
} as const;

export type RateLimitScope = keyof typeof RateLimitPolicies;

export function getRateLimitPolicy(scope: RateLimitScope): { max: number; windowMs: number } {
  return RateLimitPolicies[scope];
}
