All files / web/src/app/vision-training/train/components/wizard/cards ModelCard.tsx

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

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

import { css } from '../../../../../../../styled-system/css'
import type { ModelType, ModelsSummary, DataQuality } from '../types'

interface ModelCardProps {
  modelsSummary: ModelsSummary | null
  summaryLoading: boolean
  selectedModel: ModelType | null
  onSelectModel: (model: ModelType) => void
  onProgress: () => void
}

const QUALITY_CONFIG: Record<DataQuality, { color: string; label: string }> = {
  none: { color: 'gray.500', label: 'No Data' },
  insufficient: { color: 'red.400', label: 'Insufficient' },
  minimal: { color: 'yellow.400', label: 'Minimal' },
  good: { color: 'green.400', label: 'Good' },
  excellent: { color: 'green.300', label: 'Excellent' },
}

interface ModelOptionProps {
  icon: string
  title: string
  description: string
  sampleCount: number
  sampleLabel: string
  dataQuality: DataQuality
  hasData: boolean
  isSelected: boolean
  onClick: () => void
}

function ModelOption({
  icon,
  title,
  description,
  sampleCount,
  sampleLabel,
  dataQuality,
  hasData,
  isSelected,
  onClick,
}: ModelOptionProps) {
  const qualityConfig = QUALITY_CONFIG[dataQuality]

  return (
    <button
      type="button"
      onClick={onClick}
      data-element="model-option"
      data-model={title.toLowerCase().replace(' ', '-')}
      data-selected={isSelected}
      className={css({
        flex: 1,
        p: 4,
        bg: isSelected ? 'blue.900/50' : 'gray.800',
        border: '2px solid',
        borderColor: isSelected ? 'blue.500' : 'gray.700',
        borderRadius: 'xl',
        cursor: 'pointer',
        textAlign: 'left',
        transition: 'all 0.2s',
        _hover: {
          borderColor: isSelected ? 'blue.400' : 'gray.600',
          bg: isSelected ? 'blue.900/60' : 'gray.750',
        },
      })}
    >
      {/* Icon and title */}
      <div
        className={css({
          display: 'flex',
          alignItems: 'center',
          gap: 2,
          mb: 2,
        })}
      >
        <span className={css({ fontSize: 'xl' })}>{icon}</span>
        <span
          className={css({
            fontWeight: 'semibold',
            color: isSelected ? 'blue.300' : 'gray.200',
          })}
        >
          {title}
        </span>
      </div>

      {/* Description */}
      <div
        className={css({
          fontSize: 'sm',
          color: 'gray.400',
          mb: 3,
          lineHeight: 1.4,
        })}
      >
        {description}
      </div>

      {/* Sample count */}
      <div
        className={css({
          fontSize: 'lg',
          fontWeight: 'bold',
          color: 'gray.200',
          mb: 1,
        })}
      >
        {sampleCount.toLocaleString()} {sampleLabel}
      </div>

      {/* Quality indicator */}
      <div
        className={css({
          display: 'flex',
          alignItems: 'center',
          gap: 1.5,
          fontSize: 'sm',
        })}
      >
        {hasData ? (
          <>
            <span
              className={css({
                width: '8px',
                height: '8px',
                borderRadius: 'full',
                bg: qualityConfig.color,
              })}
            />
            <span
              style={{
                color: `var(--colors-${qualityConfig.color.replace('.', '-')})`,
              }}
            >
              {qualityConfig.label}
            </span>
          </>
        ) : (
          <span className={css({ color: 'gray.500' })}>No data collected</span>
        )}
      </div>
    </button>
  )
}

export function ModelCard({
  modelsSummary,
  summaryLoading,
  selectedModel,
  onSelectModel,
  onProgress,
}: ModelCardProps) {
  if (summaryLoading) {
    return (
      <div className={css({ textAlign: 'center', py: 6 })}>
        <span
          className={css({
            fontSize: '2xl',
            animation: 'spin 1s linear infinite',
          })}
        >

        </span>
        <div className={css({ color: 'gray.400', mt: 2 })}>Loading model info...</div>
      </div>
    )
  }

  const handleSelectAndProgress = (model: ModelType) => {
    onSelectModel(model)
    // Auto-progress after selection
    setTimeout(() => {
      onProgress()
    }, 300)
  }

  return (
    <div data-element="model-card-content">
      {/* Description */}
      <div
        className={css({
          fontSize: 'sm',
          color: 'gray.400',
          mb: 4,
          textAlign: 'center',
        })}
      >
        Select which vision model you want to train
      </div>

      {/* Model options */}
      <div
        className={css({
          display: 'flex',
          gap: 3,
          flexDirection: { base: 'column', sm: 'row' },
        })}
      >
        <ModelOption
          icon="📊"
          title="Column Classifier"
          description="Recognizes digit values (0-9) from abacus column images"
          sampleCount={modelsSummary?.columnClassifier.totalImages ?? 0}
          sampleLabel="images"
          dataQuality={modelsSummary?.columnClassifier.dataQuality ?? 'none'}
          hasData={modelsSummary?.columnClassifier.hasData ?? false}
          isSelected={selectedModel === 'column-classifier'}
          onClick={() => handleSelectAndProgress('column-classifier')}
        />

        <ModelOption
          icon="🖼️"
          title="Boundary Detector"
          description="Detects abacus corners for marker-free calibration"
          sampleCount={modelsSummary?.boundaryDetector.totalFrames ?? 0}
          sampleLabel="frames"
          dataQuality={modelsSummary?.boundaryDetector.dataQuality ?? 'none'}
          hasData={modelsSummary?.boundaryDetector.hasData ?? false}
          isSelected={selectedModel === 'boundary-detector'}
          onClick={() => handleSelectAndProgress('boundary-detector')}
        />
      </div>

      {/* Current selection info */}
      {selectedModel && (
        <div
          className={css({
            mt: 4,
            p: 3,
            bg: 'blue.900/30',
            border: '1px solid',
            borderColor: 'blue.700/50',
            borderRadius: 'lg',
            fontSize: 'sm',
            color: 'blue.300',
            textAlign: 'center',
          })}
        >
          {selectedModel === 'column-classifier' ? (
            <>
              <strong>Column Classifier</strong> selected — trains on cropped column images to
              recognize digits
            </>
          ) : (
            <>
              <strong>Boundary Detector</strong> selected — trains on full frames to detect abacus
              corners
            </>
          )}
        </div>
      )}
    </div>
  )
}