import type {
  AuthSession,
  ChangePasswordPayload,
  DemoLoginPayload,
  LoginPayload,
  TeamLoginPayload,
  TwitchBindingDisconnectResponse,
  TwitchAuthorizePayload,
  TwitchAuthorizeResponse,
} from '../../types/awards'
import { requestJson } from '../http'
import { jsonRequest } from './requestOptions'

export const authApi = {
  getSession: () => requestJson<AuthSession>('/api/auth/session'),
  login: (payload: LoginPayload) =>
    requestJson<AuthSession>('/api/auth/dev-login', jsonRequest('POST', payload)),
  demoLogin: (payload: DemoLoginPayload) =>
    requestJson<AuthSession>('/api/auth/demo-login', jsonRequest('POST', payload)),
  teamLogin: (payload: TeamLoginPayload) =>
    requestJson<AuthSession>('/api/auth/team-login', jsonRequest('POST', payload)),
  changePassword: (payload: ChangePasswordPayload) =>
    requestJson<AuthSession>('/api/auth/password/change', jsonRequest('POST', payload)),
  startTwitchAuthorization: (payload: TwitchAuthorizePayload) =>
    requestJson<TwitchAuthorizeResponse>('/api/auth/twitch/authorize', jsonRequest('POST', payload)),
  disconnectTwitchBinding: () =>
    requestJson<TwitchBindingDisconnectResponse>('/api/auth/twitch/binding', {
      method: 'DELETE',
    }),
  logout: () =>
    requestJson<{ loggedOut: boolean }>('/api/auth/logout', jsonRequest('POST', {})),
  deleteMyData: () =>
    requestJson<{
      deleted: boolean
      twitchUserId: string
      deletedVoteEntries: number
      deletedBallots: number
      deletedNominations: number
      deletedClips: number
      deletedRiskFlags: number
      disabledSessions: number
    }>('/api/auth/me/data', {
      method: 'DELETE',
    }),
}
