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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2eb6221
4d18cf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2eb6221
ebbb1ae
 
 
 
 
4d18cf9
 
 
 
 
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
import { useEffect, useMemo, useRef, useState } from 'react';
import './App.css';

const MATCH_DURATION = 70;
const SUPPORTED_REGION = 'US only';
const API_ROUTING_LABEL = 'NA1 platform + AMERICAS match routing';

// ─── Input sanitization ───────────────────────────────────────────────────────
// Accepted format: one or more uppercase letters/digits, an underscore, then
// one or more digits only.  Examples: NA1_1234567890  EUW1_9876543210
const MATCH_ID_REGEX = /^[A-Z0-9]+_\d+$/;
const MATCH_ID_MAX_LEN = 30;

const sanitizeMatchId = (raw) =>
  raw
    .toUpperCase()
    .replace(/[^A-Z0-9_]/g, '')
    .replace(/_{2,}/g, '_')
    .slice(0, MATCH_ID_MAX_LEN);

// ─── Constants ────────────────────────────────────────────────────────────────
const MODEL_META = [
  { key: 'xgboost', label: 'XGBoost', short: 'Teamfight Pattern', colorClass: 'xgboost' },
  { key: 'lstm', label: 'LSTM', short: 'Momentum Curve', colorClass: 'lstm' },
  { key: 'logreg', label: 'Logistic Regression', short: 'Stability Baseline', colorClass: 'logistic' },
];

const EXAMPLE_MATCH_IDS = ['NA1_5498339609', 'NA1_5498663444', 'NA1_5504289306'];

const EVENT_FILTERS = ['all', 'kills', 'objectives', 'structures'];
const EVENT_TYPE_COLORS = {
  all: '#c6a769',
  kills: '#ff5f7a',
  objectives: '#19d7ff',
  structures: '#f0932b',
};

// ─── Utilities ────────────────────────────────────────────────────────────────
// eslint-disable-next-line no-unused-vars
const clamp = (value, min, max) => Math.min(Math.max(value, min), max);

// ─── Components ───────────────────────────────────────────────────────────────
function AnimatedBackground() {
  return (
    <div className="background-container" aria-hidden="true">
      <div className="bg-grid" />
      <div className="bg-noise" />
      <div className="bg-scanline" />
      <div className="bg-wave" />
      <div className="bg-orb orb-left" />
      <div className="bg-orb orb-right" />
      <div className="bg-vignette" />
    </div>
  );
}

function LandingView({ matchId, onMatchIdChange, onSimulate, onExampleClick, error, isLoading, fetchError }) {
  return (
    <section className="view-shell landing-shell">
      <article className="landing-card rise">
        <p className="eyebrow centered">Post-Match Analyzer</p>
        <h1 className="landing-title centered">Rift Breakdown</h1>
        <p className="landing-subtitle">
          Analyze a finished match and inspect how the win probability evolved through each key moment.
        </p>

        <div className="landing-metrics" role="presentation">
          <span>Region available: {SUPPORTED_REGION}</span>
          <span>{API_ROUTING_LABEL}</span>
          <span>Player-friendly insights</span>
          <span>Timeline breakdown</span>
        </div>

        {/* ── ML Models badge ── */}
        <div className="ml-models-row" role="presentation" aria-label="ML models used">
          <span className="ml-models-label">Powered by</span>
          {MODEL_META.map((model) => (
            <span key={model.key} className={`ml-model-badge ${model.colorClass}`}>
              {model.label}
            </span>
          ))}
        </div>

        <form className="match-form" onSubmit={onSimulate}>
          <label htmlFor="match-id" className="input-label">
            Match ID
          </label>

          <div className={`input-row ${error ? 'has-error' : ''}`}>
            <input
              id="match-id"
              type="text"
              className="match-input"
              placeholder="NA1_1234567890"
              value={matchId}
              onChange={(event) => onMatchIdChange(sanitizeMatchId(event.target.value))}
              autoComplete="off"
              spellCheck="false"
              inputMode="text"
              aria-invalid={error ? 'true' : 'false'}
              aria-describedby="match-help"
              disabled={isLoading}
            />
            <button type="submit" className="primary-btn" disabled={isLoading}>
              {isLoading ? 'Analyzing...' : 'Analyze Match'}
            </button>
          </div>

          <p
            id="match-help"
            className={`helper-text ${error || fetchError ? 'error' : ''}`}
          >
            {error
              ? 'Match ID must follow the format REGION_DIGITS β€” e.g. NA1_1234567890.'
              : fetchError
                ? fetchError
                : 'Use a completed US match ID (NA routing only). Format: NA1_1234567890.'}
          </p>
        </form>

        {/* ── Example match IDs ── */}
        <div className="example-ids-row">
          <span className="example-ids-label">Try an example:</span>
          {EXAMPLE_MATCH_IDS.map((id) => (
            <button
              key={id}
              type="button"
              className={`example-id-chip ${matchId === id ? 'active' : ''}`}
              onClick={() => onExampleClick(id)}
              disabled={isLoading}
            >
              {id}
            </button>
          ))}
        </div>
      </article>
    </section>
  );
}

