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

export interface Agency {
  id: number;
  name: string;
  state: string;
  rc_status: string;
  rc_number: string;
  domain_name: string;
  gst_number: string;
  is_active: boolean,
  registered_office: string,
}

export interface AgencyData {
  current_page: number;
  data: Agency[];
  first_page_url: string;
  from: number;
  last_page: number;
  last_page_url: string;
  per_page: number;
  total: number;
}

export interface AgencyResponse {
  status: "success" | "error";
  status_code: number;
  message: string;
  data: AgencyData;
}

export interface UpdatePrivacyPolicyPayload {
  id: number,
  is_active: boolean,
}

export interface AgencyPost {
  raid?: string;
  name: string;
  auth_signatory?: string;
  state: string;
  district?: string;
  rc_number: string;
  contact_details?: string;
  registered_office: string;
  branch_office?: string;
  registered_email?: string;
  other_email?: string;
  registered_contact?: string;
  website?: string;
  domain_name: string;
  staff_domain_name?: string;
  gst_number: string;
  rc_status?: "Active" | "Inactive"; // Assuming it's a status with limited values
}


export const agencyMasterModule = createApi({
  reducerPath: "agencyMasterModule",
  baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
  endpoints: (builder) => ({

    createAgencyMasterPost: builder.mutation<AgencyPost, Partial<AgencyPost>>({
      query: (data) => ({
        url: "/agency-store",
        method: "POST",
        body: data,
      }),
    }),

    // getAgencyMaster: builder.query<AgencyResponse, number>({
    //   query: (page = 1) => `/agency-master?page=${page}`
    // }),

    getAgencyMaster: builder.query<AgencyResponse, { 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 `/agency-master?${params.toString()}`;
      },
    }),

    agencyMasterToggle: builder.mutation({
      query: ({ id, is_active }) => ({
        url: `/agency-status`,
        method: "POST",
        body: { id, is_active },
      }),
    }),

    updateAgencyMaster: builder.mutation({
      query: (updatedData) => ({
        url: "/agency-update",
        method: "POST",
        body: updatedData,
      }),
    }),

  }),
});

export const {
  useCreateAgencyMasterPostMutation,
  useGetAgencyMasterQuery,
  useAgencyMasterToggleMutation,
  useUpdateAgencyMasterMutation,
} = agencyMasterModule;
                                                                                                                                                                     