import { axiosClient } from '../../../api/axiosClient'

export interface LoginResponseDto {
  accessToken: string
  refreshToken: string
  expiresIn: number
  usuario: {
    id: number
    username: string
    nombre: string
    rol: string
    permisos: string[]
    mustChangePassword: boolean // UDT-008
  }
}

export interface RefreshRequest {
  accessToken: string
  refreshToken: string
}

export interface RefreshResponse {
  accessToken: string
  refreshToken: string
  expiresIn: number
}

export interface LogoutResponse {
  success: boolean
  mensaje: string
}

export async function login(username: string, password: string): Promise<LoginResponseDto> {
  const response = await axiosClient.post<LoginResponseDto>('/api/v1/auth/login', {
    username,
    password,
  })
  return response.data
}

export async function refresh(payload: RefreshRequest): Promise<RefreshResponse> {
  const response = await axiosClient.post<RefreshResponse>('/api/v1/auth/refresh', payload)
  return response.data
}

export async function logout(): Promise<LogoutResponse> {
  const response = await axiosClient.post<LogoutResponse>('/api/v1/auth/logout', {})
  return response.data
}
