import type { TreeStructure } from './tree'
import type { Step, StepContent } from './step'

export type SessionOutcome = 'resolved' | 'escalated' | 'workaround' | 'unresolved'

export interface DecisionRecord {
  node_id: string
  question: string | null
  answer: string | null
  action_performed: string | null
  notes: string | null
  command_output?: string | null
  automation_used: boolean
  timestamp: string
  entered_at?: string | null
  exited_at?: string | null
  duration_seconds?: number | null
  attachments: string[]
}

export interface CustomStep {
  id: string
  inserted_after_node_id: string
  step_data: Step | CustomStepDraft
  timestamp: string
}

export interface CustomStepDraft {
  title: string
  step_type: 'decision' | 'action' | 'solution'
  content: StepContent
  category_id?: string
  tags?: string[]
}

// Tree snapshot contains the full tree structure AND metadata
export interface TreeSnapshot extends TreeStructure {
  name?: string  // Tree name stored in snapshot
  description?: string
  category?: string
  version?: number
  tree_type?: string
}

export interface Session {
  id: string
  tree_id: string
  user_id: string
  tree_snapshot: TreeSnapshot
  path_taken: string[]
  decisions: DecisionRecord[]
  custom_steps: CustomStep[]
  started_at: string
  completed_at: string | null
  outcome: SessionOutcome | null
  outcome_notes: string | null
  ticket_number: string | null
  client_name: string | null
  exported: boolean
  scratchpad: string
  next_steps: string
  session_variables: Record<string, string>
  batch_id?: string
  target_label?: string
}

export interface SessionCreate {
  tree_id: string
  ticket_number?: string
  client_name?: string
  session_variables?: Record<string, string>
}

export interface SessionUpdate {
  path_taken?: string[]
  decisions?: DecisionRecord[]
  custom_steps?: CustomStep[]
  ticket_number?: string
  client_name?: string
  scratchpad?: string
  next_steps?: string
}

export interface SessionExport {
  format: 'text' | 'markdown' | 'html' | 'psa'
  include_timestamps?: boolean
  include_tree_info?: boolean
  include_outcome_notes?: boolean
  include_next_steps?: boolean
  max_step_index?: number
  include_summary?: boolean
  detail_level?: 'standard' | 'full'
  redaction_mode?: 'none' | 'mask'
}

export interface RedactionSummary {
  ips: number
  emails: number
  tokens: number
  unc_paths: number
  total: number
}

export interface SessionComplete {
  outcome: SessionOutcome
  outcome_notes?: string
  next_steps?: string
}

// Navigation state for active session
export interface SessionNavigationState {
  activeSession: Session | null
  currentNodeId: string
  pathTaken: string[]
  decisions: DecisionRecord[]
  isLoading: boolean
  error: string | null
}

// Save session as tree
export interface SaveAsTreeRequest {
  tree_name?: string
  description?: string
  status: 'draft' | 'published'
}

export interface SaveAsTreeResponse {
  tree_id: string
  tree_name: string
  message: string
}

// Session Sharing
export type SessionShareVisibility = 'public' | 'account'

export interface SessionShareCreate {
  visibility: SessionShareVisibility
  share_name?: string
  expires_at?: string // ISO datetime string
}

export interface SessionShare {
  id: string
  session_id: string
  account_id: string
  share_token: string
  share_name: string | null
  visibility: SessionShareVisibility
  created_by: string
  created_at: string
  updated_at: string
  expires_at: string | null
  view_count: number
  last_viewed_at: string | null
  is_active: boolean
  share_url: string | null
}

export interface SharedSessionView {
  session_id: string
  tree_name: string
  tree_description: string | null
  tree_structure: TreeStructure & Record<string, unknown>
  path_taken: string[]
  decisions: DecisionRecord[]
  custom_steps: CustomStep[]
  started_at: string
  completed_at: string | null
  ticket_number: string | null
  client_name: string | null
  share_name: string | null
  visibility: SessionShareVisibility
}
