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

export interface JobStatusData {
    id: number;
    is_active: string;
    job_status_translation:{
        job_status_xid: number,
        title:string,
        language_xid:number
    }[],
}

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: JobStatusData[];
    };
}

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


export type PostJobStatus = {
 title: string
};


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

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

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

        jobStatusToggle: builder.mutation({
            query: ({ id, is_active }) => ({
                url: `/job-status-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 {
    useGetJobStatusQuery,
    useCreateJobStatusPostMutation,
    useUpdateJobStatusMutation,
    useJobStatusToggleMutation,
    // useDeleteFaqPostMutation
} = jobStatus;