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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78b81a5
 
 
a61ba58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState, useEffect } from "react";
import { useNavigate } from "react-router-dom";
import { storyApi } from "../utils/api";
import { useGameSession } from "../hooks/useGameSession";
import {
  Box,
  Paper,
  Typography,
  Accordion,
  AccordionSummary,
  AccordionDetails,
  Chip,
  Button,
  CircularProgress,
  Alert,
  Divider,
  Stack,
  IconButton,
  Tooltip,
  Tab,
  Tabs,
  Grid,
} from "@mui/material";
import {
  ExpandMore as ExpandMoreIcon,
  Refresh as RefreshIcon,
  BugReport as BugReportIcon,
  Timer as TimerIcon,
  LocationOn as LocationIcon,
  Psychology as PsychologyIcon,
  History as HistoryIcon,
  Image as ImageIcon,
  TextFields as TextFieldsIcon,
  List as ListIcon,
  Palette as PaletteIcon,
  Category as CategoryIcon,
  AccessTime as AccessTimeIcon,
  ArrowForward as ArrowForwardIcon,
} from "@mui/icons-material";
import { DebugConsole } from "../components/DebugConsole";
import { Metric } from "../components/Metric";
import { UniverseView } from "../components/UniverseView";
import { UniverseMetrics } from "../components/UniverseMetrics";

