/**
 * User Management Service (Account API)
 * Handles account-related API calls. Authenticated user required.
 * Routes: GET list, GET by uuid (detail), POST add, PATCH update, DELETE delete, PATCH profile.
 */

import { apiClient } from '../client'

export interface User {
  id: number
  username: string
  email: string
  first_name: string
  last_name: string
  phone_number: string
  role?: string
  status?: string
  avatar?: string
  fullName?: string
  currentPlan?: string
  billing?: string
  company?: string
  country?: string
  contact?: string
}

export interface UserDetails {
  id: number
  username: string
  email: string
  first_name: string
  last_name: string
  phone_number: string
  role?: string
  status?: string
  avatar?: string
}

export interface Account {
  id: number
  username: string
  email: string
  first_name: string
  last_name: string
  phone_number: string
}

export interface ApiResponse<T> {
  code: number
  msg: string
  data: T
}

export interface UpdateProfileRequest {
  first_name?: string
  last_name?: string
  email?: string
}

export interface UpdateProfileResponse {
  id: number
  username: string
  email: string
  first_name: string
  last_name: string
  phone_number: string
}

export interface AddAccountRequest {
  first_name: string
  last_name: string
  phones: string[]
}

export interface UpdateAccountRequest {
  first_name?: string
  last_name?: string
  phones?: string[]
}

export const userManagementService = {
  /**
   * Get list of users
   */
  async getUsers(): Promise<{ users: User[]; stats: { total: number; active: number; pending: number; inactive: number } }> {
    const response = await apiClient.get<ApiResponse<{ users: User[]; stats: { total: number; active: number; pending: number; inactive: number } }>>('/api/account/')

    return response.data as { users: User[]; stats: { total: number; active: number; pending: number; inactive: number } }
  },

  /**
   * Get a single user by ID
   */
  async getUser(id: number): Promise<UserDetails> {
    const response = await apiClient.get<ApiResponse<UserDetails>>(`/api/account/${id}/`)

    return response.data as unknown as UserDetails
  },

  /**
   * Delete a user by ID
   */
  async deleteUser(id: number): Promise<void> {
    await apiClient.delete<ApiResponse<unknown>>(`/api/account/${id}/`)
  },

  /**
   * Update current user profile (first_name, last_name, email)
   */
  async updateProfile(data: UpdateProfileRequest): Promise<UpdateProfileResponse> {
    const response = await apiClient.patch<ApiResponse<UpdateProfileResponse>>('/api/account/profile/', data)
    return response.data
  },

  /**
   * Get list of accounts
   */
  async getAccounts(): Promise<ApiResponse<unknown>['data']> {
    const response = await apiClient.get<ApiResponse<unknown>>('/api/account/')
    return response.data
  },

  /**
   * Get one account by uuid
   */
  async getAccount(uuid: string): Promise<unknown> {
    const response = await apiClient.get<ApiResponse<unknown>>(`/api/account/${uuid}/`)
    return response.data
  },

  /**
   * Add a new account
   */
  async addAccount(data: AddAccountRequest): Promise<void> {
    await apiClient.post<ApiResponse<unknown>>('/api/account/', data)
  },

  /**
   * Update account by uuid
   */
  async updateAccount(uuid: string, data: UpdateAccountRequest): Promise<void> {
    await apiClient.patch<ApiResponse<unknown>>(`/api/account/${uuid}/`, data)
  },

  /**
   * Delete account by uuid
   */
  async deleteAccount(uuid: string): Promise<void> {
    await apiClient.delete<ApiResponse<unknown>>(`/api/account/${uuid}/`)
  }
}
