import { generateQueryString } from "@/utils/queryStringUtil";
import { apiSlice } from ".";
import URLS from "./constants";

export interface ClickStats {
  shared: number;
  opened: number;
}

export interface GetClickStatsResponse {
  code: number;
  message: string;
  data: ClickStats;
  success: boolean;
}

export interface CreateClickStatsRequest {
  type: "whatsapp";
  action: "shared" | "opened";
  propertyId: string;
  deviceInfo: string;
  companyId?: string;
}

export interface CreateClickStatsResponse {
  code: number;
  message: string;
  data: boolean;
  success: boolean;
}

type GetClickStatsQueryParams = {
  propertyId: string;
  type: "whatsapp";
};

export const clickStatsApi = apiSlice.injectEndpoints({
  endpoints: (builder) => ({
    getClickStats: builder.query<GetClickStatsResponse, GetClickStatsQueryParams>(
      {
        query: (params) => ({
          url: `${URLS.CLICK_STATS}?${generateQueryString(
            params as Record<string, unknown>
          )}`,
          method: "GET",
        }),
        providesTags: ["ClickStats"],
      }
    ),

    createClickStats: builder.mutation<
      CreateClickStatsResponse,
      CreateClickStatsRequest
    >({
      query: (body) => ({
        url: `${URLS.CLICK_STATS}`,
        method: "POST",
        body,
      }),
      invalidatesTags: ["ClickStats", "editIndividualProperties"],
    }),
  }),
  overrideExisting: false,
});

export const {
  useGetClickStatsQuery,
  useCreateClickStatsMutation,
} = clickStatsApi;
