import {
  TCard,
  TGeneralChartData,
  TGeneralChartGetParams,
  TStat,
  TteamChartData,
} from "../../types/Statistic/TStat";
import instance from "../api";

export type TStatGetParams = {
  search?: string;
  team?: string;
  start_date?: string;
  end_date?: string;
  for_salary?: boolean;
  page?: number;
  page_size?: number;
};

export type TStatTeamGetParams = {
  search?: string;
  start_date?: string;
  end_date?: string;
  page?: number;
  page_size?: number;
};

export type TStatCreatorsGetParams = {
  page?: number;
  page_size?: number;
  start_date?: string;
  end_date?: string;
  search?: string;
};
export type TteamChartGetParams = {
  start_date?: string;
  end_date?: string;
};

export const statController = {
  async read(filterObject: TStatGetParams) {
    const params = { ...filterObject };

    if (!!filterObject.search) params.search = filterObject.search;
    if (!!filterObject.team) params.team = filterObject.team;
    if (!!filterObject.start_date) params.start_date = filterObject.start_date;
    if (!!filterObject.end_date) params.end_date = filterObject.end_date;

    const { data } = await instance.get(`stats/all-users/`, {
      params,
    });
    return data;
  },

  async team(filterObject: TStatTeamGetParams) {
    const params = { ...filterObject };

    if (!!filterObject.search) params.search = filterObject.search;
    if (!!filterObject.start_date) params.start_date = filterObject.start_date;
    if (!!filterObject.end_date) params.end_date = filterObject.end_date;

    const { data } = await instance.get(`stats/all-teams/`, {
      params,
    });
    return data;
  },

  async creators(filterObject: TStatCreatorsGetParams) {
    const params = { ...filterObject };

    if (!!filterObject.start_date) params.start_date = filterObject.start_date;
    if (!!filterObject.end_date) params.end_date = filterObject.end_date;
    if (!!filterObject.search) params.search = filterObject.search;
    if (!!filterObject.page) params.page = filterObject.page;
    if (!!filterObject.page_size) params.page_size = filterObject.page_size;

    const { data } = await instance.get(`stats/task-creators/`, {
      params,
    });
    return data;
  },

  async cards(filterObject: TStatCreatorsGetParams) {
    const params = { ...filterObject };

    if (!!filterObject.start_date) params.start_date = filterObject.start_date;
    if (!!filterObject.end_date) params.end_date = filterObject.end_date;

    const { data } = await instance.get<TCard>(`stats/tasks-comparison/`, {
      params,
    });
    return data;
  },

  async teamChart(filterObject: TteamChartGetParams) {
    const params = { ...filterObject };

    if (!!filterObject.start_date) params.start_date = filterObject.start_date;
    if (!!filterObject.end_date) params.end_date = filterObject.end_date;

    const { data } = await instance.get<TteamChartData[]>(
      `stats/teams-line-chart/`,
      {
        params,
      }
    );
    return data;
  },
  async generalChart(filterObject: TGeneralChartGetParams) {
    const params = { ...filterObject };

    if (filterObject.start_date) params.start_date = filterObject.start_date;
    if (filterObject.end_date) params.end_date = filterObject.end_date;

    const { data } = await instance.get<TGeneralChartData>(
      "stats/general-stats",
      { params }
    );

    return data;
  },

  async saveUsersStats(
    fileName: string,
    startDate: string,
    endDate: string,
    team: string
  ) {
    const response = await instance.post(
      `stats/all-users/?start_date=${startDate}&end_date=${endDate}&team=${team}`,
      {
        headers: {
          "Content-Type": "multipart/form-data",
          "Content-Disposition": `attachment;`,
        },
        responseType: "arraybuffer",
      }
    );
    const blob = new Blob([response.data], {
      type: "application/octet-stream",
    });
    const downloadUrl = URL.createObjectURL(blob);
    const a = document.createElement("a");
    a.style.display = "none";
    a.href = downloadUrl;
    a.download = `stats_${fileName}.csv`;
    document.body.appendChild(a);
    a.click();
    window.URL.revokeObjectURL(downloadUrl);
    return response.data;
  },

  async saveTeamStats(fileName: string, startDate: string, endDate: string) {
    const response = await instance.post(
      `stats/all-teams/?start_date=${startDate}&end_date=${endDate}`,
      {
        headers: {
          "Content-Type": "multipart/form-data",
          "Content-Disposition": `attachment;`,
        },
        responseType: "arraybuffer",
      }
    );
    const blob = new Blob([response.data], {
      type: "application/octet-stream",
    });
    const downloadUrl = URL.createObjectURL(blob);
    const a = document.createElement("a");
    a.style.display = "none";
    a.href = downloadUrl;
    a.download = `stats_${fileName}.csv`;
    document.body.appendChild(a);
    a.click();
    window.URL.revokeObjectURL(downloadUrl);
    return response.data;
  },

  async statOne(Id: string | number | undefined) {
    const { data } = await instance.get<TStat>(`stats/${Id}`);
    return data;
  },
};
