import Link from "next/link" import { fetchQuery } from "convex/nextjs" import { Button } from "@/components/ui/button" import { api } from "@/convex/_generated/api" import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card" import { Badge } from "@/components/ui/badge" import { ShoppingCart, Package, Users, TrendingUp, DollarSign, Clock, CheckCircle, Truck, AlertTriangle, Settings, BarChart3, Phone, MessageSquare, ContactRound, } from "lucide-react" import { fetchAllProducts } from "@/lib/stripe/products" // Mock analytics data for demo const mockAnalytics = { totalOrders: 156, totalRevenue: 48567.89, pendingOrders: 12, completedOrders: 144, lowStockProducts: 3, avgOrderValue: 311.46, conversionRate: 2.8, monthlyGrowth: 15.2, } async function getProductsCount() { try { const products = await fetchAllProducts() return products.length } catch { return 0 } } async function getOrdersCount() { try { const response = await fetch("/api/orders") if (response.ok) { const data = await response.json() return data.pagination.total || 0 } } catch {} return mockAnalytics.totalOrders } function formatTimestamp(value?: number | null) { if (!value) { return "—" } return new Date(value).toLocaleString("en-US", { month: "short", day: "numeric", year: "numeric", hour: "2-digit", minute: "2-digit", }) } export default async function AdminDashboard() { const [productsCount, ordersCount, sync] = await Promise.all([ getProductsCount(), getOrdersCount(), fetchQuery(api.crm.getAdminSyncOverview, {}), ]) const dashboardCards = [ { title: "Total Revenue", value: `$${mockAnalytics.totalRevenue.toLocaleString()}`, description: "Total revenue from all orders", icon: DollarSign, trend: "+15.2%", trendPositive: true, color: "text-green-600", }, { title: "Total Orders", value: mockAnalytics.totalOrders.toString(), description: "Total number of orders", icon: ShoppingCart, trend: "+12.8%", trendPositive: true, color: "text-blue-600", }, { title: "Products", value: productsCount.toString(), description: "Active products in inventory", icon: Package, trend: "+5", trendPositive: true, color: "text-purple-600", }, { title: "Pending Orders", value: mockAnalytics.pendingOrders.toString(), description: "Orders awaiting processing", icon: Clock, trend: "-3", trendPositive: false, color: "text-orange-600", }, ] const quickStats = [ { title: "Average Order Value", value: `$${mockAnalytics.avgOrderValue.toFixed(2)}`, description: "Average value per order", icon: TrendingUp, }, { title: "Conversion Rate", value: `${mockAnalytics.conversionRate}%`, description: "Visitors to orders ratio", icon: Users, }, { title: "Monthly Growth", value: `${mockAnalytics.monthlyGrowth}%`, description: "Revenue growth this month", icon: BarChart3, }, { title: "Low Stock Alert", value: mockAnalytics.lowStockProducts.toString(), description: "Products need restocking", icon: AlertTriangle, }, ] const recentOrders = [ { id: "ORD-001234", customer: "john.doe@email.com", amount: 2799.98, status: "paid", date: "2024-01-15 10:30", }, { id: "ORD-001233", customer: "jane.smith@email.com", amount: 1499.99, status: "fulfilled", date: "2024-01-15 09:45", }, { id: "ORD-001232", customer: "bob.johnson@email.com", amount: 899.97, status: "pending", date: "2024-01-15 08:20", }, { id: "ORD-001231", customer: "alice.wilson@email.com", amount: 3499.99, status: "cancelled", date: "2024-01-14 16:15", }, ] const popularProducts = [ { name: "SEAGA HY900 Vending Machine", orders: 45, revenue: 112499.55, }, { name: "Vending Machine Stand", orders: 38, revenue: 11399.62, }, { name: "Snack Vending Machine Combo", orders: 23, revenue: 45999.77, }, { name: "Drink Vending Machine", orders: 19, revenue: 37999.81, }, ] return (
{/* Header */}

Admin Dashboard

Manage orders, contacts, conversations, and calls

CRM Sync Status {!sync.ghlConfigured ? "Connect GHL to load contacts and conversations." : "Customer data is mirrored here from GHL and your call flows."} {sync.overallStatus} Last sync: {formatTimestamp(sync.latestSyncAt)} {!sync.ghlConfigured ? GHL is not connected. : null} {!sync.syncTokenConfigured ? ( Manual sync endpoint is not configured yet. ) : null} {!sync.livekitConfigured ? ( LiveKit recordings are not connected yet. ) : null} {/* Main Stats */}
{dashboardCards.map((card, index) => { const Icon = card.icon return ( {card.title}
{card.value}
{card.trend} {card.trendPositive ? "↑" : "↓"} from last month

{card.description}

) })}
{/* Quick Stats */}
{quickStats.map((stat, index) => { const Icon = stat.icon return ( {stat.title}
{stat.value}

{stat.description}

) })}
{/* Main Content Grid */}
{/* Recent Orders */} Recent Orders Latest customer orders and their status
{recentOrders.map((order) => (
{order.id}
{order.customer}
{order.date}
${order.amount.toFixed(2)}
{order.status.charAt(0).toUpperCase() + order.status.slice(1)}
))}
{/* Popular Products */} Popular Products Top-selling products this month
{popularProducts.map((product, index) => (
{index + 1}
{product.name}
{product.orders} orders
${product.revenue.toLocaleString()}
${(product.revenue / product.orders).toFixed(2)} avg
))}
{/* Quick Actions */} Quick Actions Common administrative tasks and operations

Manage Orders

View and process customer orders

Manage Products

Add and update product inventory

Track Shipments

Monitor order shipments and deliveries

Reports

Generate sales and analytics reports

) } export const metadata = { title: "Admin Dashboard | Rocky Mountain Vending", description: "Administrative dashboard for managing your vending machine business", }