/**
 * Client-Safe Stripe Pricing Utilities
 *
 * This module provides pricing utilities that can be safely imported
 * in both client and server components.
 *
 * DO NOT add 'server-only' or Redis/database dependencies here.
 *
 * For server-only operations with caching, use:
 * - import { getStripePriceId } from '@/lib/stripe-prices'
 *
 * For client-safe utilities, use:
 * - import { getAvailableCurrencies } from '@/lib/stripe-prices.client'
 */

import stripePricesConfig from '@/lib/config/stripe-prices.json';
import multiCurrencyConfig from '@/lib/config/stripe-prices-multi-currency.json';

// ============================================================
// Types (safe for client and server)
// ============================================================

export interface StripePriceMapping {
  tier_id: string;
  product_id: string;
  monthly_price_id: string;
  yearly_price_id: string;
}

export type BillingPeriod = 'monthly' | 'yearly';

export const VALID_TIERS = ['tier1', 'tier2', 'tier3', 'tier4', 'tier5'] as const;
export type TierId = (typeof VALID_TIERS)[number];

export const VALID_BILLING_PERIODS = ['monthly', 'yearly'] as const;

// ============================================================
// Client-Safe Currency Utilities
// ============================================================

/**
 * Get list of currencies that have Stripe prices configured
 * Client-safe - reads directly from JSON config
 */
export function getAvailableCurrencies(): string[] {
  return Object.keys(multiCurrencyConfig.currencies);
}

/**
 * Check if a currency has Stripe prices configured
 * Client-safe - reads directly from JSON config
 */
export function isCurrencyAvailable(currency: string): boolean {
  const currencyUpper = currency.toUpperCase();
  return currencyUpper in multiCurrencyConfig.currencies;
}

// ============================================================
// Client-Safe Constants
// ============================================================

/**
 * Setup fee in cents (919 QAR)
 */
export const SETUP_FEE = stripePricesConfig.setupFee;

/**
 * Trial period in days
 */
export const TRIAL_DAYS = stripePricesConfig.trialDays;

// ============================================================
// Client-Safe Setup Fee Utilities
// ============================================================

/**
 * Get the pre-created setup fee price ID for a tier
 * Client-safe - reads directly from JSON config
 */
export function getSetupFeePriceId(tierId: string): string {
  if (tierId === 'tier5') {
    return (stripePricesConfig as { setupFeePriceIdTier5?: string }).setupFeePriceIdTier5 ||
           (stripePricesConfig as { setupFeePriceId?: string }).setupFeePriceId || '';
  }
  return (stripePricesConfig as { setupFeePriceId?: string }).setupFeePriceId || '';
}

/**
 * Get the setup fee amount for a tier (in cents)
 * Client-safe - reads directly from JSON config
 */
export function getSetupFeeAmount(tierId: string): number {
  if (tierId === 'tier5') {
    return (stripePricesConfig as { setupFeeTier5?: number }).setupFeeTier5 || stripePricesConfig.setupFee;
  }
  return stripePricesConfig.setupFee;
}

// ============================================================
// Client-Safe Price ID Lookup (no cache, reads JSON directly)
// ============================================================

/**
 * Get Stripe price ID for a tier, billing period, and currency.
 * Client-safe — reads directly from JSON config (no Redis cache).
 * Async signature matches the server version for drop-in compatibility.
 */
export async function getStripePriceId(
  tierId: string,
  billingPeriod: 'monthly' | 'yearly',
  currency: string = 'QAR',
): Promise<string | null> {
  const currencyUpper = currency.toUpperCase();
  if (!isCurrencyAvailable(currencyUpper)) return null;

  type MCConfig = typeof multiCurrencyConfig;
  type CurrencyKey = keyof MCConfig['currencies'];
  const currencyConfig = multiCurrencyConfig.currencies[currencyUpper as CurrencyKey];
  if (!currencyConfig) return null;

  const tierConfig = currencyConfig[tierId as keyof typeof currencyConfig];
  if (!tierConfig) return null;

  return billingPeriod === 'monthly' ? tierConfig.monthly : tierConfig.yearly;
}

// ============================================================
// Validation Utilities (Client-Safe)
// ============================================================

/**
 * Validate tier ID
 */
export function isValidTier(tierId: string): tierId is TierId {
  return VALID_TIERS.includes(tierId as TierId);
}

/**
 * Validate billing period
 */
export function isValidBillingPeriod(period: string): period is BillingPeriod {
  return VALID_BILLING_PERIODS.includes(period as BillingPeriod);
}
