import { apiClient } from './apiClient';
import type { AuthUser, LoginResponse, MfaSetupResponse, MfaVerifyResponse } from './types';

export const authApi = {
  login: async (username: string, password: string): Promise<LoginResponse> => {
    return apiClient.post('/v1/auth/login', { username, password });
  },

  verifyMfa: async (mfaSessionToken: string, totpCode: string): Promise<LoginResponse> => {
    return apiClient.post('/v1/auth/mfa/verify', { 
      mfa_session_token: mfaSessionToken, 
      totp_code: totpCode 
    });
  },

  setupMfa: async (token: string): Promise<MfaSetupResponse> => {
    return apiClient.post('/v1/auth/mfa/totp/setup', {}, { token });
  },

  verifyTotpSetup: async (token: string, totpCode: string): Promise<MfaVerifyResponse> => {
    return apiClient.post('/v1/auth/mfa/totp/verify', { totp_code: totpCode }, { token });
  },

  getAuditLogs: async (token: string, limit = 20, offset = 0) => {
    return apiClient.get(`/v1/auth/audit-logs?limit=${limit}&offset=${offset}`, { token });
  },

  registerUser: async (userData: any, token: string) => {
    return apiClient.post('/v1/auth/users', userData, { token });
  },

  getUsers: async (token: string) => {
    return apiClient.get('/v1/auth/users', { token });
  },

  updateUser: async (userId: string, userData: any, token: string) => {
    return apiClient.put(`/v1/auth/users/${userId}`, userData, { token });
  },

  disableMfa: async (password: string, token: string) => {
    return apiClient.post('/v1/auth/mfa/disable', { password }, { token });
  },

  getDepartments: async (token: string) => {
    return apiClient.get('/v1/hospital/departments', { token });
  },

  createDepartment: async (deptData: any, token: string) => {
    return apiClient.post('/v1/hospital/departments', deptData, { token });
  },

  getRoles: async (token: string) => {
    return apiClient.get('/v1/auth/roles', { token });
  }
};
