import { request } from '@vtx/utils';
import { useRequest } from 'ahooks';
import * as Types from './types';

const query = async <T>(url: string, params: any, method: 'get' | 'post' = 'get'): Promise<T> => {
    const res = await request[method](url, {
        data: params,
    });
    return res?.data;
};

export const queryPage = async <T>(url: string, params: T) => {
    const res = await request.get(url, {
        data: params,
    });
    return {
        list: res?.data?.rows || [],
        total: res?.data?.total || 0,
    };
};

const PREFIX = '/cloud/sdyd/kanban/v2';

// 违规行为下拉
export const getViolateBehavior = () => {
    return query<{ text: string; value: string }[]>(
        '/cloud/sdyd/common/loadEnumValue',
        {
            enumCode: 'ThrowAberrantEnum',
        },
        'post',
    );
};

// 综合总览
export const overViewService = {
    // 投放点
    throwPoints: ({ housingEstateId }: { housingEstateId: string }) => {
        const { data } = useRequest(
            () =>
                query<any>('/cloud/sdyd/throwPoint/list', {
                    housingEstateId,
                }),
            {
                refreshDeps: [housingEstateId],
            },
        );
        return data;
    },
    // 小区基本数据
    estateStatistics: ({ housingEstateId }: Types.EstateStatisticsProps) => {
        const { data } = useRequest(
            () =>
                query<Types.EstateStatisticsResp>(
                    `${PREFIX}/housingEstatePopup/housingEstateStatistics`,
                    {
                        housingEstateId,
                    },
                ),
            {
                refreshDeps: [housingEstateId],
            },
        );
        return data;
    },
    // 投放热度按垃圾类型统计
    throwSumByGarbageType: ({ housingEstateId }: Types.ThrowSumByGarbageTypeProps) => {
        const { data } = useRequest(
            () =>
                query<Types.ThrowSumByGarbageTypeResp[]>(
                    `${PREFIX}/housingEstatePopup/throwSumByGarbageType`,
                    {
                        housingEstateId,
                    },
                ),
            {
                refreshDeps: [housingEstateId],
            },
        );
        return data;
    },
    // 投放热度按24h统计
    throwSumByHour: ({ housingEstateId }: Types.ThrowSumByHourProps) => {
        const { data } = useRequest(
            () =>
                query<Types.ThrowSumByGarbageTypeResp[]>(
                    `${PREFIX}/housingEstatePopup/throwSumByHour`,
                    {
                        housingEstateId,
                    },
                ),
            {
                refreshDeps: [housingEstateId],
            },
        );
        return data;
    },
    // 投放热度点位分布及一周趋势
    throwSumByDayAndPoint: ({ housingEstateId }: Types.ThrowSumByDayAndPointProps) => {
        const { data } = useRequest(
            () =>
                query<Types.ThrowSumByDayAndPointResp[]>(
                    `${PREFIX}/housingEstatePopup/throwSumByDayAndPointOneWeek`,
                    {
                        housingEstateId,
                    },
                ),
            {
                refreshDeps: [housingEstateId],
            },
        );
        return data;
    },
    // AI工单报事件类型分布
    alarmSumByType: ({ housingEstateId }: Types.AlarmByTypeProps) => {
        const { data } = useRequest(
            () =>
                query<Types.AlarmByTypeResp[]>(`${PREFIX}/housingEstatePopup/aiAlarmGroupByType`, {
                    housingEstateId,
                }),
            {
                refreshDeps: [housingEstateId],
            },
        );
        return data;
    },
    // AI工单近一周趋势
    alarmSumByDate: ({ housingEstateId }: Types.AlarmByDateProps) => {
        const { data } = useRequest(
            () =>
                query<Types.AlarmByDateResp[]>(
                    `${PREFIX}/housingEstatePopup/aiAlarmGroupByTypeAndDayOneWeek`,
                    {
                        housingEstateId,
                    },
                ),
            {
                refreshDeps: [housingEstateId],
            },
        );
        return data;
    },
    // 运营管理
    managementData: ({ housingEstateId }: Types.ManagementProps) => {
        const { data } = useRequest(
            () =>
                query<Types.ManagementResp>(`${PREFIX}/housingEstatePopup/operationManagement`, {
                    housingEstateId,
                }),
            {
                refreshDeps: [housingEstateId],
            },
        );
        return data;
    },
    // 工单处理效率
    resolveEfficiency: ({ housingEstateId }: Types.ResolveEfficiencyProps) => {
        const { data } = useRequest(
            () =>
                query<Types.ResolveEfficiencyResp>(
                    `${PREFIX}/housingEstatePopup/resolveEfficiency`,
                    {
                        housingEstateId,
                    },
                ),
            {
                refreshDeps: [housingEstateId],
            },
        );
        return data;
    },
};

// 投放热度
export const throwRecordService = {
    count: ({ housingEstateId, startDatetime, endDatetime }: Types.ThrowRecordCountProps) => {
        const { data } = useRequest(
            () =>
                query<Types.ThrowRecordCountResp>('/cloud/sdyd/throwRecord/count', {
                    housingEstateId,
                    startDatetime,
                    endDatetime,
                }),
            {
                ready: !!housingEstateId,
                refreshDeps: [housingEstateId, startDatetime, endDatetime],
            },
        );
        return data;
    },
};
