import { supabase } from '../../lib/supabase' export interface DashboardRecentEntry { id: string title: string | null text: string durationSeconds: number mode: string createdAt: string } export interface MobileDashboardStats { totalSessions: number totalRecordingSeconds: number totalWordCount: number todaySessions: number todayRecordingSeconds: number todayWordCount: number streakDays: number recentHistory: DashboardRecentEntry[] generatedAt: string } export class DashboardServiceError extends Error { constructor(readonly code: 'auth' | 'invalid-response' | 'server') { super(code) this.name = 'DashboardServiceError' } } function isRecord(value: unknown): value is Record { return typeof value === 'object' && value !== null && !Array.isArray(value) } function nonNegativeNumber(value: unknown): number | null { const numeric = typeof value === 'number' ? value : Number(value) return Number.isFinite(numeric) && numeric >= 0 ? numeric : null } function nonNegativeInteger(value: unknown): number | null { const numeric = nonNegativeNumber(value) return numeric !== null && Number.isSafeInteger(numeric) ? numeric : null } export function normalizeDashboardStats(value: unknown): MobileDashboardStats { if (!isRecord(value) || !Array.isArray(value.recent_history)) { throw new DashboardServiceError('invalid-response') } const totalSessions = nonNegativeInteger(value.total_sessions) const totalRecordingSeconds = nonNegativeNumber(value.total_recording_seconds) const totalWordCount = nonNegativeInteger(value.total_word_count) const todaySessions = nonNegativeInteger(value.today_sessions) const todayRecordingSeconds = nonNegativeNumber(value.today_recording_seconds) const todayWordCount = nonNegativeInteger(value.today_word_count) const streakDays = nonNegativeInteger(value.streak_days) const generatedAt = typeof value.generated_at === 'string' && Number.isFinite(Date.parse(value.generated_at)) ? new Date(value.generated_at).toISOString() : null if ( totalSessions === null || totalRecordingSeconds === null || totalWordCount === null || todaySessions === null || todayRecordingSeconds === null || todayWordCount === null || streakDays === null || generatedAt === null ) throw new DashboardServiceError('invalid-response') const recentHistory = value.recent_history.map((entry): DashboardRecentEntry => { if (!isRecord(entry)) throw new DashboardServiceError('invalid-response') const durationSeconds = nonNegativeNumber(entry.duration_seconds) if ( typeof entry.id !== 'string' || (entry.title !== null && typeof entry.title !== 'string') || typeof entry.text !== 'string' || typeof entry.mode !== 'string' || typeof entry.created_at !== 'string' || !Number.isFinite(Date.parse(entry.created_at)) || durationSeconds === null ) throw new DashboardServiceError('invalid-response') return { id: entry.id, title: entry.title, text: entry.text, durationSeconds, mode: entry.mode, createdAt: new Date(entry.created_at).toISOString(), } }) return { totalSessions, totalRecordingSeconds, totalWordCount, todaySessions, todayRecordingSeconds, todayWordCount, streakDays, recentHistory, generatedAt, } } export async function loadDashboardStats(userId: string): Promise { if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(userId)) { throw new DashboardServiceError('auth') } const { data, error } = await supabase.rpc('mobile_dashboard_stats') if (error) throw new DashboardServiceError('server') return normalizeDashboardStats(data) }