const Debug = () => {
  const navigate = useNavigate();
  const [gameState, setGameState] = useState(null);
  const [currentStory, setCurrentStory] = useState(null);
  const [error, setError] = useState(null);
  const [currentTab, setCurrentTab] = useState(0);
  const [expandedPanel, setExpandedPanel] = useState("current");
  const [isLoading, setIsLoading] = useState(false);
  const historyContainerRef = React.useRef(null);

  const {
    sessionId,
    universe,
    isLoading: isSessionLoading,
    error: sessionError,
  } = useGameSession();

  const handleTabChange = (event, newValue) => {
    setCurrentTab(newValue);
  };

  const handlePanelChange = (panel) => (event, isExpanded) => {
    setExpandedPanel(isExpanded ? panel : false);
  };

  // Initialize game
  const initializeGame = async () => {
    try {
      setIsLoading(true);
      const response = await storyApi.start(sessionId);

      // Construire l'entrée d'historique initiale
      const initialHistoryEntry = {
        segment: response.story_text,
        player_choice: null,
        available_choices: response.choices.map((choice) => choice.text),
        time: response.time,
        location: response.location,
        previous_choice: response.previous_choice,
      };

      setGameState({
        universe_style: universe?.style,
        universe_genre: universe?.genre,
        universe_epoch: universe?.epoch,
        universe_macguffin: universe?.macguffin,
        universe_selected_artist: universe?.style?.selected_artist,
        story_beat: 0,
        story_history: [initialHistoryEntry],
      });
      setCurrentStory(response);
    } catch (err) {
      setError(err.message);
    } finally {
      setIsLoading(false);
    }
  };

  // Make a choice
  const makeChoice = async (choiceIndex) => {
    try {
      setIsLoading(true);
      const response = await storyApi.makeChoice(choiceIndex + 1, sessionId);
      setCurrentStory(response);

      // Construire l'entrée d'historique
      const historyEntry = {
        segment: response.story_text,
        player_choice: currentStory.choices[choiceIndex].text,
        available_choices: currentStory.choices.map((choice) => choice.text),
        time: response.time,
        location: response.location,
        previous_choice: response.previous_choice,
      };

      setGameState((prev) => ({
        ...prev,
        story_history: [...(prev.story_history || []), historyEntry],
        story_beat: (prev.story_beat || 0) + 1,
        universe_macguffin: prev.universe_macguffin,
      }));
    } catch (err) {
      setError(err.message);
    } finally {
      setIsLoading(false);
    }
  };

  useEffect(() => {
    if (sessionId && !isSessionLoading && !gameState) {
      initializeGame();
    }
  }, [sessionId, isSessionLoading, gameState]);

  // Ajout de l'effet pour le défilement automatique
  useEffect(() => {
    if (historyContainerRef.current && gameState?.story_history?.length > 0) {
      historyContainerRef.current.scrollTop =
        historyContainerRef.current.scrollHeight;
    }
  }, [gameState?.story_history]);

  // Render history entries
  const renderHistoryEntry = (entry, idx) => (
    <Box
      key={idx}
      sx={{ mb: 2, p: 2, bgcolor: "background.paper", borderRadius: 1 }}
    >
      <Stack spacing={1}>
        {/* Previous Choice (if any) */}
        {entry.previous_choice && (
          <Box
            sx={{
              display: "flex",
              alignItems: "center",
              gap: 1,
              color: "text.secondary",
            }}
          >
            <ArrowForwardIcon fontSize="small" />
            <Typography variant="body2" sx={{ fontStyle: "italic" }}>
              Choix précédent : {entry.previous_choice}
            </Typography>
          </Box>
        )}

        {/* Time and Location */}
        <Box sx={{ display: "flex", gap: 2, color: "text.secondary" }}>
          <Box sx={{ display: "flex", alignItems: "center", gap: 0.5 }}>
            <TimerIcon fontSize="small" />
            <Typography variant="body2">{entry.time}</Typography>
          </Box>
          <Box sx={{ display: "flex", alignItems: "center", gap: 0.5 }}>
            <LocationIcon fontSize="small" />
            <Typography variant="body2">{entry.location}</Typography>
          </Box>
        </Box>

        {/* Story Text */}
        <Typography>{entry.segment}</Typography>

        {/* Available Choices */}
        {entry.available_choices && entry.available_choices.length > 0 && (
          <Box sx={{ mt: 1 }}>
            <Typography variant="body2" color="text.secondary">
              Choix disponibles :
            </Typography>
            <Box sx={{ display: "flex", gap: 1, flexWrap: "wrap", mt: 0.5 }}>
              {entry.available_choices.map((choice, choiceIdx) => (
                <Chip
                  key={choiceIdx}
                  label={choice}
                  size="small"
                  color={choice === entry.player_choice ? "primary" : "default"}
                  variant={
                    choice === entry.player_choice ? "filled" : "outlined"
                  }
                />
              ))}
            </Box>
          </Box>
        )}
      </Stack>
    </Box>
  );

  if (error || sessionError) {
    return (
      <Box p={3}>
        <Alert
          severity="error"
          action={
            <Button
              color="inherit"
              size="small"
              onClick={() => window.location.reload()}
            >
              Restart
            </Button>
          }
        >
          {error || sessionError}
        </Alert>
      </Box>
    );
  }

  if (isSessionLoading || !gameState) {
    return (
      <Box
        display="flex"
        alignItems="center"
        justifyContent="center"
        minHeight="100vh"
      >
        <CircularProgress />
      </Box>
    );
  }

  return (
    <Box
      sx={{
        height: "100vh",
        display: "flex",
        flexDirection: "column",
        overflow: "hidden",
        backgroundColor: "background.default",
      }}
    >
      {/* Header - plus compact */}
      <Box
        sx={{
          p: 1.5,
          display: "flex",
          alignItems: "center",
          gap: 2,
          borderBottom: 1,
          borderColor: "divider",
          backgroundColor: "background.paper",
        }}
      >
        <BugReportIcon color="primary" />
        <Typography variant="h6" component="h1">
          Debug Mode
        </Typography>
        <Tooltip title="Restart">
          <IconButton onClick={() => window.location.reload()} size="small">
            <RefreshIcon />
          </IconButton>
        </Tooltip>
        <Tabs value={currentTab} onChange={handleTabChange} sx={{ ml: "auto" }}>
          <Tab icon={<PsychologyIcon />} label="Current State" />
          <Tab icon={<PaletteIcon />} label="Universe" />
          <Tab icon={<BugReportIcon />} label="Debug" />
        </Tabs>
      </Box>

      {/* Content - scrollable */}
      <Box sx={{ flexGrow: 1, overflow: "auto", p: 2 }}>
        {/* Current State Tab */}
        {currentTab === 0 && currentStory && (
          <Stack spacing={2}>
            {/* Universe Info & Game State */}
            <Paper
              variant="outlined"
              sx={{
                p: 2,
                backgroundColor: "background.paper",
              }}
            >
              <Grid container spacing={2}>
                {/* Universe Info */}
                <Grid item xs={12} md={6}>
                  <UniverseMetrics
                    style={gameState.universe_style}
                    genre={gameState.universe_genre}
                    epoch={gameState.universe_epoch}
                    macguffin={gameState.universe_macguffin}
                  />
                </Grid>

                {/* Game State */}
                <Grid item xs={12} md={6}>
                  <Stack spacing={1}>
                    <Typography variant="subtitle2" color="secondary.main">
                      Game State
                    </Typography>
                    <Box
                      sx={{
                        p: 1.5,
                        borderRadius: 1,
                        backgroundColor: currentStory.is_victory
                          ? "success.dark"
                          : currentStory.is_death
                          ? "error.dark"
                          : "background.paper",
                        border: 1,
                        borderColor: "divider",
                      }}
                    >
                      <Stack spacing={2}>
                        <Stack
                          direction="row"
                          spacing={1}
                          flexWrap="wrap"
                          gap={1}
                        >
                          <Metric
                            icon={<TimerIcon fontSize="small" />}
                            label="Time"
                            value={currentStory.time}
                            color="secondary"
                          />
                          <Metric
                            icon={<LocationIcon fontSize="small" />}
                            label="Location"
                            value={currentStory.location}
                            color="secondary"
                          />
                          <Metric
                            icon={<PsychologyIcon fontSize="small" />}
                            label="Story Beat"
                            value={gameState.story_beat}
                            color="secondary"
                          />
                        </Stack>
                        <Stack spacing={1}>
                          <Box
                            sx={{
                              display: "flex",
                              alignItems: "center",
                              gap: 1,
                            }}
                          >
                            <Box
                              sx={{
                                width: 8,
                                height: 8,
                                borderRadius: "50%",
                                backgroundColor: "primary.main",
                                animation: "pulse 1.5s infinite",
                                "@keyframes pulse": {
                                  "0%": {
                                    transform: "scale(.95)",
                                    boxShadow:
                                      "0 0 0 0 rgba(144, 202, 249, 0.7)",
                                  },
                                  "70%": {
                                    transform: "scale(1)",
                                    boxShadow:
                                      "0 0 0 6px rgba(144, 202, 249, 0)",
                                  },
                                  "100%": {
                                    transform: "scale(.95)",
                                    boxShadow: "0 0 0 0 rgba(144, 202, 249, 0)",
                                  },
                                },
                              }}
                            />
                            <Typography
                              variant="subtitle2"
                              sx={{ color: "primary.main" }}
                            >
                              Story in Progress
                            </Typography>
                          </Box>
                          <Stack direction="row" spacing={1}>
                            <Box
                              sx={{
                                display: "flex",
                                alignItems: "center",
                                gap: 1,
                                p: 0.5,
                                borderRadius: 1,
                                backgroundColor: currentStory.is_death
                                  ? "error.dark"
                                  : "background.paper",
                                border: 1,
                                borderColor: "divider",
                                minWidth: 100,
                              }}
                            >
                              <Typography
                                variant="caption"
                                sx={{
                                  color: currentStory.is_death
                                    ? "white"
                                    : "text.secondary",
                                }}
                              >
                                Death: {currentStory.is_death ? "Yes" : "No"}
                              </Typography>
                            </Box>
                            <Box
                              sx={{
                                display: "flex",
                                alignItems: "center",
                                gap: 1,
                                p: 0.5,
                                borderRadius: 1,
                                backgroundColor: currentStory.is_victory
                                  ? "success.dark"
                                  : "background.paper",
                                border: 1,
                                borderColor: "divider",
                                minWidth: 100,
                              }}
                            >
                              <Typography
                                variant="caption"
                                sx={{
                                  color: currentStory.is_victory
                                    ? "white"
                                    : "text.secondary",
                                }}
                              >
                                Victory:{" "}
                                {currentStory.is_victory ? "Yes" : "No"}
                              </Typography>
                            </Box>
                          </Stack>
                        </Stack>
                      </Stack>
                    </Box>
                  </Stack>
                </Grid>
              </Grid>
            </Paper>

            {/* Story and Choices Row */}
            <Grid container spacing={2}>
              {/* Story Content */}
              <Grid item xs={12} md={8}>
                <Paper variant="outlined">
                  <Box sx={{ p: 1.5, borderBottom: 1, borderColor: "divider" }}>
                    <Typography variant="subtitle2">Story</Typography>
                  </Box>
                  <Box sx={{ p: 1.5, backgroundColor: "background.default" }}>
                    <Typography variant="body2">
                      {currentStory.story_text}
                    </Typography>
                  </Box>
                </Paper>
              </Grid>

              {/* Interactive Choices */}
              <Grid item xs={12} md={4}>
                <Paper variant="outlined">
                  <Box sx={{ p: 1.5, borderBottom: 1, borderColor: "divider" }}>
                    <Typography variant="subtitle2">
                      Available Choices
                    </Typography>
                  </Box>
                  <Stack spacing={1} sx={{ p: 1.5 }}>
                    {currentStory.choices && currentStory.choices.length > 0 ? (
                      currentStory.choices.map((choice, idx) => (
                        <Button
                          key={idx}
                          variant="contained"
                          color="primary"
                          onClick={() => makeChoice(idx)}
                          disabled={isLoading}
                          sx={{ mt: 1 }}
                          endIcon={
                            isLoading ? (
                              <CircularProgress size={16} />
                            ) : (
                              <ArrowForwardIcon />
                            )
                          }
                        >
                          {choice.text}
                        </Button>
                      ))
                    ) : (
                      <Typography variant="body2" color="text.secondary">
                        No choices available
                      </Typography>
                    )}
                  </Stack>
                </Paper>
              </Grid>
            </Grid>

            {/* Story History */}
            <Paper variant="outlined">
              <Box
                sx={{
                  p: 1.5,
                  borderBottom: 1,
                  borderColor: "divider",
                  display: "flex",
                  alignItems: "center",
                  gap: 1,
                }}
              >
                <HistoryIcon fontSize="small" color="action" />
                <Typography variant="subtitle2">Story History</Typography>
              </Box>
              <Box
                ref={historyContainerRef}
                sx={{
                  maxHeight: "300px",
                  overflow: "auto",
                  scrollBehavior: "smooth",
                }}
              >
                {gameState.story_history.length > 0 ? (
                  gameState.story_history.map((entry, idx) =>
                    renderHistoryEntry(entry, idx)
                  )
                ) : (
                  <Box sx={{ p: 2, textAlign: "center" }}>
                    <Typography variant="body2" color="text.secondary">
                      No history available
                    </Typography>
                  </Box>
                )}
              </Box>
            </Paper>

            {/* Image Prompts */}
            <Paper variant="outlined">
              <Box sx={{ p: 1.5, borderBottom: 1, borderColor: "divider" }}>
                <Typography variant="subtitle2">Image Prompts</Typography>
              </Box>
              <Stack spacing={1} sx={{ p: 1.5 }}>
                {currentStory.image_prompts.map((prompt, idx) => (
                  <Box
                    key={idx}
                    sx={{
                      p: 1,
                      backgroundColor: "background.default",
                      borderRadius: 1,
                      border: 1,
                      borderColor: "divider",
                    }}
                  >
                    <Typography
                      variant="caption"
                      sx={{
                        fontFamily: "monospace",
                        color: "text.secondary",
                        display: "flex",
                        gap: 1,
                      }}
                    >
                      <Typography
                        component="span"
                        variant="caption"
                        color="primary.main"
                      >
                        {idx + 1}.
                      </Typography>
                      {prompt}
                    </Typography>
                  </Box>
                ))}
              </Stack>
            </Paper>
          </Stack>
        )}

        {/* Universe Tab */}
        {currentTab === 1 && <UniverseView universe={universe} />}

        {/* Debug Tab */}
        {currentTab === 2 && (
          <DebugConsole gameState={gameState} currentStory={currentStory} />
        )}
      </Box>
    </Box>
  );
};

export default Debug;