/**
 * Application Configuration
 * Central configuration for the Storify e-commerce platform
 */

import { DEFAULT_STORE_NAME } from "./branding.config";

export const appConfig = {
  // App Info
  name: DEFAULT_STORE_NAME,
  description: "Multi-vendor E-commerce Platform",
  // Brand tagline shown after the store name in the homepage title.
  tagline: "AI powered eCommerce Operating System",
  version: "1.0.0",

  // Default vendor slug (used in single-vendor mode)
  // Multi-vendor mode is controlled via database: settings.multiVendorMode.enabled
  defaultVendorSlug: "main-store",

  // Commission Settings (for multi-vendor mode)
  defaultCommission: 10, // Percentage

  // Pagination
  defaultPageSize: 12,
  maxPageSize: 100,

  // Image Upload
  maxImageSize: 5 * 1024 * 1024, // 5MB
  allowedImageTypes: ["image/jpeg", "image/png", "image/webp"],
  maxProductImages: 10,

  // Cart
  cartExpiryDays: 30,
  maxCartItems: 50,

  // Order
  orderNumberPrefix: "ORD",

  // Currency
  currency: {
    code: "USD",
    symbol: "$",
    locale: "en-US",
  },

  // URLs
  urls: {
    home: "/",
    login: "/login",
    register: "/register",
    adminDashboard: "/admin/dashboard",
    vendorDashboard: "/vendor/dashboard",
    staffDashboard: "/staff/dashboard",
    customerOrders: "/account/orders",
  },
} as const;

// User Roles
export const USER_ROLES = {
  CUSTOMER: "customer",
  VENDOR: "vendor",
  ADMIN: "admin",
  STAFF: "staff",
  SELLER: "seller", // Legacy staff role retained for existing accounts
} as const;

export type UserRole = (typeof USER_ROLES)[keyof typeof USER_ROLES];

// User Account Statuses
export const USER_ACCOUNT_STATUS = {
  ACTIVE: "active",
  INACTIVE: "inactive",
  BANNED: "banned",
} as const;

export type UserAccountStatus =
  (typeof USER_ACCOUNT_STATUS)[keyof typeof USER_ACCOUNT_STATUS];

// Order Statuses
export const ORDER_STATUS = {
  PREORDERED: "preordered",
  PENDING: "pending",
  PROCESSING: "processing",
  SHIPPED: "shipped",
  DELIVERED: "delivered",
  CANCELLED: "cancelled",
} as const;

export type OrderStatus = (typeof ORDER_STATUS)[keyof typeof ORDER_STATUS];

// Payment Statuses
export const PAYMENT_STATUS = {
  PENDING: "pending",
  PAID: "paid",
  PARTIALLY_PAID: "partially_paid",
  REFUNDED: "refunded",
  PARTIALLY_REFUNDED: "partially_refunded",
} as const;

export type PaymentStatus =
  (typeof PAYMENT_STATUS)[keyof typeof PAYMENT_STATUS];

// Product Statuses
export const PRODUCT_STATUS = {
  ACTIVE: "active",
  DRAFT: "draft",
  UNLISTED: "unlisted",
} as const;

export type ProductStatus =
  (typeof PRODUCT_STATUS)[keyof typeof PRODUCT_STATUS];

// Vendor Statuses
export const VENDOR_STATUS = {
  PENDING: "pending",
  PAYMENT_REQUIRED: "payment_required",
  APPROVED: "approved",
  REJECTED: "rejected",
  SUSPENDED: "suspended",
} as const;

export type VendorStatus = (typeof VENDOR_STATUS)[keyof typeof VENDOR_STATUS];

// Vendor application lifecycle before a Vendor record is approved.
export const VENDOR_APPLICATION_STATUS = {
  DRAFT: "draft",
  PAYMENT_PENDING: "payment_pending",
  PAID_PENDING_SUBMIT: "paid_pending_submit",
  SUBMITTED: "submitted",
  APPROVED: "approved",
  REJECTED: "rejected",
  REFUNDED: "refunded",
} as const;

export type VendorApplicationStatus =
  (typeof VENDOR_APPLICATION_STATUS)[keyof typeof VENDOR_APPLICATION_STATUS];

