export type DemoStatus = "Scheduled" | "Cancelled" | "Rescheduled" | "Completed"

export type DemoSlot = {
  slotId: string
  label?: string
  date?: string
  startTime?: string
  endTime?: string
  capacity?: number
  status?: "available" | "closed"
}

export type Demo = {
  _id: string
  title: string
  slug?: string
  description?: string
  date: string
  startTime: string
  endTime?: string
  timezone?: string
  googleMeetLink?: string
  googleMeetLinkAvailable?: boolean
  googleMeetLinkInfo?: string
  status: DemoStatus
  slotsEnabled?: boolean
  slots?: DemoSlot[]
  cancelledReason?: string
  rescheduledFromDate?: string
  rescheduledFromStartTime?: string
  registrationCount?: number
}

export type DemoRegistration = {
  _id: string
  demoId: string
  name?: string
  email: string
  phone?: string
  slotId?: string
  status: string
  confirmationEmailSentAt?: string | null
  reminderEmailSentAt?: string | null
  reminderScheduledFor?: string | null
  createdAt?: string
}

export function getBackendUrl() {
  if (process.env.NEXT_PUBLIC_BACKEND_URL) {
    return process.env.NEXT_PUBLIC_BACKEND_URL.replace(/\/$/, "")
  }
  if (typeof window === 'undefined') {
    return process.env.NEXT_PUBLIC_APP_URL || "http://localhost:3000"
  }
  return ""
}

export async function readApiMessage(response: Response) {
  const data = await response.json().catch(() => null)
  return data?.message || data?.error || "Request failed. Please try again."
}
