/**
 * @description 用户相关API
 * @author yslg
 * @since 2025-10-06
 */

import { api } from '@/apis/index';
import type { SysUser, SysUserInfo, UserVO, UserDeptRoleVO, SysUserDeptRole, ResultDomain, PageParam } from '@/types';
import { useStore } from 'vuex';

const store = useStore();

/**
 * 用户API服务
 */
export const userApi = {
  /**
   * 更新用户详细信息
   * @param userInfo 用户详细信息
   * @returns Promise<ResultDomain<boolean>>
   */
  async updateUserInfo(userInfo: SysUserInfo): Promise<ResultDomain<boolean>> {
    const response = await api.put<boolean>('/users/info', userInfo);
    return response.data;
  },

  /**
   * 修改密码
   * @param oldPassword 旧密码
   * @param newPassword 新密码
   * @returns Promise<ResultDomain<boolean>>
   */
  async changePassword(oldPassword: string, newPassword: string): Promise<ResultDomain<boolean>> {
    const response = await api.post<boolean>('/users/change-password', {
      oldPassword,
      newPassword
    });
    return response.data;
  },

  /**
   * 获取用户列表
   * @param filter 过滤条件
   * @returns Promise<ResultDomain<SysUser>>
   */
  async getUserList(filter: SysUser): Promise<ResultDomain<SysUser>> {
    const response = await api.post<SysUser>('/users/list', filter);
    return response.data;
  },

  /**
   * 获取用户分页列表
   * @param pageParam 分页参数
   * @param filter 过滤条件
   * @returns Promise<ResultDomain<UserVO>>
   */
  async getUserPage(pageParam: PageParam, filter?: any): Promise<ResultDomain<UserVO>> {
    const response = await api.post<UserVO>('/users/page', {
      pageParam,
      filter
    });
    return response.data;
  },

  /**
   * 创建用户
   * @param user 用户信息
   * @returns Promise<ResultDomain<SysUser>>
   */
  async createUser(user: SysUser): Promise<ResultDomain<SysUser>> {
    const response = await api.post<SysUser>('/users/user', user);
    return response.data;
  },

  /**
   * 更新用户
   * @param user 用户信息
   * @returns Promise<ResultDomain<SysUser>>
   */
  async updateUser(user: SysUser): Promise<ResultDomain<SysUser>> {
    const response = await api.put<SysUser>('/users/user', user);
    return response.data;
  },

  /**
   * 删除用户
   * @param user 用户信息
   * @returns Promise<ResultDomain<SysUser>>
   */
  async deleteUser(user: SysUser): Promise<ResultDomain<SysUser>> {
    const response = await api.delete<SysUser>('/users/user', user);
    return response.data;
  },

  /**
   * 获取用户部门角色绑定信息
   * @param user 用户信息
   * @returns Promise<ResultDomain<SysUserDeptRole>>
   */
  async getUserDeptRole(user: SysUserDeptRole): Promise<ResultDomain<SysUserDeptRole>> {
    const response = await api.post<SysUserDeptRole>('/users/bind/deptrole/list', user);
    return response.data;
  },

  /**
   * 绑定用户部门角色
   * @param userDeptRoleVO 用户部门角色绑定信息
   * @returns Promise<ResultDomain<UserDeptRoleVO>>
   */
  async bindUserDeptRole(userDeptRoleVO: UserDeptRoleVO): Promise<ResultDomain<UserDeptRoleVO>> {
    const response = await api.post<UserDeptRoleVO>('/users/bind/deptrole', userDeptRoleVO);
    return response.data;
  },

  /**
   * 解绑用户部门角色
   * @param userDeptRoleVO 用户部门角色解绑信息
   * @returns Promise<ResultDomain<UserDeptRoleVO>>
   */
  async unbindUserDeptRole(userDeptRoleVO: UserDeptRoleVO): Promise<ResultDomain<UserDeptRoleVO>> {
    const response = await api.post<UserDeptRoleVO>('/users/unbind/deptrole', userDeptRoleVO);
    return response.data;
  },

  /**
   * 批量删除用户
   * @param userIds 用户ID列表
   * @returns Promise<ResultDomain<boolean>>
   */
  async batchDeleteUsers(userIds: string[]): Promise<ResultDomain<boolean>> {
    const response = await api.post<boolean>('/users/batch-delete', { userIds });
    return response.data;
  },

  /**
   * 启用/禁用用户
   * @param userId 用户ID
   * @param status 状态 0-启用 1-禁用
   * @returns Promise<ResultDomain<boolean>>
   */
  async changeUserStatus(userId: string, status: number): Promise<ResultDomain<boolean>> {
    const response = await api.patch<boolean>(`/users/${userId}/status`, { status });
    return response.data;
  },

  /**
   * 重置用户密码
   * @param userId 用户ID
   * @returns Promise<ResultDomain<string>> 返回新密码
   */
  async resetPassword(userId: string): Promise<ResultDomain<string>> {
    const response = await api.post<string>(`/users/${userId}/reset-password`);
    return response.data;
  },

  /**
   * 上传用户头像
   * @param file 文件
   * @returns Promise<ResultDomain<string>> 返回头像URL
   */
  async uploadAvatar(file: File): Promise<ResultDomain<string>> {
    const formData = new FormData();
    formData.append('file', file);
    const response = await api.upload<string>('/users/avatar', formData);
    return response.data;
  }
};

