All files / web/src/lib/vision/recording VisionRecorder.ts

0% Statements 0/977
0% Branches 0/1
0% Functions 0/1
0% Lines 0/977

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
import { eq } from 'drizzle-orm'
import { createId } from '@paralleldrive/cuid2'
import { mkdir, writeFile, rm } from 'fs/promises'
import path from 'path'
import { db } from '@/db'
import {
  visionProblemVideos,
  type VisionProblemVideoStatus,
  type NewVisionProblemVideo,
} from '@/db/schema/vision-problem-videos'
import { VideoEncoder } from './VideoEncoder'

/**
 * Frame data received from vision-frame socket events
 */
export interface VisionFrame {
  sessionId: string
  imageData: string // base64 JPEG
  detectedValue: number | null
  confidence: number
  timestamp: number // Unix timestamp in ms
}

/**
 * Problem marker data (without offsetMs, which is calculated)
 */
export interface ProblemMarkerInput {
  problemNumber: number
  partIndex: number
  eventType: 'problem-shown' | 'answer-submitted' | 'feedback-shown'
  isCorrect?: boolean
  /** Epoch number: 0 = initial pass, 1-2 = retry epochs */
  epochNumber: number
  /** Attempt number within the epoch (1-indexed) */
  attemptNumber: number
  /** Whether this is a retry (epoch > 0) */
  isRetry: boolean
  /** Whether this is a manual redo (student clicked on completed problem) */
  isManualRedo: boolean
  /** Unique slot ID tying this recording to the specific problem slot (same ID used in answer validation) */
  slotId: string
  /** Problem data sent with 'problem-shown' markers (avoids relying on stale practice state) */
  problem?: { terms: number[]; answer: number }
}

/**
 * Practice state data for metadata capture
 */
export interface PracticeStateInput {
  currentProblem: { terms: number[]; answer: number }
  phase: 'problem' | 'feedback' | 'tutorial'
  studentAnswer: string
  isCorrect: boolean | null
  currentProblemNumber: number
}

/**
 * Snapshot of practice state for metadata capture
 */
interface PracticeStateSnapshot {
  problem: { terms: number[]; answer: number } | null
  studentAnswer: string
  phase: 'problem' | 'feedback'
  isCorrect: boolean | null
}

/**
 * Metadata entry for a single frame
 */
export interface ProblemMetadataEntry {
  /** Timestamp in ms from video start */
  t: number
  /** Detected abacus value from vision */
  detectedValue: number | null
  /** Detection confidence 0-1 */
  confidence: number
  /** Student's typed answer */
  studentAnswer: string
  /** Current phase */
  phase: 'problem' | 'feedback'
  /** Whether answer is correct (only in feedback phase) */
  isCorrect?: boolean
}

/**
 * Full metadata for a problem video
 */
export interface ProblemMetadata {
  /** Unique slot ID matching the session plan's problem slot */
  slotId: string
  /** Problem details */
  problem: { terms: number[]; answer: number }
  /** Time-coded entries */
  entries: ProblemMetadataEntry[]
  /** Duration in ms */
  durationMs: number
  /** Total frames */
  frameCount: number
  /** Final result */
  isCorrect: boolean | null
}

/**
 * Recording state for a single problem within a session
 */
interface ProblemRecording {
  videoId: string
  problemNumber: number
  partIndex: number
  /** Unique slot ID matching the session plan's problem slot */
  slotId: string
  /** Epoch number: 0 = initial pass, 1-2 = retry epochs */
  epochNumber: number
  /** Attempt number within the epoch (1-indexed) */
  attemptNumber: number
  /** Whether this is a retry (epoch > 0) */
  isRetry: boolean
  /** Whether this is a manual redo (student clicked on completed problem) */
  isManualRedo: boolean
  framesDir: string
  frameCount: number
  startedAt: Date
  lastFrameAt: Date
  isCorrect?: boolean
  /** Problem details from the marker (not from stale state) */
  problem: { terms: number[]; answer: number } | null
  /** Metadata entries for this problem */
  metadata: ProblemMetadataEntry[]
}

/**
 * Recording session state - tracks active session and current problem
 */
interface RecordingSession {
  sessionId: string
  playerId: string
  sessionStartedAt: Date
  currentProblem: ProblemRecording | null
  /** Latest practice state for metadata capture */
  latestPracticeState: PracticeStateSnapshot
}