export const VENDOR_APPLICATION_PAYMENT_STATUS = {
  NOT_REQUIRED: "not_required",
  UNPAID: "unpaid",
  PENDING: "pending",
  PAID: "paid",
  FAILED: "failed",
  EXPIRED: "expired",
  REFUNDED: "refunded",
} as const;

export type VendorApplicationPaymentStatus =
  (typeof VENDOR_APPLICATION_PAYMENT_STATUS)[keyof typeof VENDOR_APPLICATION_PAYMENT_STATUS];

// Vendor subscription plan catalog status
export const VENDOR_PLAN_STATUS = {
  ACTIVE: "active",
  ARCHIVED: "archived",
} as const;

export type VendorPlanStatus =
  (typeof VENDOR_PLAN_STATUS)[keyof typeof VENDOR_PLAN_STATUS];

// Plan billing cadence. "none" = free / no recurring charge.
export const VENDOR_BILLING_INTERVAL = {
  MONTHLY: "monthly",
  YEARLY: "yearly",
  NONE: "none",
} as const;

export type VendorBillingInterval =
  (typeof VENDOR_BILLING_INTERVAL)[keyof typeof VENDOR_BILLING_INTERVAL];

// Per-vendor subscription lifecycle. Orthogonal to VENDOR_STATUS and role.
export const VENDOR_SUBSCRIPTION_STATUS = {
  INCOMPLETE: "incomplete",
  TRIALING: "trialing",
  ACTIVE: "active",
  PAST_DUE: "past_due",
  CANCELLED: "cancelled",
  EXPIRED: "expired",
} as const;

export type VendorSubscriptionStatus =
  (typeof VENDOR_SUBSCRIPTION_STATUS)[keyof typeof VENDOR_SUBSCRIPTION_STATUS];

export const VENDOR_PENDING_CHANGE_TYPE = {
  UPGRADE: "upgrade",
  DOWNGRADE: "downgrade",
  CANCEL: "cancel",
} as const;

export type VendorPendingChangeType =
  (typeof VENDOR_PENDING_CHANGE_TYPE)[keyof typeof VENDOR_PENDING_CHANGE_TYPE];

export const VENDOR_PENDING_CHANGE_STATUS = {
  AWAITING_VENDOR: "awaiting_vendor",
  AWAITING_PAYMENT: "awaiting_payment",
  SCHEDULED: "scheduled",
  APPLIED: "applied",
  FAILED: "failed",
  EXPIRED: "expired",
} as const;

export type VendorPendingChangeStatus =
  (typeof VENDOR_PENDING_CHANGE_STATUS)[keyof typeof VENDOR_PENDING_CHANGE_STATUS];

// Subscription statuses that count as "occupying" a vendor's single active
// subscription slot (used by the partial-unique index and commission resolution).
export const ACTIVE_SUBSCRIPTION_STATUSES: VendorSubscriptionStatus[] = [
  VENDOR_SUBSCRIPTION_STATUS.TRIALING,
  VENDOR_SUBSCRIPTION_STATUS.ACTIVE,
  VENDOR_SUBSCRIPTION_STATUS.PAST_DUE,
];

// Dunning policy for a lapsed paid subscription. Gateway-agnostic: when a paid
// period ends without payment, the subscription enters PAST_DUE and is retried
// up to MAX_RETRIES over a grace window (staggered by backoff) before it finally
// EXPIRES and the plan benefit is revoked. Mirrors the WooCommerce Subscriptions
// model (retry, hold, then fail) without assuming a specific payment provider.
// The actual charge attempt is a seam a real billing engine plugs in later; the
// state machine here decides WHEN to retry and WHEN to give up.
export const SUBSCRIPTION_DUNNING = {
  /** How many charge retries before the subscription expires. */
  MAX_RETRIES: 5,
  /** Total grace window (days) from period-end to final expiry. */
  GRACE_PERIOD_DAYS: 7,
  /** Hours between successive retries (staggered backoff). Length need not
   *  equal MAX_RETRIES — the state machine clamps by GRACE_PERIOD_DAYS. */
  RETRY_BACKOFF_HOURS: [12, 12, 24, 48, 72],
} as const;

export const VENDOR_PAYMENT_INVITATION = {
  DEADLINE_DAYS: 7,
  REMINDER_DAYS: [3, 6],
} as const;

export const VENDOR_SUBSCRIPTION_TERMS_VERSION = "vendor-subscription-v1";