function ModelCard({ model, value }) {
  const redValue = 100 - value;
  return (
    <article className={`model-card ${model.colorClass}`}>
      <p className="model-kicker">{model.short}</p>
      <h3>{model.label}</h3>
      <div className="model-dual-values">
        <p className="model-value blue">Blue: {value.toFixed(1)}%</p>
        <p className="model-value red">Red: {redValue.toFixed(1)}%</p>
      </div>
      <div className="model-track" aria-hidden="true">
        <div className="model-fill" style={{ width: `${value}%` }} />
      </div>
    </article>
  );
}

function ProbabilityChart({ history, minute, selectedFilter, events }) {
  const chartWidth = 760;
  const chartHeight = 220;
  const maxIndex = history.length - 1;

  const toX = (index) => (index / maxIndex) * chartWidth;
  const toY = (value) => chartHeight - (value / 100) * chartHeight;

  const buildPath = (modelKey) =>
    history
      .map((entry, index) => `${index === 0 ? 'M' : 'L'} ${toX(index)} ${toY(entry[modelKey])}`)
      .join(' ');

  const indicatorX = toX(minute);

  const highlightedEvents = events.filter(
    (event) =>
      (selectedFilter === 'all' || event.type === selectedFilter) &&
      event.minute <= minute
  );

  const maxMark = history.length > 0 ? history[history.length - 1].minute : MATCH_DURATION;
  const axisMarks = [0, Math.round(maxMark / 3), Math.round((maxMark * 2) / 3), maxMark];

  return (
    <section className="chart-wrap">
      <svg
        className="probability-chart"
        viewBox={`0 0 ${chartWidth} ${chartHeight}`}
        preserveAspectRatio="none"
        role="img"
        aria-label="Win probability chart by minute"
      >
        <path d={buildPath('xgboost')} className="chart-line xgboost" />
        <path d={buildPath('lstm')} className="chart-line lstm" />
        <path d={buildPath('logreg')} className="chart-line logistic" />

        {highlightedEvents.map((event, index) => (
          <line
            key={`chart-event-${index}`}
            x1={toX(event.minute)} y1="0"
            x2={toX(event.minute)} y2={chartHeight}
            className={`chart-event-line ${event.type}`}
          />
        ))}

        <line
          x1={indicatorX} y1="0"
          x2={indicatorX} y2={chartHeight}
          className="chart-indicator"
        />
      </svg>

      <div className="chart-axis">
        {axisMarks.map((mark) => (
          <span key={mark}>{mark}m</span>
        ))}
      </div>
    </section>
  );
}

