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

export interface TicketDto {
  id: string;
  eventId?: string;
  status?: string;
  [key: string]: any;
}

interface TicketsApiResponse {
  totalItems: number;
  totalPages: number;
  currentPage: number;
  data: TicketDto[];
  message: string;
}

export class TicketService {
  static async getTickets(
    token: string,
    params?: { status?: string; limit?: number; page?: number }
  ): Promise<TicketDto[]> {
    const { status, limit = 50, page = 1 } = params || {};

    const res = await callEndpoint<TicketsApiResponse>(
      ticketEndpoints.getTickets,
      {
        query: {
          ...(status ? { status } : {}),
          limit,
          page,
        },
        headers: {
          Authorization: `Bearer ${token}`,
        },
      }
    );

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

    return res.data ?? [];
  }

  static async buyTicket(
    token: string,
    payload: { ticketTierId: string; eventId: string; ticketCount: number }
  ): Promise<any> {
    const res = await callEndpoint<any>(ticketEndpoints.buyTicket, {
      body: {
        ticketTierId: payload.ticketTierId,
        eventId: payload.eventId,
        ticketCount: payload.ticketCount,
      },
      headers: {
        Authorization: `Bearer ${token}`,
      },
    });

    console.log("[TicketService] buy ticket response", res);
    return res;
  }
}
