All files / web/src/hooks usePhotoManagement.ts

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

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
'use client'

import { useCallback, useEffect, useRef, useState } from 'react'
import { useQueryClient } from '@tanstack/react-query'
import { useDocumentDetection } from '@/components/practice/useDocumentDetection'
import type { DocumentAdjustmentState, Corner, Rotation } from '@/types/attachments'
import { attachmentKeys } from '@/lib/queryKeys'

/**
 * Options for usePhotoManagement hook
 */
export interface UsePhotoManagementOptions {
  studentId: string
  sessionId: string | undefined
  onError?: (message: string) => void
}

/**
 * Return type for usePhotoManagement hook
 */
export interface UsePhotoManagementReturn {
  // Camera state
  showCamera: boolean
  openCamera: () => void
  closeCamera: () => void
  handleCameraCapture: (
    cropped: File,
    original: File,
    corners: Corner[],
    rotation: Rotation
  ) => void

  // Drag-drop state
  dragOver: boolean
  handleDrop: (e: React.DragEvent) => void
  handleDragOver: (e: React.DragEvent) => void
  handleDragLeave: () => void

  // File input
  fileInputRef: React.RefObject<HTMLInputElement>
  handleFileSelect: (e: React.ChangeEvent<HTMLInputElement>) => void

  // Upload state
  isUploading: boolean
  uploadError: string | null

  // Delete state
  deletingId: string | null
  deletePhoto: (attachmentId: string) => Promise<void>

  // Document adjustment modal state
  adjustmentState: DocumentAdjustmentState | null
  handleAdjustmentConfirm: (cropped: File, corners: Corner[], rotation: Rotation) => Promise<void>
  handleAdjustmentSkip: () => Promise<void>
  handleAdjustmentCancel: () => void
  queueLength: number

  // OpenCV reference (needed by DocumentAdjuster)
  opencvRef: unknown
  detectQuadsInImage: (
    canvas: HTMLCanvasElement
  ) => import('@/components/practice/useDocumentDetection').DetectQuadsInImageResult

  // Photo editing
  handlePhotoEditConfirm: (
    photoId: string,
    croppedFile: File,
    corners: Corner[],
    rotation: Rotation
  ) => Promise<void>
}

/**
 * Hook for managing photo uploads, camera capture, and document adjustment
 *
 * Consolidates all photo-related state and callbacks from SummaryClient:
 * - Camera modal state
 * - Drag-and-drop file handling
 * - File input selection
 * - Upload progress and errors
 * - Photo deletion
 * - Document adjustment modal (OpenCV integration)
 */
