import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import {
  DataPermissionTypeVO,
  DataPermissionValueVO,
  UserDataPermissionVO,
  UserDataPermissionForm,
  DataPermissionQuery,
  DataPermissionValueQuery
} from './types';

/**
 * 查询数据权限类型列表
 */
export const listDataPermissionTypes = (): AxiosPromise<DataPermissionTypeVO[]> => {
  return request({
    url: '/system/dataPermission/types',
    method: 'get'
  });
};

/**
 * 根据权限类型ID查询权限值列表
 * @param typeId 权限类型ID
 */
export const listDataPermissionValues = (typeId: number): AxiosPromise<DataPermissionValueVO[]> => {
  return request({
    url: `/system/dataPermission/values/${typeId}`,
    method: 'get'
  });
};

/**
 * 查询权限值列表（支持分页和查询）
 * @param query 查询参数
 */
export const listDataPermissionValuesWithQuery = (query: DataPermissionValueQuery): AxiosPromise<DataPermissionValueVO[]> => {
  return request({
    url: '/system/dataPermission/values',
    method: 'get',
    params: query
  });
};

/**
 * 获取用户数据权限配置
 * @param userId 用户ID
 */
export const getUserDataPermissions = (userId: string | number): AxiosPromise<UserDataPermissionVO[]> => {
  return request({
    url: `/system/dataPermission/user/${userId}`,
    method: 'get'
  });
};

/**
 * 保存用户数据权限配置
 * @param userId 用户ID
 * @param data 权限配置数据
 */
export const saveUserDataPermissions = (userId: string | number, data: UserDataPermissionForm): AxiosPromise<void> => {
  return request({
    url: `/system/dataPermission/user/${userId}`,
    method: 'post',
    data: data
  });
};

/**
 * 批量保存用户数据权限配置
 * @param userIds 用户ID列表
 * @param data 权限配置数据
 */
export const batchSaveUserDataPermissions = (userIds: (string | number)[], data: UserDataPermissionForm): AxiosPromise<void> => {
  return request({
    url: '/system/dataPermission/users/batch',
    method: 'post',
    data: {
      userIds,
      permissions: data
    }
  });
};

/**
 * 清空用户指定类型的数据权限
 * @param userId 用户ID
 * @param typeId 权限类型ID
 */
export const clearUserDataPermission = (userId: string | number, typeId: number): AxiosPromise<void> => {
  return request({
    url: `/system/dataPermission/user/${userId}/type/${typeId}`,
    method: 'delete'
  });
};

/**
 * 复制用户数据权限到其他用户
 * @param fromUserId 源用户ID
 * @param toUserIds 目标用户ID列表
 */
export const copyUserDataPermissions = (fromUserId: string | number, toUserIds: (string | number)[]): AxiosPromise<void> => {
  return request({
    url: `/system/dataPermission/copy/${fromUserId}`,
    method: 'post',
    data: { toUserIds }
  });
};

/**
 * 根据角色获取数据权限模板
 * @param roleId 角色ID
 */
export const getDataPermissionTemplate = (roleId: string | number): AxiosPromise<UserDataPermissionVO[]> => {
  return request({
    url: `/system/dataPermission/template/${roleId}`,
    method: 'get'
  });
};

/**
 * 验证用户数据权限
 * @param userId 用户ID
 * @param typeId 权限类型ID
 * @param valueCode 权限值编码
 */
export const validateUserDataPermission = (
  userId: string | number,
  typeId: number,
  valueCode: string
): AxiosPromise<boolean> => {
  return request({
    url: '/system/dataPermission/validate',
    method: 'get',
    params: { userId, typeId, valueCode }
  });
};

// ==================== 管理端接口 ====================

/**
 * 新增数据权限类型
 * @param data 权限类型数据
 */
export const addDataPermissionType = (data: Partial<DataPermissionTypeVO>): AxiosPromise<void> => {
  return request({
    url: '/system/dataPermission/type',
    method: 'post',
    data: data
  });
};

/**
 * 修改数据权限类型
 * @param data 权限类型数据
 */
export const updateDataPermissionType = (data: DataPermissionTypeVO): AxiosPromise<void> => {
  return request({
    url: '/system/dataPermission/type',
    method: 'put',
    data: data
  });
};

/**
 * 删除数据权限类型
 * @param typeId 权限类型ID
 */
export const deleteDataPermissionType = (typeId: number): AxiosPromise<void> => {
  return request({
    url: `/system/dataPermission/type/${typeId}`,
    method: 'delete'
  });
};

/**
 * 新增数据权限值
 * @param data 权限值数据
 */
export const addDataPermissionValue = (data: Partial<DataPermissionValueVO>): AxiosPromise<void> => {
  return request({
    url: '/system/dataPermission/value',
    method: 'post',
    data: data
  });
};

/**
 * 修改数据权限值
 * @param data 权限值数据
 */
export const updateDataPermissionValue = (data: DataPermissionValueVO): AxiosPromise<void> => {
  return request({
    url: '/system/dataPermission/value',
    method: 'put',
    data: data
  });
};

/**
 * 删除数据权限值
 * @param valueId 权限值ID
 */
export const deleteDataPermissionValue = (valueId: number): AxiosPromise<void> => {
  return request({
    url: `/system/dataPermission/value/${valueId}`,
    method: 'delete'
  });
};

export default {
  listDataPermissionTypes,
  listDataPermissionValues,
  listDataPermissionValuesWithQuery,
  getUserDataPermissions,
  saveUserDataPermissions,
  batchSaveUserDataPermissions,
  clearUserDataPermission,
  copyUserDataPermissions,
  getDataPermissionTemplate,
  validateUserDataPermission,
  addDataPermissionType,
  updateDataPermissionType,
  deleteDataPermissionType,
  addDataPermissionValue,
  updateDataPermissionValue,
  deleteDataPermissionValue
};
