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

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

interface UserData {
  id: number;
  unique_id: string;
  user_name: string;
  first_name: string;
  last_name: string;
  date_of_birth: string;
  gender: string;
}

interface PaginatedData {
  current_page: number;
  data: UserData[];
  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;
}

interface ApiResponse {
  status: string;
  status_code: number;
  message: string;
  data: PaginatedData;
}

export type Permission = {
  id: number;
  app_resource_title: string;
  is_active: boolean;
  created_by: string | null;
  modified_by: string | null;
  deleted_at: string | null;
  created_at: string;
  updated_at: string;
};

export type PermissionResponse = {
  status: string;
  status_code: number;
  message: string;
  data: {
    permission: Permission[];
  };
};
// export type SubAdminPost = {
//   id: number;
//   first_name: string,
//   last_name: string,
//   unique_id: string,
//   date_of_birth: string,
//   gender: string,
// }

interface ResourceActionLink {
  id: number;
  principal_xid: number;
  role_xid: number;
  app_resource_xid: number;
  is_active: boolean;
  created_by: number | null;
  modified_by: number | null;
  deleted_at: string | null;
  created_at: string;
  updated_at: string;
  app_resource: {
    id: number;
    app_resource_title: string
  }
}

interface SubAdmin {
  id: number;
  first_name: string;
  last_name: string;
  unique_id: string;
  date_of_birth: string;
  gender: string;
  get_resource_action_link: ResourceActionLink[];
}

interface SubAdminView {
  status: string;
  status_code: number;
  message: string;
  data: SubAdmin[];
}

interface CreateSubAdminPayload {
  // user_name: string;
  first_name: string;
  last_name: string;
  date_of_birth: string;
  gender: string;
  email_address: string;
  phone_number: string;
  // created_by: number;
}

interface CreateSubAdminResponse {
  status: string;
  status_code: number;
  message: string;
  data: UserData;
}

export const manageSubAdmin = createApi({
  reducerPath: "manageSubAdmin",
  baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
  tagTypes: ["SubAdmin"],
  endpoints: (builder) => ({

    createFaqPost: builder.mutation<Post, Partial<Post>>({
      query: (data) => ({
        url: "/faq-store",
        method: "POST",
        body: data,
      }),
    }),

    getSubAdmin: 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 `/sub-admin?${params.toString()}`
      }
    }),

    getPermission: builder.query<PermissionResponse, void>({
      query: () => `/resources`,
    }),

    viewSubAdmin: builder.query<SubAdminView, number>({
      query: (id) => `/sub-admin-view/${id}`,
    }),

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

    createSubAdminPost: builder.mutation<
      CreateSubAdminResponse,
      CreateSubAdminPayload
    >({
      query: (data) => ({
        url: "/sub-admin-create",
        method: "POST",
        body: data,
      }),
      invalidatesTags: ["SubAdmin"], // Add this to invalidate cache
    }),

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

    deleteSubAdminPost: builder.mutation<{ success: boolean }, number>({
      query: (id) => ({
        url: `/sub-admin-delete`,
        method: "POST",
        body: { id },
      }),
    }),
  }),
});

export const {
  useGetSubAdminQuery,
  useGetPermissionQuery,
  useLazyViewSubAdminQuery,
  useUpdateSubAdminMutation,
  useDeleteSubAdminPostMutation,
  useCreateSubAdminPostMutation,
} = manageSubAdmin;

export type Post = {
  id: number;
  title: string;
  body: string;
};