/**
 * Configuration for the VisionRecorder
 */
export interface VisionRecorderConfig {
  /** Base directory for storing recordings (default: data/uploads/vision-recordings) */
  uploadDir: string
  /** Target frames per second (default: 5) */
  targetFps: number
  /** Maximum frame age in ms before dropping (default: 200) */
  maxFrameAgeMs: number
  /** Recording retention period in days (default: 7) */
  retentionDays: number
  /** Size of the ring buffer for live DVR in seconds (default: 60) */
  dvrBufferSeconds: number
}

const DEFAULT_CONFIG: VisionRecorderConfig = {
  uploadDir: 'data/uploads/vision-recordings',
  targetFps: 5,
  maxFrameAgeMs: 200,
  retentionDays: 7,
  dvrBufferSeconds: 60,
}

/**
 * Current problem info for DVR scrubbing
 */
interface CurrentProblemInfo {
  problemNumber: number
  startMs: number // Offset from session start
}

/**
 * Callback for when a problem video is ready
 */
export type VideoReadyCallback = (data: {
  sessionId: string
  problemNumber: number
  durationMs: number
  videoUrl: string
}) => void

/**
 * Callback for when a problem video encoding fails
 */
export type VideoFailedCallback = (data: {
  sessionId: string
  problemNumber: number
  error: string
}) => void

/**
 * VisionRecorder - Server-side service for recording vision frames during practice sessions.
 *
 * Records frames per-problem (not per-session), allowing:
 * - Incremental encoding as each problem completes
 * - Direct access to individual problem videos
 * - Click on any problem in observer nav to see that problem's video
 *
 * Usage:
 *   const recorder = VisionRecorder.getInstance()
 *   recorder.startSession(sessionId, playerId)
 *   recorder.addFrame(frameData)
 *   recorder.onProblemMarker(sessionId, marker) // Triggers encoding on problem transitions
 *   await recorder.stopSession(sessionId)
 */
export class VisionRecorder {
  private static instance: VisionRecorder | null = null

  private config: VisionRecorderConfig
  private activeSessions: Map<string, RecordingSession> = new Map()
  private frameTimestamps: Map<string, number[]> = new Map() // For FPS tracking
  private dvrBuffers: Map<string, VisionFrame[]> = new Map() // Ring buffer for DVR
  private currentProblemInfo: Map<string, CurrentProblemInfo> = new Map() // For per-problem DVR scrubbing

  // Callbacks for notifying observers when videos are ready
  private onVideoReady: VideoReadyCallback | null = null
  private onVideoFailed: VideoFailedCallback | null = null

  private constructor(config: Partial<VisionRecorderConfig> = {}) {
    this.config = { ...DEFAULT_CONFIG, ...config }
  }

  /**
   * Get the singleton instance
   */
  static getInstance(config?: Partial<VisionRecorderConfig>): VisionRecorder {
    if (!VisionRecorder.instance) {
      VisionRecorder.instance = new VisionRecorder(config)
    }
    return VisionRecorder.instance
  }

  /**
   * Reset the singleton (for testing)
   */
  static resetInstance(): void {
    VisionRecorder.instance = null
  }

  /**
   * Set callback for when a problem video is ready
   */
  setVideoReadyCallback(callback: VideoReadyCallback): void {
    this.onVideoReady = callback
  }

  /**
   * Set callback for when a problem video encoding fails
   */
  setVideoFailedCallback(callback: VideoFailedCallback): void {
    this.onVideoFailed = callback
  }

  /**
   * Start recording session for a practice session.
   * Does NOT create any directories yet - those are created per-problem.
   */
  startSession(sessionId: string, playerId: string): void {
    // Check if already recording
    if (this.activeSessions.has(sessionId)) {
      console.log(`[VisionRecorder] Already recording session ${sessionId}`)
      return
    }

    const now = new Date()

    // Initialize session state (no current problem yet)
    const session: RecordingSession = {
      sessionId,
      playerId,
      sessionStartedAt: now,
      currentProblem: null,
      latestPracticeState: {
        problem: null,
        studentAnswer: '',
        phase: 'problem',
        isCorrect: null,
      },
    }

    this.activeSessions.set(sessionId, session)
    this.frameTimestamps.set(sessionId, [])
    this.dvrBuffers.set(sessionId, [])

    console.log(`[VisionRecorder] Started session for ${sessionId}`)
  }

