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

export enum NotificationAction {
  DOCTOR_NEW_JOBS = 'DOCTOR_NEW_JOBS',
  DOCTOR_PATIENT_RESULTS_RECEIVED = 'DOCTOR_PATIENT_RESULTS_RECEIVED',
  PATIENT_DOCTOR_FEEDBACK_RECEIVED = 'PATIENT_DOCTOR_FEEDBACK_RECEIVED',
  PATIENT_ORDER_PROCESSING = 'PATIENT_ORDER_PROCESSING',
  PATIENT_FIRST_RESULTS_RECEIVED = 'PATIENT_FIRST_RESULTS_RECEIVED',
  PATIENT_FULL_RESULTS_RECEIVED = 'PATIENT_FULL_RESULTS_RECEIVED',
  TTO_ORDER_CONFIRMATION = 'TTO_ORDER_CONFIRMATION',
}

export const createNotificationLog = async ({
  action,
  status,
  comment,
  relatedRecordId,
}: {
  action: NotificationAction;
  status: Database['audit']['Enums']['action_status'];
  comment?: string;
  relatedRecordId?: string | number;
}) => {
  try {
    const supabase = getSupabaseServerAdminClient();

    await supabase
      .schema('audit')
      .from('notification_entries')
      .insert({
        action,
        status,
        comment,
        related_record_key: relatedRecordId?.toString(),
      })
      .throwOnError();
  } catch (error) {
    console.error('Failed to insert doctor notification log', error);
  }
};
