import { requestClient } from '#/api/request';

// 用户相关接口
export interface User {
  id: string;
  username: string;
  realname: string;
  avatar?: string;
  email?: string;
  mobile?: string;
  status: number;
  roles: Role[];
  created_at: string;
  updated_at: string;
}

export interface CreateUserDto {
  username: string;
  realname: string;
  password: string;
  avatar?: string;
  email?: string;
  mobile?: string;
  role_ids: string[];
  status: number;
}

export interface UpdateUserDto {
  realname?: string;
  avatar?: string;
  email?: string;
  mobile?: string;
  role_ids?: string[];
  status?: number;
}

export interface UserListParams {
  page?: number;
  limit?: number;
  username?: string;
  realname?: string;
  status?: number;
  role_id?: string;
}

// 角色相关接口
export interface Role {
  id: string;
  name: string;
  code: string;
  sort: number;
  remark?: string;
  status: number;
  permissions: Permission[];
  created_at: string;
  updated_at: string;
}

export interface CreateRoleDto {
  name: string;
  code: string;
  sort: number;
  remark?: string;
  status: number;
  permission_ids: string[];
}

export interface UpdateRoleDto {
  name?: string;
  code?: string;
  sort?: number;
  remark?: string;
  status?: number;
  permission_ids?: string[];
}

export interface RoleListParams {
  page?: number;
  limit?: number;
  name?: string;
  code?: string;
  status?: number;
}

// 权限相关接口
export interface Permission {
  id: string;
  parent_id?: string;
  name: string;
  code: string;
  type: 'button' | 'menu';
  path?: string;
  component?: string;
  icon?: string;
  sort: number;
  status: number;
  children?: Permission[];
  created_at: string;
  updated_at: string;
}

// 菜单相关接口
export interface Menu {
  id: string;
  parent_id?: string;
  type: 'button' | 'directory' | 'menu';
  name: string;
  title: string;
  path?: string;
  component?: string;
  icon?: string;
  sort: number;
  is_link: boolean;
  link_url?: string;
  is_show: boolean;
  permission?: string;
  status: number;
  children?: Menu[];
  created_at: string;
  updated_at: string;
}

export interface CreateMenuDto {
  parent_id?: string;
  type: 'button' | 'directory' | 'menu';
  name: string;
  title: string;
  path?: string;
  component?: string;
  icon?: string;
  sort: number;
  is_link: boolean;
  link_url?: string;
  is_show: boolean;
  permission?: string;
  status: number;
}

export interface UpdateMenuDto {
  parent_id?: string;
  type?: 'button' | 'directory' | 'menu';
  name?: string;
  title?: string;
  path?: string;
  component?: string;
  icon?: string;
  sort?: number;
  is_link?: boolean;
  link_url?: string;
  is_show?: boolean;
  permission?: string;
  status?: number;
}

export interface MenuListParams {
  name?: string;
  title?: string;
  status?: number;
}

// API 接口定义

// 用户管理接口
export const getUserListApi = (params: UserListParams) => {
  return requestClient.get<{
    limit: number;
    list: User[];
    page: number;
    total: number;
  }>('/api/system/users', { params });
};

export const getUserByIdApi = (id: string) => {
  return requestClient.get<User>(`/api/system/users/${id}`);
};

export const createUserApi = (data: CreateUserDto) => {
  return requestClient.post<User>('/api/system/users', data);
};

export const updateUserApi = (id: string, data: UpdateUserDto) => {
  return requestClient.put<User>(`/api/system/users/${id}`, data);
};

export const deleteUserApi = (id: string) => {
  return requestClient.delete(`/api/system/users/${id}`);
};

export const batchDeleteAdminApi = (ids: string[]) => {
  return requestClient.delete('/admin/user/batch-delete', { data: { ids } });
};

// 管理员相关接口
export interface AdminUser {
  id: string;
  username: string;
  realname: string;
  avatar?: string;
  email?: string;
  mobile?: string;
  status: number;
  roles: Role[];
  created_at: string;
  updated_at: string;
}

export interface CreateAdminParams {
  username: string;
  realname: string;
  password: string;
  avatar?: string;
  email?: string;
  mobile?: string;
  role_ids: string[];
  status: number;
}

export interface UpdateAdminParams {
  realname?: string;
  avatar?: string;
  email?: string;
  mobile?: string;
  role_ids?: string[];
  status?: number;
}

export const getAdminListApi = (params: UserListParams) => {
  return requestClient.get('/admin/user/list', {
    params,
  });
};

export const createAdminApi = (data: CreateAdminParams) => {
  return requestClient.post('/admin/user/create', data);
};

export const updateAdminApi = (id: string, data: UpdateAdminParams) => {
  return requestClient.put(`/admin/user/update/${id}`, data);
};

export const deleteAdminApi = (id: string) => {
  return requestClient.delete(`/admin/user/delete/${id}`);
};

export const setAdminRolesApi = (id: string, role_ids: string[]) => {
  return requestClient.put(`/admin/user/roles/${id}`, { role_ids });
};

export const getAdminRolesApi = (id: string) => {
  return requestClient.get(`/admin/user/roles/${id}`);
};

export const lockUserApi = (id: string) => {
  return requestClient.post(`/api/system/users/${id}/lock`);
};

export const unlockUserApi = (id: string) => {
  return requestClient.post(`/api/system/users/${id}/unlock`);
};

export const resetPasswordApi = (id: string, password: string) => {
  return requestClient.post(`/api/system/users/${id}/reset-password`, {
    password,
  });
};

// 角色管理接口
export const getRoleListApi = (params: RoleListParams) => {
  return requestClient.get<{
    limit: number;
    list: Role[];
    page: number;
    total: number;
  }>('/api/system/roles', { params });
};

export const getAllRolesApi = () => {
  return requestClient.get<Role[]>('/api/system/roles/all');
};

export const getRoleByIdApi = (id: string) => {
  return requestClient.get<Role>(`/api/system/roles/${id}`);
};

export const createRoleApi = (data: CreateRoleDto) => {
  return requestClient.post<Role>('/api/system/roles', data);
};

export const updateRoleApi = (id: string, data: UpdateRoleDto) => {
  return requestClient.put<Role>(`/api/system/roles/${id}`, data);
};

export const deleteRoleApi = (id: string) => {
  return requestClient.delete(`/api/system/roles/${id}`);
};

// 权限管理接口
export const getPermissionListApi = () => {
  return requestClient.get<Permission[]>('/api/system/permissions');
};

export const getPermissionTreeApi = () => {
  return requestClient.get<Permission[]>('/api/system/permissions/tree');
};

// 菜单管理接口
export const getMenuListApi = (params: MenuListParams) => {
  return requestClient.get<Menu[]>('/api/system/menus', { params });
};

export const getMenuTreeApi = () => {
  return requestClient.get<Menu[]>('/api/system/menus/tree');
};

export const getMenuByIdApi = (id: string) => {
  return requestClient.get<Menu>(`/api/system/menus/${id}`);
};

export const createMenuApi = (data: CreateMenuDto) => {
  return requestClient.post<Menu>('/api/system/menus', data);
};

export const updateMenuApi = (id: string, data: UpdateMenuDto) => {
  return requestClient.put<Menu>(`/api/system/menus/${id}`, data);
};

export const deleteMenuApi = (id: string) => {
  return requestClient.delete(`/api/system/menus/${id}`);
};

// 获取用户菜单（用于导航）
export const getUserMenusApi = () => {
  return requestClient.get<Menu[]>('/api/system/menus/user');
};
