import { z } from "zod";

export const bookingStatusSchema = z.enum([
  "draft",
  "held",
  "pending",
  "pending_payment",
  "confirmed",
  "cancelled",
  "expired",
  "completed",
  "no_show",
  "refunded",
]);

export type BookingStatus = z.infer<typeof bookingStatusSchema>;

const transitions: Record<BookingStatus, BookingStatus[]> = {
  draft: ["held", "pending", "cancelled"],
  held: ["pending", "pending_payment", "confirmed", "expired", "cancelled"],
  pending: ["confirmed", "cancelled"],
  pending_payment: ["confirmed", "cancelled", "expired"],
  confirmed: ["completed", "cancelled", "no_show", "refunded"],
  cancelled: [],
  expired: [],
  completed: ["refunded"],
  no_show: ["refunded"],
  refunded: [],
};

export function canTransitionBooking(
  from: BookingStatus,
  to: BookingStatus
) {
  return transitions[from].includes(to);
}

export function assertBookingTransition(from: BookingStatus, to: BookingStatus) {
  if (!canTransitionBooking(from, to)) {
    throw new Error(`Invalid booking transition: ${from} -> ${to}`);
  }
}

export function getAllowedBookingTransitions(from: BookingStatus) {
  return [...transitions[from]];
}
