// frontend/src/api/model/user.js
// User API model for searching users by username
// Structure matches other model files (see order.js for reference)

import Client from '../Client';

const API_BASE_URL = "/account/users";

const userAPI = {
  /**
   * Get current authenticated user
   * @returns {Promise<User>}
   */
  async getCurrentUser() {
    const response = await Client.auth.get(`${API_BASE_URL}/me/`);
    return response.data;
  },

  /**
   * Get all users
   * @returns {Promise<Array<User>>}
   */
  async getUsers(params: Object) {
    const response = await Client.auth.get(`${API_BASE_URL}/`, { params });
    return response.data;
  },

  /**
   * Get a single user by ID
   * @param {number|string} id
   * @returns {Promise<User>}
   */
  async getUser(id: number) {
    const response = await Client.auth.get(`${API_BASE_URL}/${id}/`);
    return response.data;
  },

  /**
   * Update a user by ID
   * @param {number|string} id
   * @param {Object} data
   * @returns {Promise<User>}
   */
  async updateUser(id: number, data: Object) {
    const response = await Client.auth.patch(`${API_BASE_URL}/${id}/`, data);
    return response.data;
  },

  /**
   * Delete a user by ID
   * @param {number|string} id
   * @returns {Promise<void>}
   */
  async deleteUser(id: number) {
    const response = await Client.auth.delete(`${API_BASE_URL}/${id}/`);
    return response.data;
  },

  /**
   * Create a new user
   * @param {Object} data
   * @returns {Promise<User>}
   */
  async createUser(data: Object) {
    const response = await Client.auth.post(`${API_BASE_URL}/`, data);
    return response.data;
  },

  /**
   * Search users by username (partial match)
   * @param {Object} params - { username: string }
   * @returns {Promise<Array<User>>}
   */
  async searchUsers(params: { username: string }) {
    // Adjust the endpoint as needed for your backend
    const response = await Client.auth.get(`${API_BASE_URL}/`, { params });
    console.log("User search response:", response.data);
    return response.data;
  },
};

export default userAPI;
