export type ReservationRecord = Record<string, unknown>

export type ReservationOrderStatus = 'TEMPORARY' | 'ACTIVE' | 'ENDED' | 'LOGIC_DELETED'

export type ReservationTaskStatus =
  | 'PENDING_CONFIRM'
  | 'READY'
  | 'EXECUTING'
  | 'FAILED'
  | 'COMPLETED'
  | 'BLOCKED'

export type ReservationOperaOperationStatus = 'PENDING' | 'EXECUTING' | 'SUCCEEDED' | 'FAILED'

export type ReservationTaskCardType =
  | 'NEW_BOOKING'
  | 'UPDATE_BOOKING'
  | 'CANCEL_BOOKING'
  | 'CANCEL_ALLOTMENT'
  | 'VOUCHER'
  | 'ROOMING_LIST'
  | 'AMEND_GROUP_CODE'
  | 'TRACE_NOTE'
  | 'TA_RECORD'
  | 'MESSAGE_NOTIFICATION'
  | 'SOURCE_MESSAGE_ONLY'
  | 'MANUAL_REVIEW'
  | 'FALLBACK_REVIEW'

export type ReservationAiResultType =
  | 'normal_task'
  | 'manual_review'
  | 'source_message_review_notification'
  | 'adapter_contract_error'
  | 'unhandled_current_intent'
  | string

export type ReservationSystemProcessCategory =
  | 'BUSINESS_TASK'
  | 'SOURCE_MESSAGE_NOTIFICATION'
  | 'ADAPTER_CONTRACT_ERROR'
  | 'UNHANDLED_CURRENT_INTENT'
  | string

export interface ReservationPageResult {
  page_num: number
  page_size: number
  total: number
}

export interface ReservationOrderListFilters {
  hotel_id?: string
  order_status?: string
  group_code?: string
  confirmation_number?: string
  keyword?: string
  page_num?: number
  page_size?: number
}

export interface ReservationOrderListItem {
  order_id: string
  hotel_id: string
  order_status: ReservationOrderStatus | string
  display_order_key: string | null
  temporary_order_no: string | null
  confirmation_number: string | null
  group_code: string | null
  display_name: string
  open_task_count: number | null
  next_processable_task_id: string | null
  created_at: string | null
  updated_at: string | null
}

export interface ReservationOrderListResult {
  items: ReservationOrderListItem[]
  page: ReservationPageResult
}

export type ReservationTaskListTaskTypeFilter =
  | ''
  | 'NEW_BOOKING'
  | 'UPDATE_BOOKING'
  | 'CANCEL_BOOKING'
  | 'MANUAL_REVIEW'
  | 'SOURCE_MESSAGE_ONLY'

export type ReservationTaskListTaskSubtypeFilter =
  | ''
  | 'NEW_BOOKING'
  | 'UPDATE_BOOKING'
  | 'CANCEL_BOOKING'
  | 'RATE_CHANGE'
  | 'UPDATE_STAY_DATES'
  | 'UPDATE_ROOM_TYPE'
  | 'UPDATE_FIX_CHARGE'
  | 'NEW_GROUP_BLOCK'
  | 'NEW_FIT_RESERVATION'
  | 'MANUAL_REVIEW'
  | 'S10'
  | 'S99'

export interface ReservationTaskListFilters {
  hotel_id?: string
  order_id?: string
  task_type?: ReservationTaskListTaskTypeFilter
  task_subtype?: ReservationTaskListTaskSubtypeFilter
  task_status?: string
  order_status?: string
  queue_participation?: boolean
  keyword?: string
  page_num?: number
  page_size?: number
}

export interface ReservationTaskListItem {
  task_id: string
  order_id: string
  hotel_id: string
  display_order_key: string | null
  temporary_order_no: string | null
  task_type: string
  task_subtype: string | null
  result_type?: ReservationAiResultType | null
  ai_task_type?: string | null
  route_code?: string | null
  system_process_category?: ReservationSystemProcessCategory | null
  task_status: ReservationTaskStatus | string
  card_name: string | null
  queue_sequence: number | null
  queue_participation: boolean
  can_process: boolean
  readonly_reason_code: string | null
  source_message_id: string | null
  source_subject: string | null
  source_sender_summary?: string | null
  source_received_at?: string | null
  external_conversation_id?: string | null
  conversation_message_count?: number | null
  created_at: string | null
  updated_at: string | null
}

export interface ReservationTaskListResult {
  items: ReservationTaskListItem[]
  page: ReservationPageResult
}

export interface ReservationOrderSummaryResult {
  order_id: string
  hotel_id: string
  order_status: ReservationOrderStatus | string
  temporary_order_no: string | null
  confirmation_number: string | null
  group_code: string | null
  block_code: string | null
  allotment_code: string | null
  display_name: string
  created_at: string | null
  updated_at: string | null
}