function DashboardView({
  matchId,
  minute,
  maxDuration,
  probabilities,
  history,
  events,
  isPlaying,
  selectedFilter,
  onFilterChange,
  onTogglePlayback,
  onSetMinute,
  onBack,
  blueWin,
}) {
  const feedScrollRef = useRef(null);
  const previousActiveCountRef = useRef(0);

  const visibleEvents = events.filter((event) => selectedFilter === 'all' || event.type === selectedFilter);
  const activeFeedEvents = visibleEvents
    .filter((event) => event.minute <= minute)
    .sort((a, b) => b.minute - a.minute);
  const matrixEvents = [...visibleEvents].sort((a, b) => a.minute - b.minute);

  useEffect(() => {
    if (!feedScrollRef.current) return;
    if (activeFeedEvents.length > previousActiveCountRef.current) {
      feedScrollRef.current.scrollTo({ top: 0, behavior: 'smooth' });
    }
    previousActiveCountRef.current = activeFeedEvents.length;
  }, [activeFeedEvents.length, minute, selectedFilter]);

  return (
    <section className="view-shell dashboard-shell rise">
      <header className="dashboard-header">
        <div className="header-left">
          <button type="button" className="ghost-btn" onClick={onBack}>
            Back to Landing
          </button>
          <p className="target-match">
            Reviewing <strong>{matchId}</strong>
          </p>
        </div>

        {blueWin !== null && (
          <div className={`winner-badge ${blueWin ? 'blue-win' : 'red-win'}`}>
            <span className="winner-label">Match Winner</span>
            <span className="winner-name">{blueWin ? 'Blue Team' : 'Red Team'}</span>
          </div>
        )}
      </header>

      <main className="dashboard-grid">
        <section className="panel primary-panel probability-panel">
          <h2>Match Analysis β€” Blue vs Red Win Probability</h2>
        </section>

        <div className="models-grid models-grid-above">
          {MODEL_META.map((model) => (
            <ModelCard key={model.key} model={model} value={probabilities[model.key]} />
          ))}
        </div>

        <section className="panel timeline-panel">
          <p className="panel-kicker">Timeline & Playback</p>
          <h3>Match Flow</h3>

          <div className="status-row">
            <span className="live-dot" />
            <span>Minute {String(minute).padStart(2, '0')}</span>
          </div>

          <div className="analysis-controls">
            <button type="button" className="ghost-btn playback-btn" onClick={onTogglePlayback}>
              {isPlaying ? 'Pause' : 'Play'} Timeline
            </button>
            <label htmlFor="minute-range" className="slider-label">
              Time Window: {minute}m
            </label>
            <input
              id="minute-range"
              className="minute-slider"
              type="range"
              min="0"
              max={maxDuration}
              value={minute}
              onChange={(event) => onSetMinute(Number(event.target.value))}
            />
          </div>

          <ProbabilityChart
            history={history}
            minute={minute}
            selectedFilter={selectedFilter}
            events={events}
          />

          <div className="timeline-feed-scroll" ref={feedScrollRef}>
            <div className="timeline-list">
              {activeFeedEvents.length === 0 ? (
                <p className="feed-placeholder">
                  No active events yet. Press play or move the slider forward.
                </p>
              ) : (
                activeFeedEvents.map((event, index) => (
                  <button
                    key={`feed-${index}`}
                    type="button"
                    className={`timeline-item active ${index === 0 ? 'latest-item' : ''}`}
                    onClick={() => onSetMinute(event.minute)}
                  >
                    <p className="timeline-clock">{event.clock}</p>
                    <p className="timeline-text">
                      <span className={event.team === 'Blue' ? 'team-blue' : 'team-red'}>
                        {event.team}
                      </span>{' '}
                      {event.text}
                    </p>
                    <span className="event-type-tag">{event.type}</span>
                  </button>
                ))
              )}
            </div>
          </div>
        </section>

        <section className="panel turning-panel">
          <p className="panel-kicker">Key Turning Points</p>
          <h3>Event Matrix</h3>

          <div className="filter-row" role="tablist" aria-label="Filter event types">
            {EVENT_FILTERS.map((filter) => (
              <button
                key={filter}
                type="button"
                className={`filter-chip ${selectedFilter === filter ? 'active' : ''}`}
                onClick={() => onFilterChange(filter)}
                style={
                  selectedFilter === filter
                    ? { borderColor: EVENT_TYPE_COLORS[filter], color: EVENT_TYPE_COLORS[filter] }
                    : undefined
                }
              >
                {filter}
              </button>
            ))}
          </div>

          <div className="turning-grid" aria-live="polite">
            {matrixEvents.map((event, index) => {
              const isActive = minute >= event.minute;
              return (
                <button
                  key={`matrix-${index}`}
                  type="button"
                  className={`turning-card ${isActive ? 'active' : ''}`}
                  onClick={() => onSetMinute(event.minute)}
                >
                  <p className="timeline-clock">{event.clock}</p>
                  <p className="timeline-text">
                    <span className={event.team === 'Blue' ? 'team-blue' : 'team-red'}>
                      {event.team}
                    </span>{' '}
                    {event.text}
                  </p>
                  <span className="event-type-tag">{event.type}</span>
                </button>
              );
            })}
          </div>
        </section>
      </main>
    </section>
  );
}

