import { useEffect } from "react"; import { useNavigate, useParams } from "react-router-dom"; import * as LucideIcons from "lucide-react"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Skeleton } from "@/components/ui/skeleton"; import { ArrowLeft, BookOpen, Clock, Check, Lock, Tag, CalendarDays, } from "lucide-react"; import { useClientTiers } from "@/contexts/ClientTiersProvider"; import { useProfile } from "@/contexts/ProfileProvider"; import { PageMeta } from "@/contexts/MetadataContext"; import { useDateFormat } from "@/hooks/useDateFormat"; import { resolveTierBadge } from "@/utils/tierBadge.util"; import { getTierColor } from "@/utils/tierColors"; import { getBundleContent, overlapsExistingAccess, isPlanCurrent } from "@/utils/planBundle.util"; // ─── Helpers ────────────────────────────────────────────────────────────────── const UNIT_TO_DAYS = { minute: 1 / 1440, hour: 1 / 24, day: 1, month: 30, year: 365 }; function formatDuration(days, unit) { if (!days) return null; const multiplier = UNIT_TO_DAYS[unit] ?? 1; const value = Math.round((days / multiplier) * 1000) / 1000; const label = unit ?? "day"; return `${value} ${label}${value !== 1 ? "s" : ""}`; } function formatCourseDuration(seconds = 0) { if (!seconds) return null; const h = Math.floor(seconds / 3600); const m = Math.floor((seconds % 3600) / 60); if (h && m) return `${h}h ${m}m`; if (h) return `${h}h`; return `${m}m`; } // ─── Skeleton ───────────────────────────────────────────────────────────────── const ViewPlanSkeleton = () => (
); // ─── Component ──────────────────────────────────────────────────────────────── const ViewPlan = () => { const { id } = useParams(); const navigate = useNavigate(); const { myTier, getMyTier, plans, plansLoading, getPlans, tierMap, getTierCategories } = useClientTiers(); const { getProfile } = useProfile(); const { fmtCurrency } = useDateFormat(); useEffect(() => { getProfile(); getMyTier(); getTierCategories(); if (!plans.length) getPlans(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [id]); const plan = plans.find((p) => String(p.plan_id) === String(id)) ?? null; const loading = plansLoading; if (loading) return ; if (!plan) return null; const { label: tierLabel, cls: badgeCls } = resolveTierBadge(plan.tier, tierMap); const Icon = LucideIcons[tierMap[plan.tier]?.badge_icon] ?? Tag; const colors = getTierColor(tierMap[plan.tier]?.color ?? "purple"); const accentSwatch = colors.swatch; const accentStyle = { color: accentSwatch }; const accentBg = colors.panel.bg; const accentBorder = colors.panel.border; const features = plan.features ?? []; const duration = formatDuration(plan.duration_days, plan.duration_unit); // "Current" means the user actually purchased THIS exact plan (matched by // plan_id), not just any plan sharing the same tier slug — two different // plans can share a tier and stay independently active (Tier Plans v2). const isCurrent = isPlanCurrent(plan, myTier); // Blocked-by-overlap ("already covered") — plan isn't owned, but at least // one of its bundle items is already granted by something else the user // holds (Scenario 3): shown, not hidden, but purchase is disabled. const isBlockedByOverlap = !isCurrent && overlapsExistingAccess(plan, myTier?.my_grants); const bundle = getBundleContent(plan); const bundleCount = bundle?.items?.length ?? 0; const BundleIcon = bundle?.icon ?? BookOpen; const totalSeconds = (bundle?.items ?? []).reduce((sum, c) => sum + (c.duration_seconds ?? 0), 0); const totalDuration = formatCourseDuration(totalSeconds); return (
{/* ── Hero Banner ───────────────────────────────────────────── */}
{/* Decorative blobs */}
{tierLabel}

{plan.label}

{plan.description || `Everything you need with the ${tierLabel} plan.`}

{fmtCurrency(plan.price, plan.currency)} {duration && ( / {duration} )}
{isCurrent ? ( Your Current Plan ) : !plan.is_active ? ( Not Available at the Moment ) : isBlockedByOverlap ? ( Already Included in Your Plan ) : null}
{/* ── Benefits ───────────────────────────────────── */} {features.length > 0 && (

Benefits

{features.map(({ text }, i) => (
{text}
))}
)} {/* ── Stats row ─────────────────────────────────────────── */}
{[ { label: bundle ? `${bundle.noun}s` : "Courses", value: bundleCount, icon: BundleIcon }, { label: "Content", value: totalDuration ?? "—", icon: Clock }, { label: "Access", value: duration ?? "Lifetime", icon: CalendarDays }, ].map(({ label, value, icon: StatIcon }) => (

{value}

{label}

))}
{/* ── Included Items ─────────────────────────────────────── */}
Bundle
{bundleCount}
{bundle ? ( bundle.items.map((item) => (

{item.title}

{item.level && ( {item.level} )} {formatCourseDuration(item.duration_seconds) && ( {formatCourseDuration(item.duration_seconds)} )} {item.course_code && ( {item.course_code} )}
)) ) : (

Content coming soon

We're curating the best content for this plan. Subscribe now and get instant access the moment they go live.

)}
{/* ── Bottom CTA ────────────────────────────────────────── */} {!isCurrent && (

{isBlockedByOverlap ? "Already Included" : plan.is_active ? "Ready to get started?" : "Coming Soon"}

{isBlockedByOverlap ? "Your existing access already covers item(s) in this bundle, so it can't be purchased separately." : plan.is_active ? (plan.description || `Everything you need with the ${tierLabel} plan.`) : "This plan is not available for purchase at the moment. Check back later."}

{plan.is_active && !isBlockedByOverlap && ( )}
)}
); }; export default ViewPlan;