export interface ReservationOrderTaskTimelineItem {
  task_id: string
  task_type: string
  task_subtype: string | null
  result_type?: ReservationAiResultType | null
  ai_task_type?: string | null
  route_code?: string | null
  system_process_category?: ReservationSystemProcessCategory | null
  task_status: ReservationTaskStatus | string
  card_name: string | null
  queue_sequence: number | null
  queue_participation: boolean
  can_process: boolean
  readonly_reason_code: string | null
  source_message_id?: string | null
  source_subject?: string | null
  source_sender_summary?: string | null
  source_received_at?: string | null
  external_conversation_id?: string | null
  conversation_message_count?: number | null
  created_at: string | null
}

export interface ReservationOrderDetailResult {
  order: ReservationOrderSummaryResult
  tasks: ReservationOrderTaskTimelineItem[]
  warnings: string[]
}

export interface SourceMessageConversationResult {
  conversation: {
    hotel_id: string
    channel: string
    external_conversation_id: string | null
    subject: string | null
    message_count: number
    first_received_at: string | null
    last_received_at: string | null
  }
  messages: SourceMessageConversationItem[]
}

export interface SourceMessageConversationItem {
  id: string
  external_message_id: string | null
  external_conversation_id: string | null
  sender_summary: string | null
  subject: string | null
  received_at: string | null
  source_sent_at: string | null
  text_body: string | null
  html_body: string | null
  html_body_sanitized: string | null
  html_sanitize_required: boolean
  html_render_mode: 'SANITIZED_HTML' | 'TEXT_ONLY' | string | null
  inline_images: SourceMessageOriginalMedia[]
  attachments: SourceMessageOriginalMedia[]
  related_orders: SourceMessageRelatedOrder[]
  related_tasks: SourceMessageRelatedTask[]
}

export interface SourceMessageRelatedOrder {
  order_id: string
  display_order_key: string | null
  order_status: ReservationOrderStatus | string
}

export interface SourceMessageRelatedTask {
  task_id: string
  order_id: string
  task_type: string
  task_subtype: string | null
  task_status: ReservationTaskStatus | string
  card_name: string | null
}

export interface SourceMessageOriginalMedia {
  mediaType: string | null
  fileName: string | null
  contentType: string | null
  sizeBytes: number | null
  externalUrl: string | null
  externalMediaId: string | null
}

export interface ReservationWorkbenchFilters {
  business_key?: string
  order_status?: string
  task_status?: string
  task_card_type?: string
}

export interface ReservationWorkbenchItem {
  order_id: string
  hotel_id: string
  display_name: string
  order_key_type: string
  order_business_key: string | null
  active_business_key: string | null
  temporary_order_code: string | null
  order_status: ReservationOrderStatus | string
  business_key_source: string
  source_message_id: string
  guest_name: string
  room_type: string
  arrival_date: string
  departure_date: string
  amount: number
  currency_code: string
  current_task_id: string
  current_task_card_type: ReservationTaskCardType | string
  current_task_status: ReservationTaskStatus | string
  updated_at: string
}

export interface ReservationTaskQueueItem {
  task_id: string
  order_id: string
  system_task_type: string
  task_card_type: ReservationTaskCardType | string
  task_status: ReservationTaskStatus | string
  queue_participation: boolean
  execution_order: number
  blocked_by_task_id: string | null
  updated_at: string
}

export interface ReservationSourceMessageSummary {
  source_message_id: string
  channel: string
  sender_identifier: string
  thread_subject: string
  payload_received_at: string
  summary: string
}

export interface ReservationOrderDetail extends ReservationWorkbenchItem {
  tasks: ReservationTaskQueueItem[]
  source_message: ReservationSourceMessageSummary
}

export interface ReservationTaskAvailabilityResult {
  blocked: boolean
  read_only: boolean
  editable: boolean
  confirmable: boolean
  executable: boolean
  blocked_by_task_id: string | null
  blocked_reason: string | null
}

export interface ReservationTaskFieldResult {
  row_number: number | null
  card_name: string | null
  result_type?: string | null
  task_type?: string | null
  task_subtype?: string | null
  display_area: string | null
  field_path: string
  field_pointer?: string | null
  legacy_field_path?: string | null
  display_name: string
  default_value_source?: string | null
  visible: string | null
  editable: string | null
  input_editable: string | null
  select_editable: string | null
  date_picker: string | null
  number_input: string | null
  file_display: string | null
  table_editable: string | null
  enum_options: string | null
  required_rule: string | null
  display_condition: string | null
  validation_rule: string | null
  write_path: string | null
  opera_write_participation: string | null
  opera_parameter_mapping: string | null
  notes: string | null
  value: unknown
}

