import type { StructuredUiRequest } from "./playbook-contracts.js";
/** Public theme tokens accepted by the Akintu widget runtime. */
export interface WidgetTheme {
  borderColors: string[];
  buttonColor: string;
  primaryColor: string;
  placeholder: string;
}

/** Tenant configuration returned by the public widget endpoint. */
export interface WidgetConfigResponse {
  sessionVersion?: number;
  embedEnabled: boolean;
  inlineEnabled: boolean;
  theme: WidgetTheme;
  accessibility: {
    stt: boolean;
    tts: boolean;
  };
}

/** One chat turn sent from an Embed or Inline surface. */
export interface ChatRequest {
  sessionId: string;
  message: string;
  context?: string;
  clientCapabilities?: ("interactive_booking" | "structured_playbooks")[];
}

export interface CalendlyBookingSlot {
  startTime: string;
  endTime?: string;
  schedulingUrl?: string;
}

export interface CalendlyBookingOffer {
  provider?: "calendly" | "google_calendar";
  eventTypeName: string;
  availableTimes: CalendlyBookingSlot[];
}

export interface PrepareCalendlyBookingRequest {
  provider?: "calendly" | "google_calendar";
  sessionId: string;
  name: string;
  email: string;
  timezone: string;
  startTime: string;
  endTime?: string;
  schedulingUrl?: string;
}

export interface ConversationFeedbackRequest {
  sessionId: string;
  messageId?: string;
  resolved: boolean;
  rating?: number;
}

/** Validated historical message restored into the widget conversation. */
export interface ChatHistoryMessage {
  id: string;
  role: string;
  content: string;
  citedChunkIds: string[] | null;
  createdAt: string | null;
}

/** Versioned conversation history returned by the public API. */
export interface ChatHistoryResponse {
  sessionId: string;
  messages: ChatHistoryMessage[];
  externalAction?: (CalendlyEmbeddedBooking & { actionId: string }) | null;
  structuredUiRequest?: StructuredUiRequest | null;
}

export interface ActionConfirmation {
  actionId: string;
  toolName: string;
  summary: string;
  confirmationToken: string;
  expiresAt: string;
}

export interface CalendlyEmbeddedBooking {
  kind: "calendly_inline_booking";
  schedulingUrl: string;
  eventTypeName: string;
  inviteeName: string;
  inviteeEmail: string;
  timezone: string;
  requestedStartTime: string;
}

export interface CalendlyScheduledEvent {
  eventUri: string;
  inviteeUri: string;
}

interface IntegrationActionResponseBase {
  id: string;
  toolName: string;
  summary: string;
}

export type IntegrationActionResponse = IntegrationActionResponseBase &
  (
    | {
        status: "awaiting_external_completion";
        result: CalendlyEmbeddedBooking;
      }
    | {
        status: "succeeded" | "failed";
        result?: Record<string, unknown>;
      }
  );
/** Events emitted by the public chat SSE stream. */
export type ChatSseEvent =
  | { type: "token"; content: string }
  | { type: "sources"; chunkIds: string[] }
  | ({ type: "action_confirmation" } & ActionConfirmation)
  | { type: "booking_slots"; offer: CalendlyBookingOffer }
  | { type: "structured_ui"; request: StructuredUiRequest }
  | { type: "done"; messageId: string }
  | { type: "error"; error: string };