  /**
   * Update practice state for metadata capture.
   * Called when practice-state socket event is received.
   *
   * IMPORTANT: This captures metadata even when no video frames are being recorded.
   * This ensures we have a record of student answers for all problems.
   */
  onPracticeState(sessionId: string, state: PracticeStateInput): void {
    const session = this.activeSessions.get(sessionId)
    if (!session) {
      return
    }

    const prevState = session.latestPracticeState

    // Update latest practice state snapshot
    session.latestPracticeState = {
      problem: state.currentProblem,
      studentAnswer: state.studentAnswer,
      phase: state.phase === 'tutorial' ? 'problem' : state.phase,
      isCorrect: state.isCorrect,
    }

    // If we have a current problem recording and no problem details yet, capture them
    if (session.currentProblem && !session.currentProblem.problem && state.currentProblem) {
      session.currentProblem.problem = state.currentProblem
    }

    // Add metadata entry when state changes (even without video frames)
    // This ensures we capture student input regardless of camera status
    const problem = session.currentProblem
    if (problem) {
      const stateChanged =
        prevState.studentAnswer !== state.studentAnswer ||
        prevState.phase !== (state.phase === 'tutorial' ? 'problem' : state.phase) ||
        prevState.isCorrect !== state.isCorrect

      if (stateChanged) {
        const now = Date.now()
        const tFromProblemStart = now - problem.startedAt.getTime()
        const phase = state.phase === 'tutorial' ? 'problem' : state.phase

        const metadataEntry: ProblemMetadataEntry = {
          t: tFromProblemStart,
          detectedValue: null, // No frame data
          confidence: 0,
          studentAnswer: state.studentAnswer,
          phase,
          ...(phase === 'feedback' && state.isCorrect !== null
            ? { isCorrect: state.isCorrect }
            : {}),
        }
        problem.metadata.push(metadataEntry)
      }
    }
  }

  /**
   * Handle problem marker - this is the key method for per-problem recording.
   *
   * When 'problem-shown' is received:
   * - Finalize and encode the previous problem (if any)
   * - Start recording for the new problem
   *
   * When 'answer-submitted' is received:
   * - Store the correctness for the current problem
   */
  async onProblemMarker(sessionId: string, marker: ProblemMarkerInput): Promise<void> {
    const session = this.activeSessions.get(sessionId)
    if (!session) {
      console.warn(`[VisionRecorder] Cannot add marker - no session for ${sessionId}`)
      return
    }

    const offsetMs = Date.now() - session.sessionStartedAt.getTime()

    // Track current problem for DVR scrubbing
    if (marker.eventType === 'problem-shown') {
      this.currentProblemInfo.set(sessionId, {
        problemNumber: marker.problemNumber,
        startMs: offsetMs,
      })

      // If there was a previous problem being recorded, finalize and encode it
      if (session.currentProblem) {
        console.log(
          `[VisionRecorder] Problem ${session.currentProblem.problemNumber} ended, starting encoding`
        )
        await this.finalizeProblem(session)
      }

      // Start recording for the new problem
      await this.startProblemRecording(
        session,
        marker.problemNumber,
        marker.partIndex,
        marker.epochNumber,
        marker.attemptNumber,
        marker.isRetry,
        marker.isManualRedo,
        marker.problem ?? null,
        marker.slotId
      )

      console.log(
        `[VisionRecorder] Problem ${marker.problemNumber} started at offset ${offsetMs}ms for session ${sessionId}`
      )
    } else if (marker.eventType === 'answer-submitted') {
      // Store correctness on the current problem
      if (session.currentProblem) {
        session.currentProblem.isCorrect = marker.isCorrect
        console.log(
          `[VisionRecorder] Problem ${marker.problemNumber} answered: ${marker.isCorrect ? 'correct' : 'incorrect'}`
        )
      }
    }
  }

