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

export interface JobStatusData {
  id: number;
  job_title: string;
  workspace_mode_xid: number;
  industry_xid: number;
  department_xid: number;
  company_name: string;
  ctc_currency_symbol: string;
  ctc_amount: string;
  experience: string;
  job_location: string;
  country_xid: number;
  job_type_xid: number;
  skill_description: string;
  job_description: string;
  job_image: string | null;
  industry: {
    id: number;
    en_name: string;
  };
  department: {
    id: number;
    en_name: string;
  };
  workspace: {
    id: number;
    en_name: string;
  };
  country: {
    id: number;
    en_name: string;
  };
  job_type: {
    id: number;
    en_name: 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: JobStatusData[];
  };
}

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


export type PostJobStatus = { 
  title: string
};

export type WorkSpace = {
  id: number;
  en_name: string;
};

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

    // getManageJobs: builder.query<ApiResponse, number>({
    //   query: (page = 1) => `/manage-jobs-list?page=${page}`,
    // }),

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

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

    viewJobs: builder.query<ApiResponse, number>({
      query: () => `/manage-jobs-list`,
    }),

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

    // Modes
    getWorkspaceModes: builder.query({
      query: () => `/manage-jobs-get-workspace`,
    }),

    getIndustry: builder.query({
      query: () => `/manage-jobs-get-industry`,
    }),

    getDepartment: builder.query({
      query: () => `/manage-jobs-get-department`,
    }),

    getCountry: builder.query({
      query: () => `/manage-jobs-get-country`,
    }),

    getManageJobType: builder.query({
      query: () => `/job-type`,
    }),

  }),
});

export const { 
  useGetManageJobsQuery,
  useLazyViewJobsQuery,
  useDeleteJobsPostMutation,
  useUpdateJobsMutation,
  useGetWorkspaceModesQuery,
  useGetIndustryQuery,
  useGetDepartmentQuery,
  useGetCountryQuery,
  useGetManageJobTypeQuery
} = manageJobs;
