import type { BookingRecord, BookingGroupEvent, BookingRecordStatus } from '../types/booking';
import { api } from './api';

interface PaginatedResponse<T> {
  data: T[];
  nextCursor?: string;
  total?: number;
}

export const bookingService = {
  /** My records for a given date (trainer view) */
  async getMyRecords(date: string): Promise<BookingRecord[]> {
    return api.get<BookingRecord[]>('/booking/records/my', {
      params: { date },
    });
  },

  /** All records for a date range */
  async getRecords(params: {
    from?: string;
    to?: string;
    status?: BookingRecordStatus;
    staffId?: string;
    cursor?: string;
    limit?: number;
  }): Promise<PaginatedResponse<BookingRecord>> {
    return api.get<PaginatedResponse<BookingRecord>>('/booking/records', {
      params: params as Record<string, string | number | undefined>,
    });
  },

  /** Single record by ID */
  async getRecord(id: string): Promise<BookingRecord> {
    return api.get<BookingRecord>(`/booking/records/${id}`);
  },

  /** Mark attendance: COMPLETED or NO_SHOW */
  async markAttendance(id: string, status: 'COMPLETED' | 'NO_SHOW'): Promise<BookingRecord> {
    return api.patch<BookingRecord>(`/booking/records/${id}/status`, {
      status,
    });
  },

  /** Confirm a pending record */
  async confirmRecord(id: string): Promise<BookingRecord> {
    return api.patch<BookingRecord>(`/booking/records/${id}/status`, {
      status: 'CONFIRMED',
    });
  },

  /** Cancel a record */
  async cancelRecord(id: string, reason?: string): Promise<BookingRecord> {
    return api.patch<BookingRecord>(`/booking/records/${id}/status`, {
      status: 'CANCELLED',
      cancelReason: reason,
    });
  },

  /** Group events list */
  async getGroupEvents(params: {
    date?: string;
    staffId?: string;
    isActive?: boolean;
  }): Promise<BookingGroupEvent[]> {
    return api.get<BookingGroupEvent[]>('/booking/group-events', {
      params: params as Record<string, string | number | undefined>,
    });
  },

  /** Single group event */
  async getGroupEvent(id: string): Promise<BookingGroupEvent> {
    return api.get<BookingGroupEvent>(`/booking/group-events/${id}`);
  },

  /** Group event participants */
  async getGroupEventParticipants(eventId: string): Promise<BookingRecord[]> {
    return api.get<BookingRecord[]>(`/booking/group-events/${eventId}/participants`);
  },
};
