/**
 * 授权管理服务
 * 用于管理后台查询和管理用户授权
 */

import apiClient from '@/infrastructure/api/client';
import { API_ENDPOINTS } from '@/infrastructure/api/endpoints';
import type {
  AuthorizationListResponse,
  QueryAuthorizationsParams,
  RevokeAuthorizationRequest,
  GrantCommunityRequest,
  GrantProvinceCompanyRequest,
  GrantCityCompanyRequest,
  GrantAuthProvinceCompanyRequest,
  GrantAuthCityCompanyRequest,
} from '@/types/authorization.types';

/**
 * 授权管理服务
 *
 * API 响应结构（经过 apiClient 拦截器解包后）:
 * { success: true, data: {...}, timestamp: "..." }
 *
 * 需要访问 .data 获取实际业务数据
 */
export const authorizationService = {
  /**
   * 查询授权列表
   */
  async getList(params: QueryAuthorizationsParams = {}): Promise<AuthorizationListResponse> {
    const response = await apiClient.get(API_ENDPOINTS.AUTHORIZATION.ADMIN_LIST, { params });
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const result = (response as any)?.data;
    return result ?? { items: [], total: 0, page: 1, limit: 20 };
  },

  /**
   * 撤销授权
   */
  async revoke(authorizationId: string, data: RevokeAuthorizationRequest): Promise<{ message: string }> {
    const response = await apiClient.post(API_ENDPOINTS.AUTHORIZATION.ADMIN_REVOKE(authorizationId), data);
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return (response as any)?.data ?? { message: '授权已撤销' };
  },

  /**
   * 授权社区
   */
  async grantCommunity(data: GrantCommunityRequest): Promise<{ message: string }> {
    const response = await apiClient.post(API_ENDPOINTS.AUTHORIZATION.ADMIN_GRANT_COMMUNITY, data);
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return (response as any)?.data ?? { message: '社区授权成功' };
  },

  /**
   * 授权正式省公司
   */
  async grantProvinceCompany(data: GrantProvinceCompanyRequest): Promise<{ message: string }> {
    const response = await apiClient.post(API_ENDPOINTS.AUTHORIZATION.ADMIN_GRANT_PROVINCE_COMPANY, data);
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return (response as any)?.data ?? { message: '正式省公司授权成功' };
  },

  /**
   * 授权正式市公司
   */
  async grantCityCompany(data: GrantCityCompanyRequest): Promise<{ message: string }> {
    const response = await apiClient.post(API_ENDPOINTS.AUTHORIZATION.ADMIN_GRANT_CITY_COMPANY, data);
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return (response as any)?.data ?? { message: '正式市公司授权成功' };
  },

  /**
   * 授权省团队
   */
  async grantAuthProvinceCompany(data: GrantAuthProvinceCompanyRequest): Promise<{ message: string }> {
    const response = await apiClient.post(API_ENDPOINTS.AUTHORIZATION.ADMIN_GRANT_AUTH_PROVINCE_COMPANY, data);
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return (response as any)?.data ?? { message: '省团队授权成功' };
  },

  /**
   * 授权市团队
   */
  async grantAuthCityCompany(data: GrantAuthCityCompanyRequest): Promise<{ message: string }> {
    const response = await apiClient.post(API_ENDPOINTS.AUTHORIZATION.ADMIN_GRANT_AUTH_CITY_COMPANY, data);
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return (response as any)?.data ?? { message: '市团队授权成功' };
  },
};

export default authorizationService;
