import { adminRequest } from "@/lib/admin-http";


import type { AdminDashboardData } from "@/types/api/admin-dashboard";
import type {
  AdminDashboardAnalyticsData,
  AdminDashboardAnalyticsQuery,
} from "@/types/api/admin-dashboard-analytics";

const A = `/admin`;
const DASHBOARD_SCOPE_SITE_PARAM = "site_code";
const DASHBOARD_SCOPE_AGENT_PARAM = "agent_node_id";

/** 首页仪表盘聚合（大厅 + 当期财务/风控/异常转账等，按账号权限填充各块） */
export async function getAdminDashboard(): Promise<AdminDashboardData> {
  return adminRequest.get<AdminDashboardData>(`${A}/dashboard`);
}

export async function getAdminDashboardByScope(scope: {
  site_code?: string;
  agent_node_id?: number;
}): Promise<AdminDashboardData> {
  const params = new URLSearchParams();
  if (scope.site_code && scope.site_code.trim() !== "") {
    params.set(DASHBOARD_SCOPE_SITE_PARAM, scope.site_code.trim());
  }
  if (scope.agent_node_id && Number.isInteger(scope.agent_node_id) && scope.agent_node_id > 0) {
    params.set(DASHBOARD_SCOPE_AGENT_PARAM, String(scope.agent_node_id));
  }
  const qs = params.toString();
  return adminRequest.get<AdminDashboardData>(`${A}/dashboard${qs ? `?${qs}` : ""}`);
}

/** 仪表盘可筛选分析（区间汇总、日趋势、玩法拆解） */
export async function getAdminDashboardAnalytics(
  query: AdminDashboardAnalyticsQuery = {},
): Promise<AdminDashboardAnalyticsData> {
  const params = new URLSearchParams();
  if (query.period) {
    params.set("period", query.period);
  }
  if (query.date_from) {
    params.set("date_from", query.date_from);
  }
  if (query.date_to) {
    params.set("date_to", query.date_to);
  }
  if (query.metric) {
    params.set("metric", query.metric);
  }
  if (query.play_code) {
    params.set("play_code", query.play_code);
  }
  if (query.site_code && query.site_code.trim() !== "") {
    params.set(DASHBOARD_SCOPE_SITE_PARAM, query.site_code.trim());
  }
  if (query.agent_node_id && Number.isInteger(query.agent_node_id) && query.agent_node_id > 0) {
    params.set(DASHBOARD_SCOPE_AGENT_PARAM, String(query.agent_node_id));
  }
  const qs = params.toString();

  return adminRequest.get<AdminDashboardAnalyticsData>(
    `${A}/dashboard/analytics${qs ? `?${qs}` : ""}`,
  );
}
