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

/* ================= HERO TYPES ================= */

export interface HeroSection {
  id: string;
  landing_page_type: string;
  background_image_url: string;
  background_image_alt_text: string;
  headline: string;
  subtext: string;
  cta_text: string;
  cta_destination: string;
}

/* ================= STATS TYPES ================= */

export interface StatItem {
  id: string;
  landing_page_type: string;
  number: number;
  suffix: string;
  label: string;
  display_order: number;
}

/* ================= HIGHLIGHT CARD ================= */

export interface HighlightCard {
  id?: string;
  card_title: string;
  icon_url: string;
  accessible_label: string;
  body_text: string;
  display_order: number;
}

/* ================= CTA BAND ================= */

export interface CtaBand {
  id: string;
  background_image_url: string;
  background_image_alt_text: string;
  text: string;
  cta_text: string;
  cta_destination: string;
}

/* ================= TESTIMONIAL TYPES ================= */

export interface TestimonialItem {
  id: string;
  profile_xid: string;
  name: string;
  designation: string;
  content: string;
  video_url: string | null;
  display_order: number;
}

/* ================= CTA SECTION TYPES ================= */

export interface CtaSection {
  id: string;
  background_image_url: string;
  text: string;
  cta_text: string;
  cta_destination: string;
  description: string;
  landing_page_type: string;
  service_type: string | null;
}

/* ================= RESPONSE ================= */

export interface HomePageResponse {
  success: boolean;
  status: number;
  message: string;
  data: {
    hero_sections: HeroSection[];
    stats_sections: StatItem[];
    highlight_cards: HighlightCard[];
    cta_bands: CtaBand[];
    cta_section: CtaSection;
    testimonial_section: TestimonialItem[];
  };
  errors: any;
  correlation_id: string;
}

/* ================= API ================= */

export const homepageApi = createApi({
  reducerPath: "homepageApi",
  baseQuery: baseQueryWithReauth,
  tagTypes: ["Homepage"],
  endpoints: (builder) => ({

    getHomepage: builder.query<
      HomePageResponse["data"],
      { landing_page_type: "home" | "services" | "about_us" }
    >({
      query: ({ landing_page_type }) => ({
        url: "/admin/home-page/list",
        params: { landing_page_type },
      }),

      transformResponse: (response: HomePageResponse) => response.data,

      providesTags: [{ type: "Homepage", id: "LIST" }],
    }),

    getFeaturedBlogs: builder.query({
      query: ({ limit = 3 }) => ({
        url: `/admin/blogs/featured?limit=${limit}`,
        method: 'GET',
      }),
      transformResponse: (response: any) => {
        if (response?.success && response?.data) {
          return response.data;
        }
        return [];
      },
    }),

  }),
});

/* ================= HOOKS ================= */

export const { useGetHomepageQuery, useGetFeaturedBlogsQuery } = homepageApi;