// ── Intake ──

export type SessionType = 'guided' | 'chat'

export interface AISessionCreateRequest {
  session_type?: SessionType
  intake_type: 'free_text' | 'psa_ticket' | 'screenshot' | 'log_paste' | 'combined'
  intake_content: Record<string, unknown>
  psa_ticket_id?: string
  psa_connection_id?: string
}

export interface AISessionCreateResponse {
  session_id: string
  status: string
  confidence_tier: string
  problem_summary: string | null
  problem_domain: string | null
  matched_flow_id: string | null
  matched_flow_name: string | null
  match_score: number | null
  first_step: AISessionStepResponse
  psa_context_status: string | null  // "loaded" | "unavailable" | null
}

// ── Step interaction ──

export interface StepOptionSchema {
  label: string
  value: string
  followup_hint: string | null
}

export interface AISessionStepResponse {
  step_id: string
  step_order: number
  step_type: string
  content: Record<string, unknown>
  context_message: string | null
  options: StepOptionSchema[]
  allow_free_text: boolean
  allow_skip: boolean
  confidence_tier: string
  confidence_score: number
}

// ── Step content type guards ──

export interface ScriptGenerationContent {
  action_type: 'script_generation'
  template_id?: string
  pre_filled_params?: Record<string, string>
  instructions?: string
}

export interface ScriptBuilderContent {
  action_type: 'open_script_builder'
  script_prompt?: string
  script_language?: string
}

export function isScriptGenerationAction(content: Record<string, unknown>): content is Record<string, unknown> & ScriptGenerationContent {
  return content.action_type === 'script_generation'
}

export function isScriptBuilderAction(content: Record<string, unknown>): content is Record<string, unknown> & ScriptBuilderContent {
  return content.action_type === 'open_script_builder'
}

export function getActionType(content: Record<string, unknown>): string | undefined {
  return typeof content.action_type === 'string' ? content.action_type : undefined
}

export interface StepResponseRequest {
  selected_option?: string | null
  free_text_input?: string | null
  was_skipped?: boolean
  action_result?: Record<string, unknown> | null
}

export interface StepResponseResponse {
  session_id: string
  status: string
  confidence_tier: string
  confidence_score: number
  next_step: AISessionStepResponse | null
  resolution_suggested: boolean
  resolution_summary: string | null
}

// ── Resolution / Escalation ──

export interface ResolveSessionRequest {
  resolution_summary: string
  resolution_action?: string | null
  session_rating?: number | null
  session_feedback?: string | null
}

export interface EscalateSessionRequest {
  escalation_reason: string
  escalated_to_id?: string | null
}

export interface DocumentationStep {
  step_number: number
  step_type: string
  description: string
  engineer_response: string | null
  outcome: string | null
}

export interface SessionDocumentation {
  problem_summary: string
  problem_domain: string | null
  intake_summary: string
  diagnostic_steps: DocumentationStep[]
  resolution_summary: string | null
  escalation_reason: string | null
  follow_up_recommendations: string[]
  total_steps: number
  duration_display: string | null
  generated_at: string
}

export interface SessionCloseResponse {
  session_id: string
  status: string
  documentation: SessionDocumentation | null
  psa_push_status: string  // "sent" | "pending_retry" | "no_psa" | "failed"
  psa_push_error: string | null
  member_mapping_warning: string | null
}

export type StatusUpdateAudience = 'ticket_notes' | 'client_update' | 'email_draft' | 'request_info'
export type StatusUpdateLength = 'quick' | 'detailed'
export type StatusUpdateContext = 'status' | 'resolution' | 'escalation'

export interface StatusUpdateRequest {
  audience: StatusUpdateAudience
  length: StatusUpdateLength
  context: StatusUpdateContext
}

export interface StatusUpdateResponse {
  content: string
  audience: StatusUpdateAudience
  length: StatusUpdateLength
  context: StatusUpdateContext
  session_status: string
  steps_completed: number
  time_spent_display: string | null
  client_name: string | null
  generated_at: string
}

export interface RateSessionRequest {
  rating: number
  feedback?: string | null
}

// ── List / Detail ──

export interface AISessionSummary {
  id: string
  session_type: SessionType
  title: string | null
  status: string
  intake_type: string
  problem_summary: string | null
  problem_domain: string | null
  confidence_tier: string
  step_count: number
  session_rating: number | null
  psa_ticket_id: string | null
  escalation_reason: string | null
  created_at: string
  resolved_at: string | null
}

export interface PickupSessionRequest {
  resume_mode: 'continue' | 'fresh'
  additional_context?: string
}

export interface AISessionDetail extends AISessionSummary {
  intake_content: Record<string, unknown>
  matched_flow_id: string | null
  match_score: number | null
  resolution_summary: string | null
  resolution_action: string | null
  escalation_reason: string | null
  session_feedback: string | null
  psa_ticket_id: string | null
  psa_connection_id: string | null
  ticket_data: Record<string, unknown> | null
  steps: AISessionStepResponse[]
  conversation_messages: Array<{ role: string; content: string }>
  pending_task_lane: { questions: QuestionItem[]; actions: ActionItem[]; responses?: Array<Record<string, unknown>> } | null
  is_branching: boolean
  active_branch_id: string | null
}

export interface AISessionSearchResult {
  id: string
  problem_summary: string | null
  problem_domain: string | null
  status: string
  created_at: string
}

// ── Chat session ──

export interface ChatSessionCreateResponse {
  session_id: string
  session_type: 'chat'
  title: string
  status: string
}

export interface ChatMessageRequest {
  message: string
  upload_ids?: string[]
}

export interface ForkMetadata {
  fork_point_id: string
  fork_reason: string
  branches: Array<{ branch_id: string; label: string }>
  active_branch_id: string
}

export interface ActionItem {
  label: string
  command?: string | null
  description: string
}

export interface QuestionItem {
  text: string
  context?: string
}

export interface ChatMessageResponse {
  content: string
  suggested_flows: Array<{ tree_id: string; tree_name: string; tree_type: string; relevance_snippet: string }>
  fork?: ForkMetadata | null
  actions?: ActionItem[] | null
  questions?: QuestionItem[] | null
}

export interface SimilarSession {
  id: string
  problem_summary: string | null
  problem_domain: string | null
  status: string
  resolution_summary: string | null
  created_at: string | null
  similarity: number
}
