import { requestClient } from '#/api/request';

enum Api {
  syncFull = '/wecom/approval/sync/full',
  syncCurrent = '/wecom/approval/sync/current',
  syncLogs = '/wecom/approval/sync/logs',
}

/**
 * 全量同步审批数据（管理员）
 * @param daysBack 同步近 N 天数据，默认30天
 */
export function wecomApprovalSyncFull(daysBack = 30) {
  return requestClient.post<number>(`${Api.syncFull}`, null, {
    params: { daysBack },
    // 设置较长的超时时间（60秒），因为即使异步，启动同步也需要时间
    timeout: 60000,
  });
}

/**
 * 同步当前用户审批数据
 */
export function wecomApprovalSyncCurrent() {
  return requestClient.post<string>(Api.syncCurrent, {});
}

/**
 * 同步审批模板（管理员）
 */
export function wecomApprovalSyncTemplates() {
  return requestClient.post<string>('/wecom/approval/templates/sync', {});
}

/**
 * 查询同步日志
 */
export function wecomApprovalSyncLogs(params: { pageNum: number; pageSize: number }) {
  return requestClient.get<{ rows: any[]; total: number }>(Api.syncLogs, { params });
}

/**
 * 获取定时任务状态
 */
export function getTaskStatus() {
  return requestClient.get<{ running: boolean; cron: string }>('/wecom/approval/sync/task/status');
}

/**
 * 启动定时任务
 */
export function startTask() {
  return requestClient.post<string>('/wecom/approval/sync/task/start', {});
}

/**
 * 停止定时任务
 */
export function stopTask() {
  return requestClient.post<string>('/wecom/approval/sync/task/stop', {});
}

/**
 * 设置同步频率
 */
export function setCron(cron: string) {
  return requestClient.post<string>('/wecom/approval/sync/task/cron', null, { params: { cron } });
}
