// Interfaces

export interface Fast2smsTemplate {
  id: string;
  templateId: string;
  name: string;
  message: string | null;
  fast2smsStatus: string;
  status: string;
  companyId: { id: string } | string;
  triggerPoint: string;
}

export interface Paginated<T> {
  results: T[];
  page: number;
  limit: number;
  totalPages: number;
  totalResults: number;
}

export interface ApiEnvelope<T> {
  code: number;
  message: string;
  data: T;
  success: boolean;
}

// Extend ApiEnvelope to include fast2sms property if your API returns it
export interface ApiEnvelopeWithFast2sms<T> extends ApiEnvelope<T> {
  fast2sms?: any; // Change 'any' to your fast2sms creds type if known
}

export interface GetFast2smsTemplatesParams {
  page?: number;
  limit?: number;
  search?: string;
  companyId?: string;
}

interface AddFast2smsCredsResponse {
  code: number;
  message: string;
  success: boolean;
}

export interface Getfast2smsResponse {
  results: Fast2smsTemplate[];
  fast2sms?: any;
}

export interface BulkUploadTemplatesResponse {
  code: number;
  message: string;
  success: boolean;
  data: {
    results: Fast2smsTemplate[];
    page: number;
    limit: number;
    totalPages: number;
    totalResults: number;
  }
}

// RTK Query API slice

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

export const fast2smsApi = apiSlice.injectEndpoints({
  endpoints: (builder) => ({
    getfast2sms: builder.query<
      Getfast2smsResponse,
      GetFast2smsTemplatesParams | void
    >({
      query: (params = {}) => ({
        url: `${URLS.FAST2SMS}?${generateQueryString(
          params as Record<string, unknown>
        )}`,
        method: "GET",
      }),
      transformResponse: (
        response: any
      ) => (
        console.log(response),
        {
          results: response.data.results,
          fast2sms: response.data.fast2sms,
        }
      ),
      providesTags: ["Fast2sms"],
    }),
    updateSMSStatus: builder.mutation<
      Fast2smsTemplate,
      { id: string; status?: string, triggerPoint?: string }
    >({
      query: ({ id, ...body }) => ({
        url: `${URLS.FAST2SMS}/${id}`,
        method: "PATCH",
        body,
      }),
      invalidatesTags: ["Fast2sms"],
    }),
    addFast2smsCreds: builder.mutation<
      AddFast2smsCredsResponse,
      {
        accessToken: string;
        senderId: string;
      }
    >({
      query: (body) => ({
        url: `${URLS.FAST2SMS}`,
        method: "PATCH",
        body,
      }),
      invalidatesTags: ["Fast2sms"],
    }),
    bulkUploadTemplat: builder.mutation<
      BulkUploadTemplatesResponse,
      { templates: Array<Record<string, any>> }
    >({
      query: (body) => ({
        url: `${URLS.FAST2SMS}`,
        method: "POST",
        body,
      }),
      invalidatesTags: ["Fast2sms"],
    }),
  }),
  overrideExisting: true,
});

export const {
  useGetfast2smsQuery,
  useUpdateSMSStatusMutation,
  useAddFast2smsCredsMutation,
  useBulkUploadTemplatMutation,
} = fast2smsApi;
