import Link from "next/link"; import { api } from "@/lib/api"; import { getAdminUser } from "@/lib/admin-permissions"; import { getActiveBrandId } from "@/lib/brand-scope"; import { isFeatureEnabled } from "@/lib/feature-flags"; import { getBillingOverview } from "@/actions/billing/billing-overview"; import DashboardClient from "@/components/admin/DashboardClient"; const TUXEDO_BRAND_ID = "64294306-5f42-463d-a5e8-2ad6c81a96de"; export default async function AdminPage() { const adminUser = await getAdminUser(); const isStoreEmployee = adminUser?.role === "store_employee"; const isWaterLogVisible = adminUser?.role === "platform_admin" || (adminUser?.role === "brand_admin" && adminUser?.brand_id === TUXEDO_BRAND_ID && adminUser?.can_manage_water_log); let enabledAddons: Record = {}; let planTier = "starter"; let usage = { users: 0, stops_this_month: 0, products: 0 }; let limits = { max_users: 1, max_stops_monthly: 10, max_products: 25 }; let brandDisplayName = "Admin"; // Resolve active brand via the canonical resolver (URL > cookie > legacy brand_id // > first of brand_ids). For platform_admin in dev mode this is null, so we // fall back to the first brand in the brands table to keep the dashboard's // "Active Products" stat in sync with the billing page. let dashboardBrandId: string | null = adminUser ? await getActiveBrandId(adminUser) : null; if (!dashboardBrandId && adminUser?.role === "platform_admin") { const { data: firstBrand } = await api .from("brands") .select("id") .limit(1) .single(); if (firstBrand?.id) { dashboardBrandId = firstBrand.id; } } if (dashboardBrandId) { // Use the centralized billing overview so the dashboard's "Active Products" // stat agrees with the plan usage in /admin/settings/billing. Previously // these were derived from two different queries with different filters // (active=true vs deleted_at IS NULL), causing the "1 vs 0/25" mismatch. const overviewRes = await getBillingOverview(dashboardBrandId); if (overviewRes.success && overviewRes.data) { const o = overviewRes.data; planTier = o.planTier; usage = o.usage; limits = o.limits; if (o.brandName) brandDisplayName = o.brandName; enabledAddons = o.enabledAddons; } else { // Fallback to per-feature flag check (matches prior behavior) for (const key of [ "harvest_reach", "wholesale_portal", "water_log", "ai_tools", "sms_campaigns", "square_sync", "route_trace", ] as const) { enabledAddons[key] = await isFeatureEnabled(dashboardBrandId, key); } } } if (isStoreEmployee) { return (

Driver Pickup

Use the buttons below to look up orders, record pickups, and manage fulfillment.

Pickup Lookup

Scan QR or search orders

Wholesale Portal

Approved buyer orders

); } return ( ); }