import { createContext } from "react";

export type GlobalContextType = {
  roomName?: string;
  name?: string;
  sendJsonMessage?: (message: any) => void;
  chat?: Array<unknown>;
  socketUrl?: string;
  readyState?: any;
  session?: Session;
  lastJsonMessage?: any;
};

const global: GlobalContextType = {
  roomName: undefined,
  name: "",
  socketUrl: undefined,
  chat: [],
  session: undefined,
  lastJsonMessage: undefined,
};

const GlobalContext = createContext(global);

/**
 * RoomModel
 * @description Core room model used across components
 */
export interface RoomModel {
  /** Id */
  id: string;
  /** Name */
  name: string;
  /**
   * Private
   * @default false
   */
  private?: boolean;
}

/**
 * RoomCreateData
 * @description Data for room creation
 */
export interface RoomCreateData {
  /** Name */
  name: string;
  /**
   * Private
   * @default false
   */
  private?: boolean;
}
/**
 * RoomCreateRequest
 * @description Request for creating a room
 */
export interface RoomCreateRequest {
  /**
   * Type
   * @constant
   */
  type: "room_create";
  data: RoomCreateData;
}
/**
 * RoomCreateResponse
 * @description Response for room creation
 */
export interface RoomCreateResponse {
  /**
   * Type
   * @constant
   */
  type: "room_created";
  data: RoomModel;
}
/**
 * RoomListItem
 * @description Room item for list responses
 */
export interface RoomListItem {
  /** Id */
  id: string;
  /** Name */
  name: string;
}
/**
 * RoomModel
 * @description Core room model used across components
 */
export interface RoomModel {
  /** Id */
  id: string;
  /** Name */
  name: string;
  /**
   * Private
   * @default false
   */
  private?: boolean;
}

/**
 * SessionResponse
 * @description Session response model
 */
export interface SessionResponse {
  /** Id */
  id: string;
  /** Name */
  name: string;
  /** Lobbies */
  lobbies: RoomModel[];
  /**
   * Protected
   * @default false
   */
  protected?: boolean;
  /**
   * Has Media
   * @default false
   */
  has_media?: boolean;
  /** Bot Run Id */
  bot_run_id?: string | null;
  /** Bot Provider Id */
  bot_provider_id?: string | null;
  /** Bot Instance Id */
  bot_instance_id?: string | null;
}

// Re-export types from api-client for backwards compatibility
export type Room = RoomModel;

// Extended Session type that allows name to be null initially (before user sets it)
export type Session = Omit<SessionResponse, "name"> & {
  name: string | null;
  has_media?: boolean; // Whether this session provides audio/video streams
};

export { GlobalContext };
