All files / web/src/components/practice ProblemDebugPanel.tsx

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

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

import { useCallback, useState } from 'react'
import { useVisualDebugSafe } from '@/contexts/VisualDebugContext'
import type { GeneratedProblem, ProblemSlot, SessionPart } from '@/db/schema/session-plans'
import { css } from '../../../styled-system/css'

interface ProblemDebugPanelProps {
  /** The current problem being displayed */
  problem: GeneratedProblem
  /** The current slot */
  slot: ProblemSlot
  /** The current part */
  part: SessionPart
  /** Current part index */
  partIndex: number
  /** Current slot index */
  slotIndex: number
  /** Current user input */
  userInput: string
  /** Current phase name */
  phaseName: string
}

/**
 * Debug panel that shows current problem details when visual debug mode is on.
 * Allows easy copying of problem data for bug reports.
 */
export function ProblemDebugPanel({
  problem,
  slot,
  part,
  partIndex,
  slotIndex,
  userInput,
  phaseName,
}: ProblemDebugPanelProps) {
  const { isVisualDebugEnabled } = useVisualDebugSafe()
  const [copied, setCopied] = useState(false)
  const [isCollapsed, setIsCollapsed] = useState(false)

  // Get trace data directly from the problem generator
  const trace = problem.generationTrace
  const maxBudget = trace?.budgetConstraint ?? slot.constraints?.maxComplexityBudgetPerTerm
  const minBudget = trace?.minBudgetConstraint ?? slot.constraints?.minComplexityBudgetPerTerm
  const totalCost = trace?.totalComplexityCost

  const debugData = {
    problem: {
      terms: problem.terms,
      answer: problem.answer,
      skillsRequired: problem.skillsRequired,
    },
    slot: {
      index: slot.index,
      purpose: slot.purpose,
      constraints: slot.constraints,
    },
    part: {
      number: part.partNumber,
      type: part.type,
    },
    position: {
      partIndex,
      slotIndex,
    },
    state: {
      userInput,
      phaseName,
    },
    complexity: {
      maxBudget: maxBudget ?? 'none',
      minBudget: minBudget ?? 'none',
      totalCost: totalCost ?? 'unknown',
      termAnalysis:
        trace?.steps.map((step, i) => ({
          term: step.termAdded,
          skills: step.skillsUsed,
          cost: step.complexityCost,
          // First term (i=0) is exempt from min budget when starting from 0
          exemptFromMin: i === 0,
        })) ?? [],
      hasTrace: !!trace,
    },
  }

  const debugJson = JSON.stringify(debugData, null, 2)

  const handleCopy = useCallback(async () => {
    try {
      await navigator.clipboard.writeText(debugJson)
      setCopied(true)
      setTimeout(() => setCopied(false), 2000)
    } catch {
      // Fallback for older browsers
      const textarea = document.createElement('textarea')
      textarea.value = debugJson
      document.body.appendChild(textarea)
      textarea.select()
      document.execCommand('copy')
      document.body.removeChild(textarea)
      setCopied(true)
      setTimeout(() => setCopied(false), 2000)
    }
  }, [debugJson])

  if (!isVisualDebugEnabled) {
    return null
  }

  return (
    <div
      data-component="problem-debug-panel"
      className={css({
        position: 'fixed',
        bottom: '10px',
        right: '10px',
        width: isCollapsed ? 'auto' : '360px',
        maxHeight: isCollapsed ? 'auto' : '400px',
        backgroundColor: 'rgba(0, 0, 0, 0.9)',
        color: 'white',
        borderRadius: '8px',
        boxShadow: '0 4px 20px rgba(0, 0, 0, 0.5)',
        zIndex: 10000,
        fontFamily: 'monospace',
        fontSize: '11px',
        overflow: 'hidden',
        border: '1px solid rgba(255, 255, 255, 0.2)',
      })}
    >
      {/* Header */}
      <div
        className={css({
          display: 'flex',
          justifyContent: 'space-between',
          alignItems: 'center',
          padding: '8px 12px',
          backgroundColor: 'rgba(255, 255, 255, 0.1)',
          borderBottom: isCollapsed ? 'none' : '1px solid rgba(255, 255, 255, 0.1)',
          cursor: 'pointer',
        })}
        onClick={() => setIsCollapsed(!isCollapsed)}
      >
        <span className={css({ fontWeight: 'bold', color: '#4ade80' })}>
          Problem Debug {isCollapsed ? '(expand)' : ''}
        </span>
        <div className={css({ display: 'flex', gap: '8px' })}>
          {!isCollapsed && (
            <button
              type="button"
              onClick={(e) => {
                e.stopPropagation()
                handleCopy()
              }}
              className={css({
                padding: '4px 8px',
                backgroundColor: copied ? '#22c55e' : '#3b82f6',
                color: 'white',
                border: 'none',
                borderRadius: '4px',
                cursor: 'pointer',
                fontSize: '10px',
                fontWeight: 'bold',
                transition: 'background-color 0.2s',
                _hover: {
                  backgroundColor: copied ? '#16a34a' : '#2563eb',
                },
              })}
            >
              {copied ? 'Copied!' : 'Copy JSON'}
            </button>
          )}
          <span className={css({ color: 'gray.400' })}>{isCollapsed ? '▲' : '▼'}</span>
        </div>
      </div>

      {/* Content */}
      {!isCollapsed && (
        <div
          className={css({
            padding: '12px',
            overflowY: 'auto',
            maxHeight: '340px',
          })}
        >
          {/* Quick summary */}
          <div
            className={css({
              marginBottom: '12px',
              padding: '8px',
              backgroundColor: 'rgba(255, 255, 255, 0.05)',
              borderRadius: '4px',
            })}
          >
            <div className={css({ color: '#fbbf24', marginBottom: '4px' })}>
              Part {part.partNumber} ({part.type}) - Slot {slotIndex + 1}
            </div>
            <div className={css({ color: '#f472b6' })}>
              {problem.terms
                .map((t, i) => (i === 0 ? t : t >= 0 ? `+ ${t}` : `- ${Math.abs(t)}`))
                .join(' ')}{' '}
              = {problem.answer}
            </div>
            <div className={css({ color: '#67e8f9', marginTop: '4px' })}>
              Skills: {problem.skillsRequired.join(', ')}
            </div>
            <div className={css({ color: '#a3a3a3', marginTop: '4px' })}>
              Phase: {phaseName} | Input: "{userInput}"
            </div>
          </div>

          {/* Complexity Budget Info */}
          <div
            data-element="complexity-breakdown"
            className={css({
              marginBottom: '12px',
              padding: '8px',
              backgroundColor: 'rgba(139, 92, 246, 0.1)',
              borderRadius: '4px',
              border: '1px solid rgba(139, 92, 246, 0.3)',
            })}
          >
            <div
              className={css({
                color: '#a78bfa',
                fontWeight: 'bold',
                marginBottom: '6px',
              })}
            >
              <div
                className={css({
                  display: 'flex',
                  justifyContent: 'space-between',
                })}
              >
                <span>Complexity Budget</span>
                {totalCost !== undefined && (
                  <span className={css({ color: '#4ade80' })}>total: {totalCost}</span>
                )}
              </div>
              <div
                className={css({
                  display: 'flex',
                  gap: '12px',
                  fontSize: '10px',
                  marginTop: '4px',
                  fontWeight: 'normal',
                })}
              >
                {minBudget !== undefined ? (
                  <span className={css({ color: '#f472b6' })}>
                    min {minBudget}/term{' '}
                    <span className={css({ color: '#6b7280' })}>(1st exempt)</span>
                  </span>
                ) : (
                  <span className={css({ color: '#6b7280' })}>no min</span>
                )}
                {maxBudget !== undefined ? (
                  <span className={css({ color: '#fbbf24' })}>max {maxBudget}/term</span>
                ) : (
                  <span className={css({ color: '#6b7280' })}>no max</span>
                )}
              </div>
            </div>
            {trace ? (
              <div
                className={css({
                  display: 'flex',
                  flexDirection: 'column',
                  gap: '4px',
                })}
              >
                {trace.steps.map((step, i) => {
                  const cost = step.complexityCost
                  const isFirstTerm = i === 0
                  const isOverBudget =
                    maxBudget !== undefined && cost !== undefined && cost > maxBudget
                  const isUnderBudget =
                    minBudget !== undefined &&
                    cost !== undefined &&
                    cost < minBudget &&
                    !isFirstTerm
                  // First term is exempt from min budget (basic skills have cost 0)
                  const isExemptFromMin = isFirstTerm && minBudget !== undefined

                  // Color: red if over, yellow if under (and not exempt), green otherwise
                  const costColor = isOverBudget ? '#f87171' : isUnderBudget ? '#fbbf24' : '#4ade80'

                  return (
                    <div
                      key={i}
                      className={css({
                        display: 'flex',
                        alignItems: 'center',
                        gap: '6px',
                        fontSize: '10px',
                      })}
                    >
                      <span
                        className={css({
                          color: '#d1d5db',
                          minWidth: '45px',
                        })}
                      >
                        {step.termAdded >= 0
                          ? `+ ${step.termAdded}`
                          : `- ${Math.abs(step.termAdded)}`}
                      </span>
                      <span
                        className={css({
                          color: costColor,
                          fontWeight: 'bold',
                          minWidth: '35px',
                        })}
                        title={isExemptFromMin ? 'First term exempt from min budget' : undefined}
                      >
                        {cost !== undefined ? `${cost}` : '—'}
                        {isExemptFromMin && cost !== undefined && cost < minBudget && (
                          <span
                            className={css({
                              color: '#6b7280',
                              fontSize: '8px',
                            })}
                          >
                            *
                          </span>
                        )}
                      </span>
                      <span
                        className={css({
                          color: '#9ca3af',
                          overflow: 'hidden',
                          textOverflow: 'ellipsis',
                          whiteSpace: 'nowrap',
                          flex: 1,
                        })}
                        title={step.skillsUsed.join(', ')}
                      >
                        {step.skillsUsed.length > 0 ? step.skillsUsed.join(', ') : '(none)'}
                      </span>
                    </div>
                  )
                })}
              </div>
            ) : (
              <div className={css({ color: '#f87171', fontSize: '10px' })}>
                No generation trace available
              </div>
            )}
          </div>

          {/* Full JSON */}
          <pre
            className={css({
              whiteSpace: 'pre-wrap',
              wordBreak: 'break-all',
              color: '#a3e635',
              margin: 0,
              lineHeight: '1.4',
            })}
          >
            {debugJson}
          </pre>
        </div>
      )}
    </div>
  )
}