// ─── Root ─────────────────────────────────────────────────────────────────────
function App() {
  const [view, setView] = useState('landing');
  const [matchId, setMatchId] = useState('');
  const [showInputError, setShowInputError] = useState(false);
  const [isLoading, setIsLoading] = useState(false);
  const [fetchError, setFetchError] = useState(null);
  const [matchData, setMatchData] = useState(null);
  const [timeMin, setTimeMin] = useState(0);
  const [isPlaying, setIsPlaying] = useState(false);
  const [selectedFilter, setSelectedFilter] = useState('all');

  // ── Derived data ────────────────────────────────────────────────────────────
  const history = useMemo(() => {
    if (!matchData) return [];
    const { minutes, predictions } = matchData;

    return minutes.map((minute, i) => {
      const getProb = (key) => {
        const raw = predictions[key];
        if (raw === undefined || raw === null) return 50;
        if (!Array.isArray(raw)) return raw <= 1.0 ? raw * 100 : raw;
        if (raw.length === 0) return 50;
        const val = raw[i] !== undefined ? raw[i] : raw[raw.length - 1];
        return val <= 1.0 ? val * 100 : val;
      };

      return {
        minute,
        xgboost: getProb('xgboost'),
        lstm: getProb('lstm'),
        logreg: getProb('logreg'),
      };
    });
  }, [matchData]);

  const maxDuration = history.length > 0 ? history[history.length - 1].minute : MATCH_DURATION;

  const probabilities = useMemo(() => {
    if (!history.length || timeMin === 0) return { xgboost: 0, lstm: 0, logreg: 0 };
    const entry = history.find((h) => h.minute === timeMin) || history[history.length - 1];
    return { xgboost: entry.xgboost, lstm: entry.lstm, logreg: entry.logreg };
  }, [history, timeMin]);

  const events = useMemo(() => matchData?.events || [], [matchData]);

  // ── Playback ticker ─────────────────────────────────────────────────────────
  useEffect(() => {
    if (!isPlaying || view !== 'dashboard') return undefined;

    const interval = setInterval(() => {
      setTimeMin((prev) => {
        if (prev >= maxDuration) {
          setIsPlaying(false);
          return prev;
        }
        return prev + 1;
      });
    }, 1200);

    return () => clearInterval(interval);
  }, [isPlaying, view, maxDuration]);

  // ── Handlers ────────────────────────────────────────────────────────────────
  const handleExampleClick = (id) => {
    setMatchId(id);
    setShowInputError(false);
    setFetchError(null);
  };

  const handleMatchIdChange = (value) => {
    setMatchId(value);
    // Clear the validation error as soon as the input becomes valid
    if (showInputError && MATCH_ID_REGEX.test(value)) {
      setShowInputError(false);
    }
  };

  const handleSimulate = async (event) => {
    event.preventDefault();

    const value = matchId.trim();

    // Validate against the strict regex β€” not just "non-empty"
    if (!value || !MATCH_ID_REGEX.test(value)) {
      setShowInputError(true);
      return;
    }

    setShowInputError(false);
    setIsLoading(true);
    setFetchError(null);

    try {
      const isDev = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1';
      const apiUrl = import.meta.env.VITE_API_URL || (isDev ? 'http://localhost:8000' : '');

      const response = await fetch(`${apiUrl}/api/v1/predict/${value}`, {
        method: 'POST',
        headers: {
          'Accept': 'application/json'
        }
      });

      if (!response.ok) {
        const errorData = await response.json().catch(() => ({}));
        throw new Error(errorData.detail || `Server error: ${response.status}`);
      }

      const data = await response.json();
      setMatchData(data);
      setView('dashboard');
      setTimeMin(0);
      setIsPlaying(true);
    } catch (err) {
      setFetchError(err.message);
    } finally {
      setIsLoading(false);
    }
  };

  const handleBackToLanding = () => {
    setView('landing');
    setIsPlaying(false);
  };

  // ── Render ──────────────────────────────────────────────────────────────────
  return (
    <>
      <AnimatedBackground />

      {view === 'landing' ? (
        <LandingView
          matchId={matchId}
          onMatchIdChange={handleMatchIdChange}
          onSimulate={handleSimulate}
          onExampleClick={handleExampleClick}
          error={showInputError}
          isLoading={isLoading}
          fetchError={fetchError}
        />
      ) : (
        <DashboardView
          matchId={matchId}
          minute={timeMin}
          maxDuration={maxDuration}
          probabilities={probabilities}
          history={history}
          events={events}
          isPlaying={isPlaying}
          selectedFilter={selectedFilter}
          onFilterChange={setSelectedFilter}
          onTogglePlayback={() => setIsPlaying((current) => !current)}
          onSetMinute={(minute) => {
            setTimeMin(minute);
            setIsPlaying(false);
          }}
          onBack={handleBackToLanding}
          blueWin={matchData?.blue_win ?? null}
        />
      )}

      <footer className="riot-disclaimer">
        Rift Breakdown isn&#39;t endorsed by Riot Games and doesn&#39;t reflect the views or opinions
        of Riot Games or anyone officially involved in producing or managing Riot Games properties.
        Riot Games, and all associated properties are trademarks or registered trademarks of Riot Games, Inc.
      </footer>
    </>
  );
}

export default App;