import { supabase } from './supabase'; import type { AgendaRow, CheckinResult, Connection, Feedback, FeedbackResult, LeaderboardRow, PointsLogRow, Preference, PublicProfile, Session, Slot, } from './types'; export class ApiError extends Error {} async function rpc(fn: string, args: Record): Promise { const { data, error } = await supabase.rpc(fn, args); if (error) throw new ApiError(error.message); return data as T; } /** Resolve a scanned lanyard QR token to a public profile (pre-login + connect). */ export async function lanyardLookup(qrToken: string): Promise { const rows = await rpc('lanyard_lookup', { p_qr_token: qrToken }); return rows && rows.length > 0 ? rows[0] : null; } export async function getPublicProfiles(): Promise { return rpc('get_public_profiles', {}); } export async function getAgenda(): Promise { return rpc('get_agenda', {}); } /** Group the flat agenda rows into slots with their sessions. */ export function groupAgenda(rows: AgendaRow[]): { slot: Slot; sessions: Session[] }[] { const map = new Map(); for (const r of rows) { let entry = map.get(r.slot_id); if (!entry) { entry = { slot: { id: r.slot_id, name: r.slot_name, slot_date: r.slot_date, start_time: r.start_time, end_time: r.end_time }, sessions: [], }; map.set(r.slot_id, entry); } if (r.session_id && r.title) { entry.sessions.push({ id: r.session_id, title: r.title, description: r.description, slot_id: r.slot_id, room: r.room, capacity: r.capacity, speakers: r.speakers, }); } } return [...map.values()].sort((a, b) => a.slot.id - b.slot.id); } export async function getMyPreferences(): Promise { const { data, error } = await supabase.from('preferences').select('*').order('slot_id'); if (error) throw new ApiError(error.message); return data as Preference[]; } export async function setPreference(sessionId: string): Promise { await rpc('set_preference', { p_session_id: sessionId }); } export async function clearPreference(slotId: number): Promise { await rpc('clear_preference', { p_slot_id: slotId }); } export async function getMyConnections(): Promise { const { data, error } = await supabase.from('connections').select('*').order('created_at', { ascending: false }); if (error) throw new ApiError(error.message); return data as Connection[]; } export async function createConnectionRequest(targetQr: string): Promise { return rpc('create_connection_request', { p_target_qr: targetQr }); } export async function acceptConnection(id: string): Promise { return rpc('accept_connection', { p_connection_id: id }); } export async function rejectConnection(id: string): Promise { return rpc('reject_connection', { p_connection_id: id }); } export async function getMyCheckins(): Promise { const { data, error } = await supabase.from('checkins').select('session_id'); if (error) throw new ApiError(error.message); return (data as { session_id: string }[]).map((c) => c.session_id); } export async function checkinAttendee(sessionId: string, attendeeQr: string): Promise { const rows = await rpc('checkin_attendee', { p_session_id: sessionId, p_attendee_qr: attendeeQr }); return rows[0]; } export async function getMyFeedback(): Promise { const { data, error } = await supabase.from('feedback').select('*'); if (error) throw new ApiError(error.message); return data as Feedback[]; } export async function submitFeedback(sessionId: string, rating: number, comment: string): Promise { const rows = await rpc('submit_feedback', { p_session_id: sessionId, p_rating: rating, p_comment: comment }); return rows[0]; } export async function getLeaderboard(): Promise { const { data, error } = await supabase.from('leaderboard').select('*').order('points', { ascending: false }); if (error) throw new ApiError(error.message); return data as LeaderboardRow[]; } export async function getMyPointsLog(): Promise { const { data, error } = await supabase.from('points_log').select('*').order('created_at', { ascending: false }); if (error) throw new ApiError(error.message); return data as PointsLogRow[]; }