"use client"; import { useEffect, useRef, useState } from "react"; import { useSearchParams } from "next/navigation"; import { useQuery } from "@tanstack/react-query"; import { useTranslations } from "next-intl"; import { useRouter } from "@/i18n/routing"; import { apiGet, apiPost } from "@/lib/api/client"; import { isCafeOwner } from "@/lib/auth-permissions"; import { useAuthStore } from "@/lib/stores/auth.store"; import { formatNumber } from "@/lib/format"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { PageHeader } from "@/components/layout/page-header"; import { PlanComparison } from "@/components/settings/plan-comparison"; import type { AuthTokenResponse } from "@/lib/api/types"; import { Alert } from "@/components/ui/alert"; import { notify } from "@/lib/notify"; type BillingStatus = { planTier: string; planExpiresAt: string | null; ordersToday: number; ordersDailyLimit: number | null; customersCount: number; customersLimit: number | null; smsUsedThisMonth: number; smsMonthlyLimit: number; menu3dEnabled: boolean; discoverProfileEnabled: boolean; isPlanExpired: boolean; }; export function SubscriptionScreen() { const t = useTranslations("subscription"); const tSettings = useTranslations("settings"); const searchParams = useSearchParams(); const router = useRouter(); const cafeId = useAuthStore((s) => s.user?.cafeId); const role = useAuthStore((s) => s.user?.role); const setAuth = useAuthStore((s) => s.setAuth); const billingRefreshed = useRef(false); const [billingBanner, setBillingBanner] = useState<"success" | "failed" | null>(null); useEffect(() => { const billing = searchParams.get("billing"); if (billing === "success") { notify.success(t("paymentSuccess")); setBillingBanner("success"); } if (billing === "failed") { notify.error(t("paymentFailed")); setBillingBanner("failed"); } }, [searchParams, t]); const { data: status, isLoading, refetch } = useQuery({ queryKey: ["billing-status", cafeId], queryFn: () => apiGet("/api/billing/status"), enabled: !!cafeId, }); useEffect(() => { if (searchParams.get("billing") !== "success" || billingRefreshed.current) return; const refresh = localStorage.getItem("meezi_refresh_token"); if (!refresh) return; billingRefreshed.current = true; apiPost("/api/auth/refresh", { refreshToken: refresh }) .then((tokens) => { setAuth(tokens); refetch(); }) .catch(() => notify.warning(tSettings("profile.reloginHint"))); }, [searchParams, setAuth, refetch, tSettings]); if (!cafeId) return null; if (!isCafeOwner(role)) { return (

{t("ownerOnly")}

); } const expiry = status?.planExpiresAt ? new Date(status.planExpiresAt).toLocaleDateString("fa-IR") : t("noExpiry"); return (
{billingBanner ? ( setBillingBanner(null)} > {billingBanner === "failed" ? t("paymentFailed") : t("paymentSuccess")} ) : null} {t("currentPlan")} {isLoading ? (

{t("loading")}

) : status ? ( <>
{status.planTier} {status.isPlanExpired ? ( {t("planExpired")} ) : null} {t("expires")}: {expiry}
  • {t("ordersToday")}: {formatNumber(status.ordersToday)} {status.ordersDailyLimit != null && ` / ${formatNumber(status.ordersDailyLimit)}`}
  • {t("customers")}: {formatNumber(status.customersCount)} {status.customersLimit != null && ` / ${formatNumber(status.customersLimit)}`}
  • {t("smsUsage")}: {formatNumber(status.smsUsedThisMonth)} {status.smsMonthlyLimit >= 0 && ` / ${formatNumber(status.smsMonthlyLimit)}`}
  • {t("featureMenu3d")}:{" "} {status.menu3dEnabled ? t("featureOn") : t("featureOff")}
  • {t("featureDiscover")}:{" "} {status.discoverProfileEnabled ? t("featureOn") : t("featureOff")}
) : null}
router.push(`/subscription/checkout?plan=${planTier}`) } />
); }