import type { MenuModel, UserModel, RoleModel,  LoggerModel } from '@/request/model/manage.model';

import HTTP from '@/request/alova';
import { cacheFor } from '@/request/api/index';
import { fromMenusData, fromUserData, fromRoleData, fromLoggerData } from '@/request/model/manage.model';

export function login(data: { account: string; password: string }) {
  return HTTP.Post<string, any>(
    `/login`,
    { username: data.account, password: data.password },
    {
      meta: { ignoreToken: true },
      transform(data) {
        return data.token;
      },
    }
  );
}

export function getAccountInfo(token: string) {
  return HTTP.Get<[string, UserModel], any>(`/getInfo`, {
    headers: {
      Authorization: token,
    },
    transform(data: any) {
      return [token, fromUserData(data)];
    },
  });
}

export function getMenus() {
  return HTTP.Get<MenuModel[], any>(`/warrantManage/getMenus`, {
    transform(data) {
      const menus = typeof data === 'string' ? JSON.parse(data) : data;
      return fromMenusData(menus)!;
    },
    shareRequest: true,
    cacheFor,
  });
}

export function getRoles() {
  return HTTP.Get<(RoleModel & { __raw__: any })[], any>(`/admin/right_RoleMgr/optionselect`, {
    transform(data) {
      return Array.isArray(data) ? data.map((item) => Object.assign(fromRoleData(item), { __raw__: item })) : [];
    },
    hitSource: /^role-/,
  });
}

export function listRoles(page = 1, size = 20, query = {}) {
  const params = { page, limit: size };
  return HTTP.Post<{ data: RoleModel[]; total: number }, any>(
    `/admin/right_RoleMgr/listPain`,
    { ...params, ...query },
    {
      params,
      transform(data) {
        return { total: data.TotalRecordCount, data: data.Items.map(fromRoleData) };
      },
      hitSource: /^role-/,
    }
  );
}

export function editRole(data: { pid?: string } & Record<string, any>) {
  return HTTP.Post<RoleModel, any>(data.pid ? `/admin/right_RoleMgr/update` : `/admin/right_RoleMgr/Add`, data, {
    transform() {
      return fromRoleData(data);
    },
    name: `role-edit`,
  });
}

export function deleteRole(id: string) {
  return HTTP.Post(
    `/admin/right_RoleMgr/BatchDelete`,
    {},
    {
      params: { ids: id },
      name: `role-delete`,
    }
  );
}

export function listUsers(page = 1, size = 20, query = {}) {
  const params = { page, limit: size };
  return HTTP.Post<{ data: UserModel[]; total: number }, any>(
    `/portal/userMgr/listPain`,
    { ...params, ...query },
    {
      params,
      transform(data) {
        return { total: data.TotalRecordCount, data: data.Items.map(fromUserData) };
      },
    }
  );
}

export function editUser(data: { pid?: string } & Record<string, any>) {
  if (!data.pid) data.isFirst = 0;
  return HTTP.Post<UserModel, any>(data.pid ? `/portal/userMgr/update` : `/portal/userMgr/Add`, data, {
    transform() {
      return fromUserData(data);
    },
  });
}

export function deleteUser(id: string) {
  return HTTP.Post(`/portal/userMgr/BatchDelete`, {}, { params: { ids: id } });
}

export function resetUserPassword(pid: string) {
  return HTTP.Post(`/portal/userMgr/resetPassWord`, { userPid: pid });
}


export function updateUserPassword(data: { password: string; again: string; pid: string | number }, token?: string) {
  return HTTP.Post(
    `/portal/userMgr/UpdatePassWord`,
    {
      userPid: data.pid,
      password: data.password,
      newPassword: data.again,
    },
    { headers: { token } }
  );
}

export function listLoggers(page = 1, size = 20, query = {}) {
  const params = { page, limit: size };
  return HTTP.Get<{ data: LoggerModel[]; total: number }, any>(
    `/monitor/operlog/list`,
    {
      params: { ...params, ...query },
      transform(data) {
        return { total: data.TotalRecordCount, data: data.Items.map(fromLoggerData) };
      },
    }
  );
}