  /**
   * Start recording for a specific problem
   */
  private async startProblemRecording(
    session: RecordingSession,
    problemNumber: number,
    partIndex: number,
    epochNumber: number,
    attemptNumber: number,
    isRetry: boolean,
    isManualRedo: boolean,
    problem: { terms: number[]; answer: number } | null,
    slotId: string
  ): Promise<void> {
    const videoId = createId()
    // New filename pattern: problem_NNN_eX_aY.mp4
    const baseName = `problem_${problemNumber.toString().padStart(3, '0')}_e${epochNumber}_a${attemptNumber}`
    const filename = `${baseName}.mp4`
    const framesDir = path.join(
      this.config.uploadDir,
      session.playerId,
      session.sessionId,
      `${baseName}.frames`
    )
    const now = new Date()
    const expiresAt = new Date(now.getTime() + this.config.retentionDays * 24 * 60 * 60 * 1000)

    // Create frames directory
    await mkdir(framesDir, { recursive: true })

    // Create database record
    const newVideo: NewVisionProblemVideo = {
      id: videoId,
      sessionId: session.sessionId,
      playerId: session.playerId,
      problemNumber,
      partIndex,
      epochNumber,
      attemptNumber,
      isRetry,
      isManualRedo,
      filename,
      startedAt: now,
      expiresAt,
      status: 'recording',
    }

    await db.insert(visionProblemVideos).values(newVideo)

    // Update session's current problem
    session.currentProblem = {
      videoId,
      problemNumber,
      partIndex,
      slotId,
      epochNumber,
      attemptNumber,
      isRetry,
      isManualRedo,
      framesDir,
      frameCount: 0,
      startedAt: now,
      lastFrameAt: now,
      problem: problem ?? session.latestPracticeState.problem, // Prefer marker data over potentially stale state
      metadata: [],
    }

    console.log(
      `[VisionRecorder] Started problem ${problemNumber} (e${epochNumber}/a${attemptNumber}) recording: ${videoId} -> ${framesDir}`
    )
  }

  /**
   * Finalize the current problem and trigger encoding
   *
   * IMPORTANT: Metadata is always written, even if there are no video frames.
   * This ensures we have a record of student answers for all problems.
   */
  private async finalizeProblem(session: RecordingSession): Promise<void> {
    const problem = session.currentProblem
    if (!problem) return

    const endedAt = new Date()
    const durationMs = endedAt.getTime() - problem.startedAt.getTime()
    const avgFps = problem.frameCount > 0 ? (problem.frameCount / durationMs) * 1000 : 0
    const hasVideo = problem.frameCount > 0

    // Determine initial status based on whether we have video frames
    const initialStatus: VisionProblemVideoStatus = hasVideo ? 'processing' : 'no_video'

    // Update database record
    await db
      .update(visionProblemVideos)
      .set({
        status: initialStatus,
        endedAt,
        durationMs,
        frameCount: problem.frameCount,
        avgFps,
        isCorrect: problem.isCorrect,
      })
      .where(eq(visionProblemVideos.id, problem.videoId))

    // Write metadata JSON - ALWAYS, even without video frames
    // This ensures we capture student answers for playback/review
    if (problem.problem || problem.metadata.length > 0) {
      const metadata: ProblemMetadata = {
        slotId: problem.slotId,
        problem: problem.problem ?? { terms: [], answer: 0 }, // Fallback if problem details weren't captured
        entries: problem.metadata,
        durationMs,
        frameCount: problem.frameCount,
        isCorrect: problem.isCorrect ?? null,
      }

      // Match the new filename pattern: problem_NNN_eX_aY.meta.json
      const baseName = `problem_${problem.problemNumber.toString().padStart(3, '0')}_e${problem.epochNumber}_a${problem.attemptNumber}`
      const metadataPath = path.join(
        this.config.uploadDir,
        session.playerId,
        session.sessionId,
        `${baseName}.meta.json`
      )

      try {
        // Ensure directory exists (may not exist if no frames were captured)
        const dir = path.dirname(metadataPath)
        await mkdir(dir, { recursive: true })

        await writeFile(metadataPath, JSON.stringify(metadata, null, 2))
        console.log(
          `[VisionRecorder] Wrote metadata for problem ${problem.problemNumber}: ${problem.metadata.length} entries (${hasVideo ? 'with video' : 'no video'})`
        )
      } catch (error) {
        console.error(
          `[VisionRecorder] Failed to write metadata for problem ${problem.problemNumber}:`,
          error
        )
      }
    }

    console.log(
      `[VisionRecorder] Finalized problem ${problem.problemNumber}: ${problem.frameCount} frames, ${(durationMs / 1000).toFixed(1)}s, ${avgFps.toFixed(1)} fps`
    )

    // Clear current problem
    session.currentProblem = null

    // Only trigger encoding if we have frames
    if (hasVideo) {
      this.encodeProblemVideo(problem.videoId, session.sessionId, problem.problemNumber).catch(
        (error) => {
          console.error(
            `[VisionRecorder] Encoding failed for problem ${problem.problemNumber}:`,
            error
          )
        }
      )
    } else {
      // Clean up the empty frames directory if it exists
      try {
        await rm(problem.framesDir, { recursive: true, force: true })
      } catch {
        // Ignore if directory doesn't exist
      }
    }
  }

