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

export interface DepartmentData {
    id: number;
    industry_masters_xid: number;
    en_name: string;
    hi_name: string;
    mr_name: string;
    te_name: string;
    ta_name: string;
    bn_name: string;
    or_name: 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: DepartmentData[];
    };
}

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

export interface DropDown{
    status: string;
    status_code: number;
    message: string;
    data: {
        id: number;
        en_name: string;
    }[]
}

export type PostDepartment = {
    en_name: string;
    industry_masters_xid: number;
};


export const departmentMaster = createApi({
    reducerPath: "departmentMaster",
    baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
    endpoints: (builder) => ({
        createDepartmentPost: builder.mutation<PostDepartment, Partial<PostDepartment>>({
            query: (data) => ({
                url: "/department-master-store",
                method: "POST",
                body: data,
            }),
        }),
        // 🔹 GET: Fetch all posts
        // getDepartmentMaster: builder.query<ApiResponse, number>({
        //     query: (page = 1) => `/department-master-list?page=${page}`,
        // }),

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

        getDepartmentMasterDropDown: builder.query<DropDown, void>({
            query: () => `/industry-master-get-category`,
        }),

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

        departmentToggle: builder.mutation({
            query: ({ id, is_active }) => ({
                url: `/department-master-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 {
    useGetDepartmentMasterQuery,
    useGetDepartmentMasterDropDownQuery,
    useCreateDepartmentPostMutation,
    useUpdateDepartmentMutation,
    useDepartmentToggleMutation,
    // useDeleteFaqPostMutation
} = departmentMaster;