/**
 * User Management API
 */
import request from '@/utils/request'

// ==================== Admin APIs ====================

/**
 * Get user list (admin only)
 * @param {Object} params - { page, page_size, search }
 */
export function getUserList (params) {
  return request({
    url: '/api/users/list',
    method: 'get',
    params
  })
}

/**
 * Get user detail (admin only)
 * @param {Number} id - User ID
 */
export function getUserDetail (id) {
  return request({
    url: '/api/users/detail',
    method: 'get',
    params: { id }
  })
}

/**
 * Create new user (admin only)
 * @param {Object} data - { username, password, email, nickname, role }
 */
export function createUser (data) {
  return request({
    url: '/api/users/create',
    method: 'post',
    data
  })
}

/**
 * Update user (admin only)
 * @param {Number} id - User ID
 * @param {Object} data - { email, nickname, role, status }
 */
export function updateUser (id, data) {
  return request({
    url: '/api/users/update',
    method: 'put',
    params: { id },
    data
  })
}

/**
 * Delete user (admin only)
 * @param {Number} id - User ID
 */
export function deleteUser (id) {
  return request({
    url: '/api/users/delete',
    method: 'delete',
    params: { id }
  })
}

/**
 * Reset user password (admin only)
 * @param {Object} data - { user_id, new_password }
 */
export function resetUserPassword (data) {
  return request({
    url: '/api/users/reset-password',
    method: 'post',
    data
  })
}

/**
 * Get available roles
 */
export function getRoles () {
  return request({
    url: '/api/users/roles',
    method: 'get'
  })
}

// ==================== Self-Service APIs ====================

/**
 * Get current user profile
 */
export function getProfile () {
  return request({
    url: '/api/users/profile',
    method: 'get'
  })
}

/**
 * Update current user profile
 * @param {Object} data - { nickname, email, avatar }
 */
export function updateProfile (data) {
  return request({
    url: '/api/users/profile/update',
    method: 'put',
    data
  })
}

/**
 * Change current user password
 * @param {Object} data - { old_password, new_password }
 */
export function changePassword (data) {
  return request({
    url: '/api/users/change-password',
    method: 'post',
    data
  })
}

/**
 * Get current user's credits log
 * @param {Object} params - { page, page_size }
 */
export function getMyCreditsLog (params) {
  return request({
    url: '/api/users/my-credits-log',
    method: 'get',
    params
  })
}

/**
 * Get current user's referral list
 * @param {Object} params - { page, page_size }
 */
export function getMyReferrals (params) {
  return request({
    url: '/api/users/my-referrals',
    method: 'get',
    params
  })
}

// ==================== Billing Management (Admin) ====================

/**
 * Set user credits (admin only)
 * @param {Object} data - { user_id, credits, remark }
 */
export function setUserCredits (data) {
  return request({
    url: '/api/users/set-credits',
    method: 'post',
    data
  })
}

/**
 * Set user VIP status (admin only)
 * @param {Object} data - { user_id, vip_days, vip_expires_at, remark }
 */
export function setUserVip (data) {
  return request({
    url: '/api/users/set-vip',
    method: 'post',
    data
  })
}

/**
 * Get user credits log (admin only)
 * @param {Object} params - { user_id, page, page_size }
 */
export function getUserCreditsLog (params) {
  return request({
    url: '/api/users/credits-log',
    method: 'get',
    params
  })
}
