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

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

export interface WebinarItem {
  id: string;
  session_title: string;
  description: string | null;
  session_datetime: string;
  duration_minutes: number;
  timezone_xid: string;
  max_attendee: number;
  passcode: string;
  require_registration: boolean;
  recurring_webinar: boolean;
  webinar_status: "scheduled" | "live" | "ended" | "cancelled";
  owner?: string;
}

export interface WebinarListData {
  total: number;
  limit: number;
  offset: number;
  items: WebinarItem[];
}

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

/* ================= QUERY PARAM TYPE ================= */

export interface WebinarListParams {
  limit?: number;
  offset?: number;
  search?: string;
  status?: string[]; // ✅ multiple status
  fromDate?: string;
  toDate?: string;
  minDuration?: number;
  maxDuration?: number;
  minAttendees?: number; // ✅ NEW
  maxAttendees?: number; // ✅ NEW
  sortBy?: "most_popular" | "newest" | "oldest" | "title" | "duration";
}

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

export const webinarApi = createApi({
  reducerPath: "webinarApi",
  baseQuery: baseQueryWithReauth,
  tagTypes: ["Webinar"],

  endpoints: (builder) => ({
    webinarList: builder.query<WebinarListResponse, WebinarListParams>({
      query: ({
        limit = 10,
        offset = 0,
        search,
        status,
        fromDate,
        toDate,
        minDuration,
        maxDuration,
        minAttendees,
        maxAttendees,
        sortBy,
      }) => {
        const params = new URLSearchParams();

        params.append("limit", String(limit));
        params.append("offset", String(offset));

        if (search) {
          params.append("search_term", search); // ✅ FIXED NAME
        }

        if (status && status.length > 0) {
          status.forEach((s) =>
            params.append("session_status", s) // ✅ array support
          );
        }

        if (fromDate) {
          params.append("from_date", fromDate);
        }

        if (toDate) {
          params.append("to_date", toDate);
        }

        if (minDuration !== undefined) {
          params.append("min_duration", String(minDuration));
        }

        if (maxDuration !== undefined) {
          params.append("max_duration", String(maxDuration));
        }

        if (minAttendees !== undefined) {
          params.append("min_attendee", String(minAttendees)); // ✅ NEW
        }

        if (maxAttendees !== undefined) {
          params.append("max_attendee", String(maxAttendees)); // ✅ NEW
        }

        if (sortBy) {
          params.append("sort_by", sortBy);
        }

        return `/admin/webinars/list?${params.toString()}`;
      },

      providesTags: ["Webinar"],
    }),
  }),
});

/* ================= EXPORT HOOK ================= */

export const { useWebinarListQuery } = webinarApi;