// PocketBase collection types

export interface SourceDocument {
  id: string
  filename: string
  format: 'pdf' | 'md' | 'txt'
  status: 'processing' | 'processed' | 'failed'
  ingested_at: string
  chunk_count: number
  created_by: string
}

export interface Theme {
  id: string
  title: string
  description: string
  status: 'draft' | 'published' | 'rejected'
  source_documents: string[]
  approved_by: string
  approved_at: string
  created_at: string
  updated_at: string
  expand?: { source_documents?: SourceDocument[] }
}

export interface Topic {
  id: string
  theme: string
  title: string
  body: string
  difficulty: 'introductory' | 'intermediate' | 'advanced'
  complexity_weight: number
  status: 'draft' | 'published'
  related_topics: string[]
  prerequisite_topics: string[]
  contrast_topics: string[]
  key_terms: string[]
  qdrant_chunk_ids: string[]
  created_at: string
  updated_at: string
  expand?: {
    theme?: Theme
    related_topics?: Topic[]
    prerequisite_topics?: Topic[]
  }
}

export interface QuizOption {
  label: string
  text: string
  correct: boolean
  explanation: string
}

export interface MisconceptionItem {
  misconception: string
  correction: string
}

export interface HowToStep {
  number: number
  instruction: string
}

export interface ComparisonDimension {
  label: string
  a: string
  b: string
}

export interface Flashcard {
  question: string
  answer: string
}

export type MicroLearningType =
  | 'concept_explainer'
  | 'scenario_quiz'
  | 'misconceptions'
  | 'how_to'
  | 'comparison_card'
  | 'reflection_prompt'
  | 'flashcard_set'
  | 'case_study'
  | 'glossary_anchor'
  | 'myth_vs_evidence'

export type MicroLearningContent =
  | { type: 'concept_explainer'; paragraphs: string[]; example: string }
  | { type: 'scenario_quiz'; scenario: string; options: QuizOption[] }
  | { type: 'misconceptions'; items: MisconceptionItem[] }
  | { type: 'how_to'; steps: HowToStep[] }
  | {
      type: 'comparison_card'
      subject_a: string
      subject_b: string
      dimensions: ComparisonDimension[]
    }
  | { type: 'reflection_prompt'; prompt: string; model_answer: string }
  | { type: 'flashcard_set'; cards: Flashcard[] }
  | { type: 'case_study'; scenario: string; questions: string[] }
  | {
      type: 'glossary_anchor'
      term: string
      definition: string
      correct_use: string
      misuse: string
    }
  | { type: 'myth_vs_evidence'; myth: string; evidence: string; sources: string[] }

export interface MicroLearning {
  id: string
  topic: string
  type: MicroLearningType
  content: MicroLearningContent
  status: 'queued' | 'generated' | 'published' | 'rejected'
  generation_model: string
  generated_at: string
  published_at: string
  updated_at: string
}

export interface CurriculumVersion {
  id: string
  version: number
  status: 'draft' | 'active' | 'superseded'
  generated_at: string
  approved_by: string
  approved_at: string
  generation_notes: string
}

export interface CurriculumWeek {
  id: string
  curriculum_version: string
  week_number: number
  theme: string
  topics: string[]
  topic_order: number[]
  estimated_duration_minutes: number
  admin_notes: string
  expand?: { theme?: Theme; topics?: Topic[] }
}

export interface EmployeeCurriculumState {
  id: string
  user: string
  current_cycle: number
  current_week: number
  start_date: string
  active_version: string
  updated_at: string
}

export interface SessionCompletion {
  id: string
  user: string
  topic: string
  micro_learning: string
  week_number: number
  cycle: number
  completed_at: string
}

export interface GamificationProfile {
  id: string
  user: string
  total_commits: number
  current_level: 'intern' | 'junior' | 'medior' | 'senior' | 'staff' | 'principal'
  current_streak_weeks: number
  longest_streak_weeks: number
  types_used: string[]
  last_active_week: number
  updated_at: string
  expand?: { user?: PBUser }
}

export interface Badge {
  id: string
  key: string
  tier: 'bronze' | 'silver' | 'gold' | 'legendary' | 'content'
  label: string
  description: string
  icon: string
}

export interface EmployeeBadge {
  id: string
  user: string
  badge: string
  earned_at: string
  cycle: number
  expand?: { badge?: Badge }
}

export interface MilestoneCard {
  id: string
  user: string
  cycle: number
  week: number
  total_commits: number
  streak_weeks: number
  badge_keys: string[]
  created_at: string
  expand?: { user?: PBUser }
}

export interface PBUser {
  id: string
  email: string
  role: 'admin' | 'employee'
  display_name: string
  avatar?: string
}

// Service response types

export interface IngestionJobStatus {
  jobId: string
  status:
    | 'queued'
    | 'extracting'
    | 'chunking'
    | 'structuring'
    | 'writing'
    | 'embedding'
    | 'done'
    | 'failed'
  chunksTotal: number
  chunksEmbedded: number
  themesFound: number
  topicsFound: number
  reason?: string
}

export interface CompletePayload {
  userId: string
  topicId: string
  microLearningId: string
  weekNumber: number
}

export interface CompleteResponse {
  commitsEarned: number
  newBadges: Badge[]
  totalCommits: number
}

export interface LeaderboardEntry {
  userId: string
  displayName: string
  totalCommits: number
  currentStreak: number
  typesUsed: number
  badgeCount: number
  level: string
}

export interface FeedEntry {
  id: string
  displayName: string
  cycle: number
  week: number
  totalCommits: number
  streakWeeks: number
  badgeKeys: string[]
  createdAt: string
}

export type R42SSEEvent =
  | { type: 'chunk'; text: string }
  | { type: 'citations'; topics: Array<{ id: string; title: string }> }
  | { type: 'out_of_scope'; text: string }
  | { type: 'done' }
