import { createContext, useContext } from "react";
import { Booking } from "../types/type";

/**
 * Host-app options for the chat page.
 *
 * Deliberately small: tabs, search, and service grouping are driven by the
 * chat API itself, so they are not options. Only the booking *detail panel*
 * needs data the API does not return (totals, payment breakdown, history).
 */
export interface ChatUIOptions {
  /** Booking shown in the detail panel for the selected conversation. */
  booking?: Booking | null;
  /** Resolver for apps holding many bookings; takes precedence over `booking`. */
  getBooking?: (conversationId: string) => Booking | null | undefined;
  /** Hides the right-hand panel regardless of booking data. */
  showBookingPanel?: boolean;
  onAcceptBooking?: (booking: Booking) => void;
  onCancelBooking?: (booking: Booking) => void;
}

const ChatUIOptionsContext = createContext<ChatUIOptions>({});

export const ChatUIOptionsProvider = ChatUIOptionsContext.Provider;

export const useChatUIOptions = () => useContext(ChatUIOptionsContext);

/**
 * Resolves the booking for a conversation, preferring the resolver over the
 * single-booking prop. Returns null when the host app supplied neither.
 */
export const useBookingFor = (conversationId?: string): Booking | null => {
  const { booking, getBooking } = useChatUIOptions();
  if (conversationId && getBooking) return getBooking(conversationId) ?? null;
  return booking ?? null;
};
