import { createApi } from "@reduxjs/toolkit/query/react";
import { baseQueryWithReauth } from "./apiSlice";

interface ContactData {
  id: number;
  email: string;
  first_name: string;
  created_at: string;
}

interface ApiResponse {
  data: {
    data: ContactData[];
  };
}

export const manageContactUs = createApi({
  reducerPath: "manageContactUs",
  baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
  tagTypes: ["Contact"],
  endpoints: (builder) => ({


    getContact: builder.query<ApiResponse, { page?: number; search?: string }>({
      query: ({ page, search }) => {
        const params = new URLSearchParams();
        if (page) params.append("page", page.toString());
        if (search) params.append("search", search);
        return `/contact-us?${params.toString()}`;
      },
      providesTags: ["Contact"],
    }),

    pendingRequest: builder.mutation({
      query: (body) => ({
        url: `/contact-us-response`,
        method: "POST",
        body,
      }),
      invalidatesTags: ["Contact"],
    }),
  }),
});

export const { useGetContactQuery, usePendingRequestMutation } = manageContactUs;