export interface ReservationTaskDetailResult {
  task_id: string
  order_id: string
  source_message_id: string
  source_subject?: string | null
  source_sender_summary?: string | null
  source_received_at?: string | null
  external_conversation_id?: string | null
  conversation_message_count?: number | null
  result_type?: ReservationAiResultType | null
  ai_task_type?: string | null
  task_subtype?: string | null
  route_code?: string | null
  system_process_category?: ReservationSystemProcessCategory | null
  system_task_type: string
  task_card_type: ReservationTaskCardType | string
  task_status: ReservationTaskStatus | string
  field_contract_version: string
  draft_payload: ReservationRecord | null
  confirmed_payload: ReservationRecord | null
  review_status?: string | null
  review_resolution?: ReservationRecord | null
  manual_review?: ReservationRecord | null
  context_used?: ReservationRecord | null
  availability: ReservationTaskAvailabilityResult
  fields: ReservationTaskFieldResult[]
  opera_operations: ReservationOperaOperationResult[]
  source_message_only_result?: ReservationSourceMessageOnlyResult | null
  adapter_contract_errors?: ReservationAiTransitionDisplayResult[]
  unhandled_intents?: ReservationAiTransitionDisplayResult[]
}

export interface ReservationSourceMessageOnlyResult {
  entry_result_code: string | null
  entry_result_meaning: string | null
  entry_result_description: string | null
  entry_result_source_message_id: string | null
  result_type?: ReservationAiResultType | null
  route_code?: string | null
  agent_assessment?: ReservationRecord | null
  notification?: ReservationRecord | null
  manual_review?: ReservationRecord | null
  raw_answer: string | null
}

export interface ReservationAiTransitionDisplayResult {
  transition_id?: string | null
  source_event_index?: number | null
  array_index?: number | null
  result_type?: ReservationAiResultType | null
  ai_task_type?: string | null
  task_subtype?: string | null
  route_code?: string | null
  system_process_category?: ReservationSystemProcessCategory | null
  adapter_error_code?: string | null
  adapter_error_message?: string | null
  payload_fragment?: ReservationRecord | null
}

export interface ReservationTaskPayloadMutationRequest {
  field_values: ReservationRecord
}

export interface ReservationTaskPayloadMutationResult {
  task_id: string
  order_id: string
  task_status: ReservationTaskStatus | string
  draft_payload: ReservationRecord | null
  confirmed_payload: ReservationRecord | null
  opera_operations: ReservationOperaOperationResult[]
}

export type ReservationManualReviewTargetTaskType = 'NEW_BOOKING' | 'UPDATE_BOOKING' | 'CANCEL_BOOKING'

export interface ReservationManualReviewConversionRequest {
  target_task_type: ReservationManualReviewTargetTaskType
  target_order_id?: string
  reason?: string
}

export interface ReservationManualReviewConversionResult {
  task_id: string
  order_id: string
  original_order_id: string
  system_task_type: string
  task_card_type: ReservationTaskCardType | string
  task_status: ReservationTaskStatus | string
  original_order_logic_deleted: boolean
}

export type ReservationManualReviewResolutionFieldOverrideRequest = {
  field_pointer: string
  field_path?: string
  value: unknown
} | {
  field_pointer?: string
  field_path: string
  value: unknown
}

export interface ReservationManualReviewResolutionRequest {
  confirmed_order_id?: string
  reason?: string
  field_overrides: ReservationManualReviewResolutionFieldOverrideRequest[]
}

export interface ReservationManualReviewResolutionResult {
  task_id: string
  order_id: string
  task_status: ReservationTaskStatus | string
  review_status: string | null
  review_resolution: ReservationRecord | null
  confirmed_payload: ReservationRecord | null
  opera_operations: ReservationOperaOperationResult[]
}

export interface ReservationOperaOperationResult {
  operation_id: string
  task_id: string
  operation_sequence: number
  operation_code: string
  operation_name: string
  operation_status: ReservationOperaOperationStatus | string
  attempt_count: number
  last_error_message: string | null
  attempts: ReservationOperaOperationAttemptResult[]
}

export interface ReservationOperaOperationAttemptResult {
  attempt_id: string
  attempt_number: number
  attempt_status: ReservationOperaOperationStatus | string
  response_payload: ReservationRecord | null
  error_message: string | null
  started_at: string | null
  finished_at: string | null
}

export interface ReservationOperaSimulationRequest {
  simulate_success?: boolean
  failure_message?: string
}

export interface ReservationTaskAuditListResult {
  task_id: string
  items: ReservationTaskAuditLogResult[]
}

export interface ReservationTaskAuditLogResult {
  audit_id: string
  order_id: string
  task_id: string
  operation_id: string | null
  actor_type: string
  actor_id: string
  action: string
  reason: string | null
  before_snapshot: ReservationRecord | null
  after_snapshot: ReservationRecord | null
  occurred_at: string
}

export interface BackendHealthResult {
  status: string
  service: string
}