  /**
   * Add a frame to the current problem's recording.
   * Handles frame rate limiting and DVR buffer management.
   */
  async addFrame(frame: VisionFrame): Promise<boolean> {
    const session = this.activeSessions.get(frame.sessionId)
    if (!session) {
      return false
    }

    const now = Date.now()
    const frameAge = now - frame.timestamp

    // Drop stale frames
    if (frameAge > this.config.maxFrameAgeMs) {
      console.log(`[VisionRecorder] Dropping stale frame (${frameAge}ms old)`)
      return false
    }

    // Check frame rate
    const timestamps = this.frameTimestamps.get(frame.sessionId) || []
    const recentTimestamps = timestamps.filter((t) => now - t < 1000)

    if (recentTimestamps.length >= this.config.targetFps) {
      // At target FPS, drop frame
      return false
    }

    // Add to DVR buffer (ring buffer) - always, regardless of problem recording
    const dvrBuffer = this.dvrBuffers.get(frame.sessionId) || []
    dvrBuffer.push(frame)

    // Calculate max DVR buffer size based on FPS
    const maxDvrFrames = this.config.dvrBufferSeconds * this.config.targetFps
    while (dvrBuffer.length > maxDvrFrames) {
      dvrBuffer.shift()
    }
    this.dvrBuffers.set(frame.sessionId, dvrBuffer)

    // Save frame to disk (only if we have a current problem)
    const problem = session.currentProblem
    if (!problem) {
      // No current problem - frame goes to DVR buffer only, not saved to disk
      recentTimestamps.push(now)
      this.frameTimestamps.set(frame.sessionId, recentTimestamps)
      return true
    }

    // Save frame to problem's frames directory
    const frameNumber = problem.frameCount.toString().padStart(6, '0')
    const framePath = path.join(problem.framesDir, `frame_${frameNumber}.jpg`)

    try {
      // Remove data URL prefix if present
      const base64Data = frame.imageData.replace(/^data:image\/\w+;base64,/, '')
      const buffer = Buffer.from(base64Data, 'base64')
      await writeFile(framePath, buffer)

      // Calculate timestamp relative to problem start (for metadata)
      const tFromProblemStart = frame.timestamp - problem.startedAt.getTime()

      // Create metadata entry combining frame data with latest practice state
      const metadataEntry: ProblemMetadataEntry = {
        t: tFromProblemStart,
        detectedValue: frame.detectedValue,
        confidence: frame.confidence,
        studentAnswer: session.latestPracticeState.studentAnswer,
        phase: session.latestPracticeState.phase,
        ...(session.latestPracticeState.phase === 'feedback' &&
        session.latestPracticeState.isCorrect !== null
          ? { isCorrect: session.latestPracticeState.isCorrect }
          : {}),
      }
      problem.metadata.push(metadataEntry)

      problem.frameCount++
      problem.lastFrameAt = new Date(frame.timestamp)

      // Update FPS tracking
      recentTimestamps.push(now)
      this.frameTimestamps.set(frame.sessionId, recentTimestamps)

      return true
    } catch (error) {
      console.error(`[VisionRecorder] Error saving frame:`, error)
      return false
    }
  }

  /**
   * Stop recording session.
   * Finalizes and encodes the current problem (if any).
   */
  async stopSession(sessionId: string): Promise<void> {
    const session = this.activeSessions.get(sessionId)
    if (!session) {
      console.log(`[VisionRecorder] No active session for ${sessionId}`)
      return
    }

    // Finalize the current problem (the last one)
    if (session.currentProblem) {
      console.log(
        `[VisionRecorder] Session ending, finalizing problem ${session.currentProblem.problemNumber}`
      )
      await this.finalizeProblem(session)
    }

    // Clean up in-memory state
    this.activeSessions.delete(sessionId)
    this.frameTimestamps.delete(sessionId)
    this.dvrBuffers.delete(sessionId)
    this.currentProblemInfo.delete(sessionId)

    console.log(`[VisionRecorder] Stopped session ${sessionId}`)
  }

