import { format } from 'date-fns';

import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';

export const getLastCheckedDate = async () => {
  const { data: lastChecked } = await getSupabaseServerAdminClient()
    .schema('audit')
    .from('sync_entries')
    .select('created_at')
    .eq('status', 'SUCCESS')
    .order('created_at')
    .limit(1);
  const lastEntry = lastChecked?.[0];
  const lastCheckedDate = lastEntry
    ? format(lastEntry.created_at, 'yyyy-MM-dd HH:mm:ss')
    : null;

  return lastCheckedDate;
};
