import api, { PageResponse, unwrap } from './request';

export interface UserResp {
  userId: number;
  loginId: string;
  userName: string;
  email?: string;
  phone?: string;
  agentId?: number;
  agentName?: string;
  orgName?: string;
  status: string;
  failCount?: number;
  lastLoginAt?: string;
  createdAt?: string;
  roleCodes?: string[];
}

export interface UserSaveReq {
  loginId: string;
  userName: string;
  email?: string;
  phone?: string;
  agentId?: number;
  status?: string;
  password?: string;
  roleIds?: number[];
}

export const userApi = {
  list: (p: { searchKeyword?: string; status?: string; pageNum?: number; pageSize?: number }) =>
    unwrap<PageResponse<UserResp>>(api.get('/api/system/users', { params: p })),
  detail: (id: number) => unwrap<UserResp>(api.get(`/api/system/users/${id}`)),
  create: (req: UserSaveReq) => unwrap<number>(api.post('/api/system/users', req)),
  update: (id: number, req: UserSaveReq) => unwrap<void>(api.put(`/api/system/users/${id}`, req)),
  resetPassword: (id: number) => unwrap<void>(api.put(`/api/system/users/${id}/reset-password`)),
  unlock: (id: number) => unwrap<void>(api.put(`/api/system/users/${id}/unlock`)),
  updatePhone: (id: number, phone: string) =>
    unwrap<void>(api.put(`/api/system/users/${id}/phone`, { phone })),
};

export interface RoleVO {
  roleId: number;
  roleCode: string;
  roleName: string;
  roleDesc?: string;
  roleLevel: number;
  isSystem: string;
  isActive: string;
}

export const roleApi = {
  list: () => unwrap<RoleVO[]>(api.get('/api/system/roles')),
  detail: (id: number) => unwrap<RoleVO>(api.get(`/api/system/roles/${id}`)),
  permissions: (id: number) =>
    unwrap<Array<{ menuId: number; permCode: string; isGranted: string }>>(api.get(`/api/system/roles/${id}/permissions`)),
  create: (vo: Partial<RoleVO>) => unwrap<number>(api.post('/api/system/roles', vo)),
  update: (id: number, vo: Partial<RoleVO>) => unwrap<void>(api.put(`/api/system/roles/${id}`, vo)),
  savePermissions: (id: number, perms: Array<{ menuId: number; permCode: string; isGranted: string }>) =>
    unwrap<void>(api.put(`/api/system/roles/${id}/permissions`, perms)),
};

export interface CommonCodeGroup {
  groupCode: string;
  groupName: string;
  description?: string;
  isSystem: string;
  isActive: string;
  sortOrder: number;
}

export interface CommonCode {
  codeId: number;
  groupCode: string;
  code: string;
  codeName: string;
  codeDesc?: string;
  sortOrder: number;
  isActive: string;
}

export const commonCodeApi = {
  groups: (keyword?: string) =>
    unwrap<CommonCodeGroup[]>(api.get('/api/common/codes/groups', { params: { keyword } })),
  codes: (groupCode: string) =>
    unwrap<CommonCode[]>(api.get(`/api/common/codes/groups/${groupCode}/codes`)),
  createGroup: (vo: Partial<CommonCodeGroup>) =>
    unwrap<void>(api.post('/api/common/codes/groups', vo)),
  updateGroup: (groupCode: string, vo: Partial<CommonCodeGroup>) =>
    unwrap<void>(api.put(`/api/common/codes/groups/${groupCode}`, vo)),
  deleteGroup: (groupCode: string) =>
    unwrap<void>(api.delete(`/api/common/codes/groups/${groupCode}`)),
  createCode: (groupCode: string, vo: Partial<CommonCode>) =>
    unwrap<void>(api.post(`/api/common/codes/groups/${groupCode}/codes`, vo)),
  updateCode: (codeId: number, vo: Partial<CommonCode>) =>
    unwrap<void>(api.put(`/api/common/codes/${codeId}`, vo)),
  deleteCode: (codeId: number) =>
    unwrap<void>(api.delete(`/api/common/codes/${codeId}`)),
  refreshCache: (groupCode?: string) =>
    unwrap<void>(api.post('/api/common/codes/refresh', null, { params: { groupCode } })),
};
