import apiClient from './client'

export interface AuthUserResponse {
  id: number
  name: string | null
  email: string
  is_admin: boolean | null
  has_write_access: boolean | null
  is_active: boolean | null
  role: string | null
  created_at: string | null
  updated_at: string | null
}

export interface AuthUserCreateRequest {
  email: string
  name: string
  password: string
  is_admin: boolean
  has_write_access: boolean
}

export interface AuthUserUpdateRequest {
  email: string
  name: string
  is_admin: boolean
  has_write_access: boolean
  is_active?: boolean
  role?: string
  password?: string
}

export interface AuthUsersListResponse {
  items: AuthUserResponse[]
  total: number
}

export const authUsersApi = {
  listUsers: async (): Promise<AuthUsersListResponse> => {
    const { data } = await apiClient.get<AuthUsersListResponse>('/api/v1/auth-users')
    return data
  },

  getUser: async (userId: number): Promise<AuthUserResponse> => {
    const { data } = await apiClient.get<AuthUserResponse>(`/api/v1/auth-users/${userId}`)
    return data
  },

  createUser: async (request: AuthUserCreateRequest): Promise<AuthUserResponse> => {
    const { data } = await apiClient.post<AuthUserResponse>('/api/v1/auth-users', request)
    return data
  },

  updateUser: async (
    userId: number,
    request: AuthUserUpdateRequest
  ): Promise<AuthUserResponse> => {
    const { data } = await apiClient.put<AuthUserResponse>(
      `/api/v1/auth-users/${userId}`,
      request
    )
    return data
  },

  deleteUser: async (userId: number): Promise<{ message?: string; deactivated?: boolean }> => {
    const response = await apiClient.delete(`/api/v1/auth-users/${userId}`)
    // Return data if present (200 OK with deactivation message), otherwise empty object (204 No Content)
    return response.data || {}
  },
}


