/**
 * P18 任务引擎运营看板 API。
 *
 * 端点前缀：/api/admin/task-engine
 */

import { apiClient } from "./client";

/* ------------------------------------------------------------------ */
/*  类型定义                                                           */
/* ------------------------------------------------------------------ */

export interface TransferLogItem {
  id: number;
  site_id: number;
  site_name: string;
  member_id: number;
  member_name: string;
  from_assistant_id: number;
  from_assistant_name: string;
  to_assistant_id: number;
  to_assistant_name: string;
  transfer_reason: string | null;
  transfer_score: number | null;
  guard_checks: Record<string, unknown> | null;
  created_at: string;
}

export interface TransferLogPage {
  items: TransferLogItem[];
  total: number;
}

export interface PendingReviewItem {
  id: number;
  site_id: number;
  site_name: string;
  member_id: number;
  member_name: string;
  assistant_id: number;
  assistant_name: string;
  task_type: string;
  task_type_label: string;
  transfer_count: number;
  priority_score: number | null;
  created_at: string;
}

export interface PendingReviewPage {
  items: PendingReviewItem[];
  total: number;
}

export interface ConfigParam {
  id: number;
  site_id: number | null;
  site_name: string | null;
  param_key: string;
  param_value: number;
  description: string | null;
  updated_at: string;
}

export interface ConfigParamList {
  params: ConfigParam[];
}

/* ------------------------------------------------------------------ */
/*  转移日志                                                           */
/* ------------------------------------------------------------------ */

export interface TransferLogQuery {
  site_id?: number;
  from_date?: string;
  to_date?: string;
  assistant_id?: number;
  page?: number;
  page_size?: number;
}

export async function fetchTransferLogs(
  query: TransferLogQuery = {},
): Promise<TransferLogPage> {
  const resp = await apiClient.get<TransferLogPage>(
    "/admin/task-engine/transfer-log",
    { params: query },
  );
  return resp.data;
}

export async function fetchMemberTransferHistory(
  memberId: number,
): Promise<TransferLogItem[]> {
  const resp = await apiClient.get<TransferLogItem[]>(
    `/admin/task-engine/transfer-log/${memberId}/history`,
  );
  return resp.data;
}

/* ------------------------------------------------------------------ */
/*  待审核任务                                                         */
/* ------------------------------------------------------------------ */

export interface PendingReviewQuery {
  site_id?: number;
  page?: number;
  page_size?: number;
}

export async function fetchPendingReviews(
  query: PendingReviewQuery = {},
): Promise<PendingReviewPage> {
  const resp = await apiClient.get<PendingReviewPage>(
    "/admin/task-engine/pending-review",
    { params: query },
  );
  return resp.data;
}

export async function reassignTask(
  taskId: number,
  toAssistantId: number,
): Promise<{ success: boolean; new_task_id: number | null }> {
  const resp = await apiClient.post(
    `/admin/task-engine/pending-review/${taskId}/reassign`,
    { to_assistant_id: toAssistantId },
  );
  return resp.data;
}

export async function closeTask(
  taskId: number,
  reason: string,
): Promise<{ success: boolean }> {
  const resp = await apiClient.post(
    `/admin/task-engine/pending-review/${taskId}/close`,
    { reason },
  );
  return resp.data;
}

/* ------------------------------------------------------------------ */
/*  参数管理                                                           */
/* ------------------------------------------------------------------ */

export async function fetchConfigParams(
  siteId?: number,
): Promise<ConfigParamList> {
  const resp = await apiClient.get<ConfigParamList>(
    "/admin/task-engine/config",
    { params: siteId != null ? { site_id: siteId } : {} },
  );
  return resp.data;
}

export async function updateConfigParam(
  paramId: number,
  paramValue: number,
): Promise<{ success: boolean }> {
  const resp = await apiClient.put(
    `/admin/task-engine/config/${paramId}`,
    { param_value: paramValue },
  );
  return resp.data;
}

export async function createConfigParam(
  siteId: number,
  paramKey: string,
  paramValue: number,
): Promise<{ success: boolean; id: number }> {
  const resp = await apiClient.post("/admin/task-engine/config", {
    site_id: siteId,
    param_key: paramKey,
    param_value: paramValue,
  });
  return resp.data;
}

export async function deleteConfigParam(
  paramId: number,
): Promise<{ success: boolean }> {
  const resp = await apiClient.delete(
    `/admin/task-engine/config/${paramId}`,
  );
  return resp.data;
}
