import { callEndpoint } from "../api/client";
import { eventEndpoints } from "../api/eventEndpoints";

export interface EventDto {
  id: string;
  name: string;
  startDate: string;
  endDate: string;
  venue: string;
  status: string;
  ticketCutOff: string;
  images: string[];
  [key: string]: any;
}

interface EventsApiResponse {
  totalItems: number;
  totalPages: number;
  currentPage: number;
  data: EventDto[];
  message: string;
}

interface EventDetailApiResponse {
  message: string;
  data: EventDto;
}

export class EventService {
  static async getEvents(
    token: string,
    params?: { status?: string; limit?: number }
  ): Promise<EventDto[]> {
    const { status = "ACTIVE", limit = 50 } = params || {};

    const res = await callEndpoint<EventsApiResponse>(
      eventEndpoints.getEvents,
      {
        query: { status, limit },
        headers: {
          Authorization: `Bearer ${token}`,
        },
      }
    );

    console.log("[EventService] events response", {
      totalItems: res.totalItems,
      currentPage: res.currentPage,
      dataLength: Array.isArray(res.data) ? res.data.length : null,
    });

    return res.data ?? [];
  }

  static async getEventById(
    token: string,
    id: string
  ): Promise<EventDto | null> {
    if (!id) return null;

    const endpoint = {
      base: eventEndpoints.getEventById.base,
      path: `${eventEndpoints.getEventById.path}/${id}`,
      method: eventEndpoints.getEventById.method,
    } as const;

    const res = await callEndpoint<EventDetailApiResponse>(endpoint, {
      headers: {
        Authorization: `Bearer ${token}`,
      },
    });

    return res.data ?? null;
  }
}
