import { request } from '../../request';

// ============ 租户管理 ============

/** 获取租户分页列表 */
export function fetchGetTenantList(params?: { keywords?: string; pageNum: number; pageSize: number }) {
  return request<any>({
    url: '/smqjh-system/api/v1/tenants/page',
    method: 'get',
    params
  });
}

/** 新增租户 */
export function fetchAddTenant(data: any) {
  return request({
    url: '/smqjh-system/api/v1/tenants',
    method: 'post',
    data
  });
}

/** 修改租户 */
export function fetchEditTenant(data: any) {
  return request({
    url: `/smqjh-system/api/v1/tenants/${data.id}`,
    method: 'put',
    data
  });
}

/** 删除租户 */
export function fetchDeleteTenant(ids: string) {
  return request({
    url: `/smqjh-system/api/v1/tenants/${ids}`,
    method: 'delete'
  });
}

// ============ 租户套餐 ============

/** 获取租户套餐分页列表 */
export function fetchGetTenantPackageList(params?: { pageNum: number; pageSize: number }) {
  return request<any>({
    url: '/smqjh-system/api/v1/tenant-packages/page',
    method: 'get',
    params
  });
}

/** 新增租户套餐 */
export function fetchAddTenantPackage(data: any) {
  return request({
    url: '/smqjh-system/api/v1/tenant-packages',
    method: 'post',
    data
  });
}

/** 修改租户套餐 */
export function fetchEditTenantPackage(data: any) {
  return request({
    url: `/smqjh-system/api/v1/tenant-packages/${data.id}`,
    method: 'put',
    data
  });
}

/** 获取套餐关联的菜单列表 */
export function fetchGetTenantPackageMenus(packageId: number) {
  return request<number[]>({
    url: `/smqjh-system/api/v1/tenant-packages/${packageId}/menus`,
    method: 'get'
  });
}

/** 为套餐分配菜单权限 */
export function fetchUpdateTenantPackageMenus(packageId: number, data: number[]) {
  return request({
    url: `/smqjh-system/api/v1/tenant-packages/${packageId}/menus`,
    method: 'put',
    data
  });
}

/** 删除租户套餐 */
export function fetchDeleteTenantPackage(ids: string) {
  return request({
    url: `/smqjh-system/api/v1/tenant-packages/${ids}`,
    method: 'delete'
  });
}

// ============ 租户切换 ============

/** 获取可切换的租户列表（适配ZTable分页格式） */
export async function fetchGetActiveTenants(_params?: any) {
  const res = await request<any[]>({
    url: '/smqjh-system/api/v1/tenants/active',
    method: 'get'
  });
  // ZTable的defaultTransform期望 data 格式为 { list, total, current, size }
  if (!res.error && Array.isArray(res.data)) {
    return {
      ...res,
      data: {
        list: res.data,
        total: res.data.length,
        current: 1,
        size: res.data.length
      }
    };
  }
  return {
    ...res,
    data: {
      list: [],
      total: 0,
      current: 1,
      size: 10
    }
  };
}

/** 切换租户 */
export function fetchSwitchTenant(tenantCode: string) {
  return request({
    url: `/smqjh-system/api/v1/tenants/switch/${tenantCode}`,
    method: 'post'
  });
}

/** 获取当前切换的租户 */
export function fetchGetCurrentSwitchTenant() {
  return request<any>({
    url: '/smqjh-system/api/v1/tenants/switch/current',
    method: 'get'
  });
}

/** 清除租户切换 */
export function fetchClearSwitchTenant() {
  return request({
    url: '/smqjh-system/api/v1/tenants/switch/clear',
    method: 'post'
  });
}
