Build a real-time Admin Analytics Dashboard at /admin/analytics in Daryle AI that replaces all dummy metrics with live data from Supabase.
Context
- Existing placeholder cards and charts on /admin/analytics need to show actual counts and trends from our chats, profiles, and related tables.
- Only superadmins may access this page.
Task
Fetch and display these metrics via Supabase JS calls (or RPCs) and bind them into the existing summary cards and chart components:
- Total Chats – exact count of rows in chats.
- Unique Users – distinct count of user_id in chats.
- Average Chat Length – average number of messages per chat (can use an RPC or calculate client-side).
- Returning Users % – percentage of users with more than one chat.
- Chat Volume Over Time – line chart grouped by day/week from created_at.
- Mode Usage Breakdown – donut/bar chart showing counts per mode field in chats.
- Messages Per Session Distribution – histogram of jsonb_array_length(messages) per chat.
Functional Requirements
- Use Supabase .select() with { count: "exact" }, .rpc(), or .from().group() queries.
- Show a loading spinner while data fetches, and an error message on failure.
- Bind results into existing <SummaryCards> and <ChartSection> components.
Constraints
- Respect RLS: ensure only superadmins can query these aggregates.
- Do not hard-code any numbers.
- Retain current layout, Tailwind styling, and chart libraries.
Output Files
- pages/admin/analytics.tsx – add live data fetching and pass props to cards/charts
- utils/analyticsQueries.ts (optional) – encapsulate Supabase calls or RPC wrappers
- Supabase migration or RPC definitions if needed for averages or complex grouping
Make it copy-and-paste ready for Lovable’s prompt input.