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

export interface BlogTag {
  id: string;
  blog_xid: string;
  tag_name: string;
  display_order: number;
}

export interface BlogItem {
  id: string;
  content_category: string;
  content_type: string;
  title: string;
  slug_name: string;
  content: string;
  banner_img: string;
  meta_title: string;
  meta_description: string;
  content_status: string;
  updated_at: string;
  blog_tags: BlogTag[];
  short_description: string | null;
  content_category_id?: string; // Add this field to store the category ID
}

export interface Pagination {
  limit: number;
  offset: number;
  total: number;
}

export interface BlogListResponse {
  success: boolean;
  status: number;
  message: string;
  data: {
    pagination: Pagination;
    items: BlogItem[];
  };
  errors: any;
  correlation_id: string;
}

export interface BlogListParams {
  limit?: number;
  offset?: number;
  search?: string;
  content_status?: string;
  content_type?: string;
  date_range?:
    | "all_time"
    | "last_7_days"
    | "last_30_days"
    | "last_3_months"
    | "last_6_months";
  sort_by?: "most_recent" | "oldest_first" | "title_az";
  content_category_id?: string; // Changed from category to content_category_id
  tag_id?: string;
}

export interface BlogByIdResponse {
  success: boolean;
  status: number;
  message: string;
  data: BlogItem;
  errors: any;
  correlation_id: string;
}

export const blogApi = createApi({
  reducerPath: "blogApi",
  baseQuery: baseQueryWithReauth,
  tagTypes: ["blog"],
  endpoints: (builder) => ({
    // ✅ GET BLOGS LIST
    getBlogs: builder.query<BlogListResponse, BlogListParams>({
      query: ({
        limit = 10,
        offset = 0,
        search,
        content_status = "publish",
        content_type,
        date_range,
        sort_by = "most_recent",
        content_category_id,
        tag_id,
      }) => {
        // Build params object
        const params: Record<string, any> = {
          limit,
          offset,
          sort_by,
        };

        // Only add params if they have values
        if (search) params.search = search;
        if (content_status) params.content_status = content_status;
        if (content_type) params.content_type = content_type;
        if (date_range) params.date_range = date_range;
        if (content_category_id)
          params.content_category_id = content_category_id; // Send UUID
        if (tag_id && tag_id !== "all") params.tag_id = tag_id;

        return {
          url: "/admin/blogs/list",
          method: "GET",
          params,
        };
      },
      providesTags: ["blog"],
    }),

    getBlogByID: builder.query<BlogItem, string>({
      query: (id) => ({
        url: `/admin/blogs/list/${id}`,
        method: "GET",
      }),
      transformResponse: (response: BlogByIdResponse) => response.data,
      providesTags: ["blog"],
    }),
  }),
});

export const { useGetBlogsQuery, useGetBlogByIDQuery } = blogApi;
