/**
 * 用户待办操作服务
 * 用于后台管理指定用户登录后需要执行的特定操作
 */

import apiClient from '@/infrastructure/api/client';
import { API_ENDPOINTS } from '@/infrastructure/api/endpoints';
import type {
  PendingAction,
  PendingActionListResponse,
  CreatePendingActionRequest,
  BatchCreatePendingActionRequest,
  UpdatePendingActionRequest,
  QueryPendingActionsParams,
  BatchCreateResult,
} from '@/types/pending-action.types';

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

  /**
   * 获取单个待办操作详情
   */
  async getDetail(id: string): Promise<PendingAction> {
    const response = await apiClient.get(API_ENDPOINTS.PENDING_ACTIONS.DETAIL(id));
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return (response as any)?.data?.data;
  },

  /**
   * 创建待办操作
   */
  async create(data: CreatePendingActionRequest): Promise<PendingAction> {
    const response = await apiClient.post(API_ENDPOINTS.PENDING_ACTIONS.CREATE, data);
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return (response as any)?.data?.data;
  },

  /**
   * 批量创建待办操作
   */
  async batchCreate(data: BatchCreatePendingActionRequest): Promise<BatchCreateResult> {
    const response = await apiClient.post(API_ENDPOINTS.PENDING_ACTIONS.BATCH_CREATE, data);
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return (response as any)?.data?.data;
  },

  /**
   * 更新待办操作
   */
  async update(id: string, data: UpdatePendingActionRequest): Promise<PendingAction> {
    const response = await apiClient.put(API_ENDPOINTS.PENDING_ACTIONS.UPDATE(id), data);
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return (response as any)?.data?.data;
  },

  /**
   * 取消待办操作（软删除）
   */
  async cancel(id: string): Promise<PendingAction> {
    const response = await apiClient.post(API_ENDPOINTS.PENDING_ACTIONS.CANCEL(id));
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return (response as any)?.data?.data;
  },

  /**
   * 删除待办操作（硬删除）
   */
  async delete(id: string): Promise<void> {
    await apiClient.delete(API_ENDPOINTS.PENDING_ACTIONS.DELETE(id));
  },
};

export default pendingActionService;