  /**
   * Encode a problem's frames into MP4 video.
   */
  private async encodeProblemVideo(
    videoId: string,
    sessionId: string,
    problemNumber: number
  ): Promise<void> {
    // Get video record from database
    const video = await db.query.visionProblemVideos.findFirst({
      where: eq(visionProblemVideos.id, videoId),
    })

    if (!video) {
      console.error(`[VisionRecorder] Video ${videoId} not found`)
      return
    }

    // Derive framesDir from filename (e.g., problem_001_e0_a1.mp4 -> problem_001_e0_a1.frames)
    const baseName = video.filename.replace('.mp4', '')
    const framesDir = path.join(
      this.config.uploadDir,
      video.playerId,
      video.sessionId,
      `${baseName}.frames`
    )
    const outputPath = path.join(
      this.config.uploadDir,
      video.playerId,
      video.sessionId,
      video.filename
    )

    try {
      // Check if ffmpeg is available
      const ffmpegAvailable = await VideoEncoder.isAvailable()
      if (!ffmpegAvailable) {
        console.error('[VisionRecorder] ffmpeg not available, cannot encode video')
        // Mark as failed - ffmpeg is required for video encoding
        await db
          .update(visionProblemVideos)
          .set({
            status: 'failed',
            processingError: 'ffmpeg not available on server',
          })
          .where(eq(visionProblemVideos.id, videoId))

        // Notify observers of failure
        if (this.onVideoFailed) {
          this.onVideoFailed({
            sessionId,
            problemNumber,
            error: 'ffmpeg not available on server',
          })
        }
        return
      }

      // Encode frames to MP4
      const result = await VideoEncoder.encode({
        framesDir,
        outputPath,
        fps: this.config.targetFps,
        quality: 23, // Good balance of quality and size
        preset: 'fast', // Faster encoding, slightly larger file
      })

      if (!result.success) {
        throw new Error(result.error || 'Encoding failed')
      }

      // Update database with encoding results
      await db
        .update(visionProblemVideos)
        .set({
          status: 'ready',
          fileSize: result.fileSize,
        })
        .where(eq(visionProblemVideos.id, videoId))

      console.log(
        `[VisionRecorder] Encoded problem ${problemNumber}: ${(result.fileSize! / 1024).toFixed(1)} KB`
      )

      // Notify observers that video is ready
      if (this.onVideoReady) {
        this.onVideoReady({
          sessionId,
          problemNumber,
          durationMs: video.durationMs || 0,
          videoUrl: `/api/curriculum/${video.playerId}/sessions/${video.sessionId}/problems/${problemNumber}/video`,
        })
      }

      // Clean up frame files after successful encoding
      await VideoEncoder.cleanupFrames(framesDir)
    } catch (error) {
      const errorMessage = error instanceof Error ? error.message : 'Unknown error'
      console.error(`[VisionRecorder] Encoding failed for problem ${problemNumber}:`, errorMessage)

      await db
        .update(visionProblemVideos)
        .set({
          status: 'failed',
          processingError: errorMessage,
        })
        .where(eq(visionProblemVideos.id, videoId))

      // Notify observers of failure
      if (this.onVideoFailed) {
        this.onVideoFailed({
          sessionId,
          problemNumber,
          error: errorMessage,
        })
      }
    }
  }

  /**
   * Get a frame from the DVR buffer for scrub-back.
   * @param sessionId The session to get the frame from
   * @param offsetMs Offset from session start
   */
  getDvrFrame(sessionId: string, offsetMs: number): VisionFrame | null {
    const dvrBuffer = this.dvrBuffers.get(sessionId)
    const session = this.activeSessions.get(sessionId)

    if (!dvrBuffer || !session || dvrBuffer.length === 0) {
      return null
    }

    // Calculate target timestamp
    const targetTimestamp = session.sessionStartedAt.getTime() + offsetMs

    // Find closest frame
    let closestFrame = dvrBuffer[0]
    let closestDiff = Math.abs(closestFrame.timestamp - targetTimestamp)

    for (const frame of dvrBuffer) {
      const diff = Math.abs(frame.timestamp - targetTimestamp)
      if (diff < closestDiff) {
        closestFrame = frame
        closestDiff = diff
      }
    }

    return closestFrame
  }

