import {
    UserFormData,
    UserResponseData,
    FeeResponseData,
    WithRechargeResponseData,
    AllScsResponseData,
    ContractCoinListFormData,
    ContractCoinListResponseData,
    MemberBoardResponseData,
} from "@/types/api/system/dashboard";
import request from "@/utils/request";
import { AxiosPromise } from "axios";

/**
 * 获取用户信息
 * @param data
 */
export function user(data: UserFormData): AxiosPromise<UserResponseData> {
    return request({
        url: "admin/member/stat",
        method: "post",
        params: data,
    });
}

/**
 * 获取手续费
 */
// export function fee(): AxiosPromise<FeeResponseData> {
//   return request({
//     url: 'admin/finance/member-transaction/contract-fee/stat',
//     method: 'post',
//   });
// }
export function fee(): AxiosPromise<FeeResponseData> {
    return request({
        url: "admin/finance/member-transaction/stat",
        method: "post",
    });
}

/**
 * 今日充提
 */
export function trw(): AxiosPromise<WithRechargeResponseData> {
    return request({
        url: "admin/finance/member-transaction/recharge-and-withdraw/today",
        method: "post",
    });
}

/**
 * 合约平台盈利
 */
export function scs(): AxiosPromise<AllScsResponseData> {
    return request({
        url: "admin/swap-coin/stat",
        method: "post",
    });
}

/**
 * 跟单平台累计跟随
 */
export function cumulativeFollow(): AxiosPromise<AllScsResponseData> {
    return request({
        url: "/copy-trade-admin/position/position-statistics",
        method: "post",
    });
}

/**
 * "永续合约"=>"交易对管理"=>"获取交易对列表"
 * */
export function queryContractCoinList(
    data: ContractCoinListFormData
): AxiosPromise<ContractCoinListResponseData> {
    return request({
        url: "admin/swap-coin/page-query",
        method: "post",
        params: data,
    });
}

/**
 * 今日概览统计：查询 stat_today_overview 当日数据
 * @param includeInternal 0=不含内部账户（默认）；1=含内部账户
 */
export function todayOverview(includeInternal: 0 | 1 = 0) {
    return request({
        url: "admin/stat/dashboard/today-overview",
        method: "get",
        params: { includeInternal },
    });
}

/**
 * 合约盈亏概览：查询 stat_contract_pnl 当日数据
 * @param includeInternal 0=不含内部账户（默认）；1=含内部账户
 */
export function contractPnl(includeInternal: 0 | 1 = 0) {
    return request({
        url: "admin/stat/dashboard/contract-pnl",
        method: "get",
        params: { includeInternal },
    });
}

/**
 * 合约盈亏刷新：触发 datacenter 重新计算并返回最新数据
 * @param includeInternal 0=不含内部账户（默认）；1=含内部账户
 */
export function contractPnlRefresh(includeInternal: 0 | 1 = 0) {
    return request({
        url: "admin/stat/dashboard/contract-pnl/refresh",
        method: "post",
        params: { includeInternal },
    });
}

/**
 * 今日概览刷新：触发 datacenter 重新计算并返回最新数据
 * @param includeInternal 0=不含内部账户（默认）；1=含内部账户
 */
export function todayOverviewRefresh(includeInternal: 0 | 1 = 0) {
    return request({
        url: "admin/stat/dashboard/today-overview/refresh",
        method: "post",
        params: { includeInternal },
    });
}

/**
 * 全量刷新：同时触发 today-overview + contract-pnl 两个 datacenter job，
 * 刷新首页所有统计数据（两个变体 includeInternal=0/1 均会被重算）
 */
export function refreshAll(includeInternal: 0 | 1 = 0) {
    return request({
        url: "admin/stat/dashboard/refresh-all",
        method: "post",
        params: { includeInternal },
    });
}

/**
 * 每日盈亏统计：自主+跟单合计，最近 N 天（最多 30）
 * @param days 天数（7 或 30）
 * @param includeInternal 0=不含内部账户；1=含
 */
export function dailyPnl(days: number = 7, includeInternal: 0 | 1 = 0) {
    return request({
        url: "admin/stat/dashboard/daily-pnl",
        method: "get",
        params: { days, includeInternal },
    });
}

/**
 * 触发 datacenter 补算过去 N 天的每日盈亏历史数据
 */
export function dailyPnlBackfill(days: number = 30) {
    return request({
        url: "admin/stat/dashboard/daily-pnl/backfill",
        method: "post",
        params: { days },
    });
}

/**
 * 每日充值提现统计：读取 stat_today_overview 中最近 N 天的 deposit/withdraw 数据
 * @param days 天数（7 或 30）
 * @param includeInternal 0=不含内部账户；1=含
 */
export function dailyRecharge(days: number = 7, includeInternal: 0 | 1 = 0) {
    return request({
        url: "admin/stat/dashboard/daily-recharge",
        method: "get",
        params: { days, includeInternal },
    });
}

/**
 * 触发 datacenter 补算过去 N 天的每日充提历史数据
 */
export function dailyRechargeBackfill(days: number = 30) {
    return request({
        url: "admin/stat/dashboard/daily-recharge/backfill",
        method: "post",
        params: { days },
    });
}

/**
 * 每日注册/登录统计：读取 stat_today_overview 中最近 N 天的 register_count/login_count
 * @param days 天数（7 或 30）
 */
export function dailyUserStat(days: number = 7, includeInternal: 0 | 1 = 0) {
    return request({
        url: "admin/stat/dashboard/daily-user-stat",
        method: "get",
        params: { days, includeInternal },
    });
}

/**
 * 平台每日盈亏统计：查询 stat_platform_daily_pnl 明细
 * @param startDate yyyy-MM-dd（可选）
 * @param endDate   yyyy-MM-dd（可选）
 * @param includeInternal 0=不含内部账户；1=含
 */
export function platformDailyPnl(params: {
    startDate?: string;
    endDate?: string;
    includeInternal?: 0 | 1;
} = {}) {
    return request({
        url: "admin/stat/dashboard/platform-daily-pnl",
        method: "get",
        params,
    });
}

/**
 * 触发 datacenter 补算平台每日盈亏历史数据
 */
export function platformDailyPnlBackfill(days: number = 30) {
    return request({
        url: "admin/stat/dashboard/platform-daily-pnl/backfill",
        method: "post",
        params: { days },
    });
}

/**
 * "首页"=>"统计"=>"会员统计表"
 */
export function memberBoard(
    data: ContractCoinListFormData
): AxiosPromise<MemberBoardResponseData> {
    return request({
        url: "admin/index/statistics/member-statistics-info",
        method: "get",
        params: data,
    });
}