export function usePhotoManagement({
  studentId,
  sessionId,
  onError,
}: UsePhotoManagementOptions): UsePhotoManagementReturn {
  const queryClient = useQueryClient()

  // Camera state
  const [showCamera, setShowCamera] = useState(false)

  // Drag-drop state
  const [dragOver, setDragOver] = useState(false)

  // Upload state
  const [isUploading, setIsUploading] = useState(false)
  const [uploadError, setUploadError] = useState<string | null>(null)

  // Delete state
  const [deletingId, setDeletingId] = useState<string | null>(null)

  // File input ref (non-null type to match OfflineWorkSection expectations)
  const fileInputRef = useRef<HTMLInputElement>(null)

  // File queue for document adjustment
  const [fileQueue, setFileQueue] = useState<File[]>([])
  const [adjustmentState, setAdjustmentState] = useState<DocumentAdjustmentState | null>(null)

  // Document detection hook (lazy loads OpenCV)
  const {
    ensureOpenCVLoaded,
    detectQuadsInImage,
    loadImageToCanvas,
    cv: opencvRef,
  } = useDocumentDetection()

  // Report errors through callback or state
  const reportError = useCallback(
    (message: string) => {
      setUploadError(message)
      onError?.(message)
    },
    [onError]
  )

  // Upload photos with optional original preservation and corners
  const uploadPhotos = useCallback(
    async (
      photos: File[],
      originals?: File[],
      cornersData?: Array<Corner[] | null>,
      rotationData?: Rotation[]
    ) => {
      if (!sessionId || photos.length === 0) return

      setIsUploading(true)
      setUploadError(null)

      try {
        const formData = new FormData()
        for (const file of photos) {
          formData.append('photos', file)
        }
        if (originals) {
          for (const file of originals) {
            formData.append('originals', file)
          }
        }
        if (cornersData) {
          for (const corners of cornersData) {
            formData.append('corners', corners ? JSON.stringify(corners) : '')
          }
        }
        if (rotationData) {
          for (const rotation of rotationData) {
            formData.append('rotation', rotation.toString())
          }
        }

        const response = await fetch(
          `/api/curriculum/${studentId}/sessions/${sessionId}/attachments`,
          { method: 'POST', body: formData }
        )

        if (!response.ok) {
          const data = await response.json()
          throw new Error(data.error || 'Failed to upload photos')
        }

        // Refresh attachments
        queryClient.invalidateQueries({
          queryKey: attachmentKeys.session(studentId, sessionId),
        })
      } catch (err) {
        reportError(err instanceof Error ? err.message : 'Failed to upload photos')
      } finally {
        setIsUploading(false)
      }
    },
    [studentId, sessionId, queryClient, reportError]
  )

  // Process next file in queue - load, detect quads, show adjuster
  const processNextFile = useCallback(async () => {
    if (fileQueue.length === 0) {
      setAdjustmentState(null)
      return
    }

    const nextFile = fileQueue[0]

    // Load file to canvas (doesn't require OpenCV)
    const canvas = await loadImageToCanvas(nextFile)
    if (!canvas) {
      console.warn('Failed to load image:', nextFile.name)
      // Skip this file and process next
      setFileQueue((prev) => prev.slice(1))
      return
    }

    // Ensure OpenCV is loaded before detecting quads (lazy load)
    await ensureOpenCVLoaded()

    // Detect quads (or get fallback corners if OpenCV failed to load)
    const result = detectQuadsInImage(canvas)

    // Show adjustment UI
    setAdjustmentState({
      originalFile: nextFile,
      sourceCanvas: result.sourceCanvas,
      corners: result.corners,
    })
  }, [fileQueue, loadImageToCanvas, ensureOpenCVLoaded, detectQuadsInImage])

  // Start processing queue when files are added
  useEffect(() => {
    if (fileQueue.length > 0 && !adjustmentState) {
      processNextFile()
    }
  }, [fileQueue, adjustmentState, processNextFile])

  // Camera handlers
  const openCamera = useCallback(() => setShowCamera(true), [])
  const closeCamera = useCallback(() => setShowCamera(false), [])

  const handleCameraCapture = useCallback(
    (cropped: File, original: File, corners: Corner[], rotation: Rotation) => {
      setShowCamera(false)
      uploadPhotos([cropped], [original], [corners], [rotation])
    },
    [uploadPhotos]
  )

  // Drag-drop handlers
  const handleDrop = useCallback((e: React.DragEvent) => {
    e.preventDefault()
    setDragOver(false)
    const files = Array.from(e.dataTransfer.files)
    const imageFiles = files.filter((f) => f.type.startsWith('image/'))
    if (imageFiles.length > 0) {
      setFileQueue((prev) => [...prev, ...imageFiles])
    }
  }, [])

  const handleDragOver = useCallback((e: React.DragEvent) => {
    e.preventDefault()
    setDragOver(true)
  }, [])

  const handleDragLeave = useCallback(() => {
    setDragOver(false)
  }, [])

  // File input handler
  const handleFileSelect = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
    const files = e.target.files ? Array.from(e.target.files) : []
    const imageFiles = files.filter((f) => f.type.startsWith('image/'))
    if (imageFiles.length > 0) {
      setFileQueue((prev) => [...prev, ...imageFiles])
    }
    e.target.value = ''
  }, [])

  // Delete photo
  const deletePhoto = useCallback(
    async (attachmentId: string) => {
      if (!sessionId) return

      setDeletingId(attachmentId)
      try {
        const response = await fetch(`/api/curriculum/${studentId}/attachments/${attachmentId}`, {
          method: 'DELETE',
        })

        if (!response.ok) {
          const data = await response.json()
          throw new Error(data.error || 'Failed to delete photo')
        }

        // Refresh attachments
        queryClient.invalidateQueries({
          queryKey: attachmentKeys.session(studentId, sessionId),
        })
      } catch (err) {
        reportError(err instanceof Error ? err.message : 'Failed to delete photo')
      } finally {
        setDeletingId(null)
      }
    },
    [studentId, sessionId, queryClient, reportError]
  )

  // Document adjustment handlers
  const handleAdjustmentConfirm = useCallback(
    async (cropped: File, corners: Corner[], rotation: Rotation) => {
      if (!adjustmentState) return

      // Upload both cropped and original, with corners and rotation for later re-editing
      await uploadPhotos([cropped], [adjustmentState.originalFile], [corners], [rotation])

      // Remove from queue and process next
      setFileQueue((prev) => prev.slice(1))
      setAdjustmentState(null)
    },
    [adjustmentState, uploadPhotos]
  )

  const handleAdjustmentSkip = useCallback(async () => {
    if (!adjustmentState) return

    // Upload original only (no crop)
    await uploadPhotos([adjustmentState.originalFile])

    // Remove from queue and process next
    setFileQueue((prev) => prev.slice(1))
    setAdjustmentState(null)
  }, [adjustmentState, uploadPhotos])

  const handleAdjustmentCancel = useCallback(() => {
    setFileQueue([])
    setAdjustmentState(null)
  }, [])

  // Photo edit confirm (from PhotoViewerEditor)
  const handlePhotoEditConfirm = useCallback(
    async (photoId: string, croppedFile: File, corners: Corner[], rotation: Rotation) => {
      try {
        setIsUploading(true)
        const formData = new FormData()
        formData.append('file', croppedFile)
        formData.append('corners', JSON.stringify(corners))
        formData.append('rotation', rotation.toString())

        const response = await fetch(`/api/curriculum/${studentId}/attachments/${photoId}`, {
          method: 'PATCH',
          body: formData,
        })

        if (!response.ok) {
          const data = await response.json()
          throw new Error(data.error || 'Failed to update photo')
        }

        // Refresh attachments
        if (sessionId) {
          queryClient.invalidateQueries({
            queryKey: attachmentKeys.session(studentId, sessionId),
          })
        }
      } catch (err) {
        reportError(err instanceof Error ? err.message : 'Failed to update photo')
      } finally {
        setIsUploading(false)
      }
    },
    [studentId, sessionId, queryClient, reportError]
  )

  return {
    // Camera
    showCamera,
    openCamera,
    closeCamera,
    handleCameraCapture,

    // Drag-drop
    dragOver,
    handleDrop,
    handleDragOver,
    handleDragLeave,

    // File input
    fileInputRef,
    handleFileSelect,

    // Upload
    isUploading,
    uploadError,

    // Delete
    deletingId,
    deletePhoto,

    // Document adjustment
    adjustmentState,
    handleAdjustmentConfirm,
    handleAdjustmentSkip,
    handleAdjustmentCancel,
    queueLength: fileQueue.length,

    // OpenCV
    opencvRef,
    detectQuadsInImage,

    // Photo editing
    handlePhotoEditConfirm,
  }
}

export default usePhotoManagement