import { createApi } from "@reduxjs/toolkit/query/react";
import { baseQueryWithReauth } from "./apiSlice";
import { AboutUsResponse } from "../../Types/aboutUsType";
export const aboutUs = createApi({
  reducerPath: "aboutUs",
  baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
  endpoints: (builder) => ({



    // 🔹 GET: Fetch all posts
    getAboutUs: builder.query<AboutUsResponse, void>({
      query: () => "/about-us",
    }),





    // 🔹 GET: Fetch a single post by ID
    getPostById: builder.query<Post, number>({
      query: (id) => `/posts/${id}`,
    }),

    // 🔹 POST: Create a new post
    createPost: builder.mutation<Post, Partial<Post>>({
      query: (data) => ({
        url: "/posts",
        method: "POST",
        body: data,
      }),
    }),
    // 🔹 PUT: Update an existing post
    // updateAboutUs: builder.mutation<UpdateAboutUsResponse, UpdateAboutUsRequest>({
    //   query: ({ id, updatedData }) => ({
    //     url: `/posts/${id}`,
    //     method: "POST",
    //     body: updatedData,
    //   }),
    // }),
       // 🔹 PUT: Update an About Us entry
       updateAboutUs: builder.mutation({
        query: (updatedData) => ({
          url: "/about-us/update", // ✅ Updated URL
          method: "POST",
          body: updatedData,
        }),
      }),
    // 🔹 DELETE: Remove a post by ID
    deletePost: builder.mutation<{ success: boolean }, number>({
      query: (id) => ({
        url: `/posts/${id}`,
        method: "DELETE",
      }),
    }),
  }),
});

export const { 
  useGetAboutUsQuery, 
  useUpdateAboutUsMutation,










  
  useGetPostByIdQuery, 
  useCreatePostMutation,  
  useDeletePostMutation 
} = aboutUs;

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


export type UpdateAboutUsRequest={
  id: number; updatedData: string,language_code:string
}


export type UpdateAboutUsResponse={
  id: number; updatedData: string,language_code:string
}


export type AboutUs = {
  id: number;
  language_master_xid: number;
  content: string;
  is_active: boolean;
};

