'use client' import { useEffect, useState } from 'react' import Link from 'next/link' import { getCurrencyLabel, PLAN_FEATURES, PLAN_PRICES } from '@rentaldrivego/types' import { useMarketplacePreferences } from '@/components/MarketplaceShell' import { marketplaceFetchOrDefault } from '@/lib/api' import { resolveBrowserAppUrl } from '@/lib/appUrls' type Billing = 'monthly' | 'annual' type PricingMatrix = Record>> type PlanFeatureMap = Record type PlatformPricing = { prices: PricingMatrix planFeatures: PlanFeatureMap } export const DEFAULT_PRICING: PlatformPricing = { prices: PLAN_PRICES, planFeatures: PLAN_FEATURES, } const PLANS = [ { key: 'STARTER', name: 'Starter', tagline: 'Launch your fleet online', highlight: false, }, { key: 'GROWTH', name: 'Growth', tagline: 'Scale with confidence', highlight: true, }, { key: 'PRO', name: 'Pro', tagline: 'Enterprise-grade power', highlight: false, }, ] const copy = { en: { monthly: 'Monthly', annual: 'Annual', yearly: '/ year', youSave: 'You save', withAnnual: 'with annual billing — billed as one payment per year.', mostPopular: 'Most popular', perMonth: '/mo', billedAs: 'Billed as', getStarted: 'Get started', footer: 'All plans include a 90-day free trial. No credit card required.', }, fr: { monthly: 'Mensuel', annual: 'Annuel', yearly: '/ an', youSave: 'Vous économisez', withAnnual: 'avec la facturation annuelle, prélevée en un seul paiement par an.', mostPopular: 'Le plus populaire', perMonth: '/mois', billedAs: 'Facturé à', getStarted: 'Commencer', footer: "Toutes les formules incluent un essai gratuit de 90 jours. Aucune carte bancaire n'est requise.", }, ar: { monthly: 'شهري', annual: 'سنوي', yearly: '/ سنة', youSave: 'توفر', withAnnual: 'مع الفوترة السنوية، تُسدد دفعة واحدة كل سنة.', mostPopular: 'الأكثر شيوعاً', perMonth: '/شهر', billedAs: 'يتم الفوترة بمبلغ', getStarted: 'ابدأ الآن', footer: 'تشمل جميع الخطط تجربة مجانية لمدة 14 يوماً. لا حاجة إلى بطاقة ائتمان.', }, } as const function getPlanPrice(prices: PricingMatrix, plan: string, billingPeriod: 'MONTHLY' | 'ANNUAL') { return (prices[plan]?.[billingPeriod]?.MAD ?? PLAN_PRICES[plan]?.[billingPeriod]?.MAD ?? 0) / 100 } function annualSavingsPct(prices: PricingMatrix, plan: string): number { const monthly = getPlanPrice(prices, plan, 'MONTHLY') const annual = getPlanPrice(prices, plan, 'ANNUAL') const wouldPay = monthly * 12 if (wouldPay <= 0) return 0 return Math.max(0, Math.round(((wouldPay - annual) / wouldPay) * 100)) } export default function PricingClient({ initialPricing = DEFAULT_PRICING }: { initialPricing?: PlatformPricing }) { const { language } = useMarketplacePreferences() const currencyLabel = getCurrencyLabel(language) const dict = copy[language] const [billing, setBilling] = useState('monthly') const [pricing, setPricing] = useState(initialPricing) const dashboardUrl = resolveBrowserAppUrl(process.env.NEXT_PUBLIC_DASHBOARD_URL ?? 'http://localhost:3000/dashboard') useEffect(() => { marketplaceFetchOrDefault('/site/platform/pricing', DEFAULT_PRICING) .then(setPricing) }, []) const savings = annualSavingsPct(pricing.prices, 'STARTER') return (
{/* Billing toggle */}
{(['monthly', 'annual'] as Billing[]).map((b) => ( ))}
{billing === 'annual' && (

{dict.youSave} {savings}% {dict.withAnnual}

)} {/* Plan cards */}
{PLANS.map((plan) => { const monthlyPrice = getPlanPrice(pricing.prices, plan.key, 'MONTHLY') const annualPrice = getPlanPrice(pricing.prices, plan.key, 'ANNUAL') const displayPrice = billing === 'annual' ? Math.round(annualPrice / 12) : monthlyPrice const features = pricing.planFeatures[plan.key] ?? PLAN_FEATURES[plan.key] ?? [] return (
{plan.highlight && (
{dict.mostPopular}
)}

{plan.name}

{plan.tagline}

{displayPrice} {currencyLabel} {dict.perMonth}
{billing === 'annual' && (

{dict.billedAs} {annualPrice} {currencyLabel} {dict.yearly}

)}
    {features.map((f) => (
  • {f}
  • ))}
{dict.getStarted}
) })}
{/* Footer note */}

{dict.footer}

) }