import type { SupabaseClient } from '@supabase/supabase-js'; export const SAJU_DAILY_LIMIT = 1; export const TAROT_DAILY_LIMIT = 3; export const MUSIC_DAILY_LIMIT = 1; export type AiService = 'saju' | 'tarot' | 'music'; /** KST(UTC+9) 자정을 UTC ISO로. 오늘 사용량 집계 하한. */ export function kstDayStartISO(now: Date): string { const kstMs = now.getTime() + 9 * 60 * 60 * 1000; const kst = new Date(kstMs); const kstMidnightUtcMs = Date.UTC(kst.getUTCFullYear(), kst.getUTCMonth(), kst.getUTCDate()) - 9 * 60 * 60 * 1000; return new Date(kstMidnightUtcMs).toISOString(); } export async function getTodayUsage(admin: SupabaseClient, userId: string, service: AiService): Promise { const since = kstDayStartISO(new Date()); const { count } = await admin .from('ai_usage_log') .select('id', { count: 'exact', head: true }) .eq('user_id', userId) .eq('service', service) .gte('created_at', since); return count ?? 0; } export async function recordUsage(admin: SupabaseClient, userId: string, service: AiService): Promise { await admin.from('ai_usage_log').insert({ user_id: userId, service }); }