import { supabase } from '@lenserfight/data/supabase'

export interface WaitingListRepositoryPort {
  getIsInWaitingList(): Promise<boolean | null>
  toggleWaitingList(kvkkApproved: boolean): Promise<boolean>
}

/**
 * ============================
 * Supabase Implementation
 * ============================
 * Information Expert
 */
export class SupabaseWaitingListRepository
  implements WaitingListRepositoryPort {
  async getIsInWaitingList(): Promise<boolean | null> {
    const { data, error } = await supabase.rpc(
      'fn_lensers_get_is_in_waitinglist'
    )

    if (error) throw error
    return data as boolean | null
  }

  async toggleWaitingList(kvkkApproved: boolean): Promise<boolean> {
    const { error } = await supabase.rpc(
      'fn_lensers_toggle_waitinglist',
      { p_kvkk_approved: kvkkApproved }
    )

    if (error) {
      switch (error.message) {
        case 'kvkk_not_approved':
          throw new Error('KVKK approval is required.')
        case 'not_authenticated':
          throw new Error('You must be logged in.')
        case 'not_a_lenser':
          throw new Error('Only lensers can join the waiting list.')
        default:
          throw new Error(error.message)
      }
    }

    return true
  }
}
