import { apiFetch } from "@/lib/api/server";
import type { MyTicket, TicketDetail, TicketDownload } from "@/types/ticket";
import type { PaginatedResponse } from "@/types/event";

export interface TicketListParams {
  status?: string;
  page?: number;
}

/** GET /tickets — FR-069/FR-074. */
export async function getMyTickets(params: TicketListParams = {}) {
  const query = new URLSearchParams();
  for (const [key, value] of Object.entries(params)) {
    if (value !== undefined && value !== "") query.set(key, String(value));
  }
  const qs = query.toString();

  return apiFetch<PaginatedResponse<MyTicket>>(`/tickets${qs ? `?${qs}` : ""}`, { auth: true });
}

/** GET /tickets/{id} — FR-070. */
export async function getMyTicket(id: number | string) {
  return apiFetch<{ data: MyTicket }>(`/tickets/${id}`, { auth: true });
}

/** GET /tickets/{id}/detail — Detail Ticket / E-Ticket pages. */
export async function getMyTicketDetail(id: number | string) {
  return apiFetch<{ data: TicketDetail }>(`/tickets/${id}/detail`, { auth: true });
}

/** GET /tickets/{id}/download — FR-075. */
export async function getMyTicketDownload(id: number | string) {
  return apiFetch<{ data: TicketDownload }>(`/tickets/${id}/download`, { auth: true });
}