  /**
   * Get DVR buffer info for a session, including current problem boundaries
   */
  getDvrBufferInfo(sessionId: string): {
    availableFromMs: number
    availableToMs: number
    currentProblemStartMs: number | null
    currentProblemNumber: number | null
  } | null {
    const dvrBuffer = this.dvrBuffers.get(sessionId)
    const session = this.activeSessions.get(sessionId)

    if (!dvrBuffer || !session || dvrBuffer.length === 0) {
      return null
    }

    const startOffset = dvrBuffer[0].timestamp - session.sessionStartedAt.getTime()
    const endOffset = dvrBuffer[dvrBuffer.length - 1].timestamp - session.sessionStartedAt.getTime()
    const problemInfo = this.currentProblemInfo.get(sessionId)

    return {
      availableFromMs: startOffset,
      availableToMs: endOffset,
      currentProblemStartMs: problemInfo?.startMs ?? null,
      currentProblemNumber: problemInfo?.problemNumber ?? null,
    }
  }

  /**
   * Check if a session is currently being recorded
   */
  isRecording(sessionId: string): boolean {
    return this.activeSessions.has(sessionId)
  }

  /**
   * Get list of available problem videos for a session
   */
  async getSessionVideos(
    sessionId: string
  ): Promise<Array<{ problemNumber: number; status: string; durationMs: number | null }>> {
    const videos = await db.query.visionProblemVideos.findMany({
      where: eq(visionProblemVideos.sessionId, sessionId),
      orderBy: (table, { asc }) => [asc(table.problemNumber)],
    })

    return videos.map((v) => ({
      problemNumber: v.problemNumber,
      status: v.status,
      durationMs: v.durationMs,
    }))
  }

  /**
   * Clean up expired recordings.
   * Should be called periodically (e.g., by a cron job).
   */
  async cleanupExpiredRecordings(): Promise<number> {
    const now = new Date()

    // Find expired videos
    const expired = await db.query.visionProblemVideos.findMany({
      where: (table, { lt }) => lt(table.expiresAt, now),
    })

    let deletedCount = 0

    // Group by session to clean up directories efficiently
    const sessionGroups = new Map<string, typeof expired>()
    for (const video of expired) {
      const key = `${video.playerId}/${video.sessionId}`
      if (!sessionGroups.has(key)) {
        sessionGroups.set(key, [])
      }
      sessionGroups.get(key)!.push(video)
    }

    for (const [, videos] of sessionGroups) {
      for (const video of videos) {
        try {
          // Delete video file, frames directory, and metadata file
          const videoPath = path.join(
            this.config.uploadDir,
            video.playerId,
            video.sessionId,
            video.filename
          )
          // Derive framesDir and metadataPath from filename
          const baseName = video.filename.replace('.mp4', '')
          const framesDir = path.join(
            this.config.uploadDir,
            video.playerId,
            video.sessionId,
            `${baseName}.frames`
          )
          const metadataPath = path.join(
            this.config.uploadDir,
            video.playerId,
            video.sessionId,
            `${baseName}.meta.json`
          )

          await rm(videoPath, { force: true })
          await rm(framesDir, { recursive: true, force: true })
          await rm(metadataPath, { force: true })

          // Delete database record
          await db.delete(visionProblemVideos).where(eq(visionProblemVideos.id, video.id))

          deletedCount++
          console.log(
            `[VisionRecorder] Deleted expired video ${video.id} (problem ${video.problemNumber})`
          )
        } catch (error) {
          console.error(`[VisionRecorder] Failed to delete video ${video.id}:`, error)
        }
      }

      // Try to clean up session directory if empty
      try {
        const sessionDir = path.join(this.config.uploadDir, videos[0].playerId, videos[0].sessionId)
        await rm(sessionDir, { recursive: true, force: true })
      } catch {
        // Directory might not be empty or might not exist, that's ok
      }
    }

    if (deletedCount > 0) {
      console.log(`[VisionRecorder] Cleaned up ${deletedCount} expired video recordings`)
    }

    return deletedCount
  }
}