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

interface JobTypeResponse {
    status: string;
    status_code: number;
    message: string;
    data: PaginationData;
}

interface PaginationData {
    current_page: number;
    data: JobTypeData[];
    first_page_url: string;
    from: number;
    last_page: number;
    last_page_url: string;
    links: PaginationLink[];
    next_page_url: string | null;
    path: string;
    per_page: number;
    prev_page_url: string | null;
    to: number;
    total: number;
}

export interface JobTypeData {
    id: number;
    is_active: boolean;
    en_name: string;
    hi_name: string;
    mr_name:string,
    te_name:string,
    ta_name:string,
    bn_name:string,
    or_name:string,
}


interface PaginationLink {
    url: string | null;
    label: string;
    active: boolean;
}

export interface Post {
    en_name: string
}

export const jobType = createApi({
    reducerPath: "jobType",
    baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
    endpoints: (builder) => ({
        createJobTypePost: builder.mutation<Post, Partial<Post>>({
            query: (data) => ({
                url: "/job-type-add",
                method: "POST",
                body: data,
            }),
        }),
        // 🔹 GET: Fetch all posts
        getJobType: builder.query<JobTypeResponse, number>({
            query: (page = 1) => `/job-type?page=${page}`,
        }),

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

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

    }),
});

export const {
    useGetJobTypeQuery,
    useCreateJobTypePostMutation,
    useUpdateJobTypeMutation,
    useJobTypeToggleMutation,
} = jobType;
