export type BookingRecordStatus =
  | 'PENDING'
  | 'CONFIRMED'
  | 'IN_PROGRESS'
  | 'COMPLETED'
  | 'CANCELLED'
  | 'NO_SHOW';

export type BookingServiceType = 'INDIVIDUAL' | 'GROUP';

export type BookingSource = 'ADMIN' | 'WIDGET' | 'API' | 'CRM';

export interface BookingServiceSummary {
  id: string;
  name: string;
  type: BookingServiceType;
  durationMin: number;
  price: number;
  color?: string;
}

export interface BookingStaffSummary {
  id: string;
  firstName: string;
  lastName: string;
  avatarUrl?: string;
}

export interface BookingClientSummary {
  id: string;
  firstName?: string;
  lastName?: string;
  phone: string;
}

export interface BookingRecord {
  id: string;
  clubId: string;
  number: number;
  clientId?: string;
  clientName: string;
  clientPhone: string;
  clientEmail?: string;
  serviceId: string;
  staffId: string;
  groupEventId?: string;
  startAt: string;
  endAt: string;
  status: BookingRecordStatus;
  source: BookingSource;
  places: number;
  price: number;
  paidAmount: number;
  notes?: string;
  cancelReason?: string;
  confirmedAt?: string;
  completedAt?: string;
  cancelledAt?: string;
  createdAt: string;
  updatedAt: string;
  // Relations (included when expanded)
  service?: BookingServiceSummary;
  staff?: BookingStaffSummary;
  client?: BookingClientSummary;
}

export interface BookingGroupEvent {
  id: string;
  clubId: string;
  serviceId: string;
  staffId: string;
  roomId?: string;
  title: string;
  startAt: string;
  endAt: string;
  maxParticipants: number;
  currentParticipants: number;
  isRecurring: boolean;
  isActive: boolean;
  service?: BookingServiceSummary;
  staff?: BookingStaffSummary;
}

export interface BookingDaySummary {
  date: string;
  records: BookingRecord[];
  totalCount: number;
  confirmedCount: number;
  completedCount: number;
}

export const STATUS_LABELS: Record<BookingRecordStatus, string> = {
  PENDING: 'Ожидание',
  CONFIRMED: 'Подтверждено',
  IN_PROGRESS: 'В процессе',
  COMPLETED: 'Завершено',
  CANCELLED: 'Отменено',
  NO_SHOW: 'Не пришёл',
};

export const STATUS_COLORS: Record<BookingRecordStatus, string> = {
  PENDING: '#FF9800',
  CONFIRMED: '#2196F3',
  IN_PROGRESS: '#9C27B0',
  COMPLETED: '#43A047',
  CANCELLED: '#9CA3AF',
  NO_SHOW: '#E53935',
};

export function formatBookingTime(startAt: string, endAt: string): string {
  const start = new Date(startAt);
  const end = new Date(endAt);
  const fmt = (d: Date) =>
    `${d.getHours().toString().padStart(2, '0')}:${d.getMinutes().toString().padStart(2, '0')}`;
  return `${fmt(start)} – ${fmt(end)}`;
}

export function getBookingDuration(startAt: string, endAt: string): number {
  return Math.round((new Date(endAt).getTime() - new Date(startAt).getTime()) / 60000);
}
