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

export interface CountryData {
    id: number;
    en_name: string;
    hi_name: string;
    mr_name: string;
    te_name: string;
    ta_name: string;
    bn_name: string;
    or_name: string;
    country_code: string;
    phonecode: string;
    capital: string;
    currency: string;
    currency_name: string;
    currency_symbol: string;
    is_active: string;
}

interface ApiResponse {
    status: string;
    status_code: number;
    message: string;
    data: {
        current_page: number,
        last_page: number,
        total: number,
        from: number,
        per_page: number,
        to: number,
        data: CountryData[];
    };
}

export interface CountryEdit {
    status: string;
    status_code: number;
    message: string;
    data: CountryData[];
}


export type PostCountry = {
    en_name: string;
    country_code: string;
    phonecode: string;
    capital: string;
    currency: string;
    currency_name: string;
    currency_symbol: string;
};


export const countryMaster = createApi({
    reducerPath: "countryMaster",
    baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
    endpoints: (builder) => ({
        createCountryPost: builder.mutation<PostCountry, Partial<PostCountry>>({
            query: (data) => ({
                url: "/country-add",
                method: "POST",
                body: data,
            }),
        }),
        // 🔹 GET: Fetch all posts
        // getCountryMaster: builder.query<ApiResponse, number>({
        //     query: (page = 1) => `/country-list?page=${page}`,
        // }),

        getCountryMaster: 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 `/country-list?${params.toString()}`;
            },
        }),

        getCountryMasterEdit: builder.query<CountryEdit, number>({
            query: (id) => `/country-edit/${id}`,
        }),

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

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

        // deleteFaqPost: builder.mutation<{ status: string; message: string }, { id: number }>({
        //     query: ({ id }) => ({
        //         url: `/faq-delete`,
        //         method: "POST",
        //         body: { id },
        //     }),
        // }),

    }),
});

export const {
    useGetCountryMasterQuery,
    useGetCountryMasterEditQuery,
    useCreateCountryPostMutation,
    useUpdateCountryMutation,
    useCountryToggleMutation,
    // useDeleteFaqPostMutation
} = countryMaster;