File size: 31,951 Bytes
389148d
 
bb07183
389148d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb07183
389148d
bb07183
389148d
 
 
 
bb07183
389148d
 
 
 
 
 
 
 
 
 
 
 
 
459eb5a
 
389148d
 
 
 
bb07183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
389148d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
459eb5a
389148d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb07183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
389148d
 
 
 
 
 
 
 
 
 
 
 
 
bb07183
 
389148d
 
bb07183
 
 
 
 
 
 
 
 
 
 
 
 
 
389148d
bb07183
 
389148d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb07183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
389148d
bb07183
239572f
389148d
 
459eb5a
 
 
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
// frontend/src/pages/History.jsx

import React, { useState, useEffect } from "react";
import { motion, AnimatePresence } from "framer-motion";
import {
  FileText,
  Clock,
  CheckCircle2,
  ChevronRight,
  Download,
  Eye,
  Trash2,
  Search,
  Filter,
  Calendar,
  Upload,
  Cpu,
  TableProperties,
  MonitorPlay,
  TrendingUp,
  TrendingDown,
  Minus,
  AlertCircle,
  X,
  FileSpreadsheet,
  Table2,
} from "lucide-react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Badge } from "@/components/ui/badge";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { cn } from "@/lib/utils";
import { getHistory } from "@/services/api";

// minimal "toast"
const toastSuccess = (msg) => {
  console.log(msg);
};

const stageConfig = {
  uploading: { label: "Uploading", icon: Upload, color: "blue" },
  aiAnalysis: { label: "AI Analysis", icon: Cpu, color: "violet" },
  dataExtraction: { label: "Data Extraction", icon: TableProperties, color: "emerald" },
  outputRendering: { label: "Output Rendering", icon: MonitorPlay, color: "amber" },
};

const variationConfig = {
  fast: { icon: TrendingDown, color: "text-emerald-500", label: "Faster than avg" },
  normal: { icon: Minus, color: "text-slate-400", label: "Normal" },
  slow: { icon: TrendingUp, color: "text-amber-500", label: "Slower than avg" },
  error: { icon: AlertCircle, color: "text-red-500", label: "Error" },
  skipped: { icon: Minus, color: "text-slate-300", label: "Skipped" },
};

export default function History() {
  const [searchQuery, setSearchQuery] = useState("");
  const [selectedStatus, setSelectedStatus] = useState("all");
  const [expandedReport, setExpandedReport] = useState(null);
  const [isExporting, setIsExporting] = useState(false);
  const [history, setHistory] = useState([]);
  const [isLoading, setIsLoading] = useState(true);
  const [error, setError] = useState(null);

  // Fetch history on component mount
  useEffect(() => {
    const fetchHistory = async () => {
      setIsLoading(true);
      setError(null);
      try {
        const data = await getHistory();
        setHistory(data);
      } catch (err) {
        console.error("Failed to fetch history:", err);
        setError(err.message || "Failed to load history");
        setHistory([]); // Fallback to empty array
      } finally {
        setIsLoading(false);
      }
    };

    fetchHistory();
  }, []);

  const filteredHistory = history.filter((item) => {
    const matchesSearch = item.fileName?.toLowerCase().includes(searchQuery.toLowerCase()) ?? false;
    const matchesStatus = selectedStatus === "all" || item.status === selectedStatus;
    return matchesSearch && matchesStatus;
  });

  const formatTime = (ms) => {
    if (ms >= 1000) {
      return `${(ms / 1000).toFixed(2)}s`;
    }
    return `${ms}ms`;
  };

  const formatTimeForExport = (ms) => {
    return ms >= 1000 ? `${(ms / 1000).toFixed(2)}s` : `${ms}ms`;
  };

  const formatDate = (dateString) => {
    const date = new Date(dateString);
    return date.toLocaleDateString("en-US", {
      month: "short",
      day: "numeric",
      hour: "2-digit",
      minute: "2-digit",
    });
  };

  const formatDateForExport = (dateString) => {
    const date = new Date(dateString);
    return date.toISOString().replace("T", " ").slice(0, 19);
  };

  const generateCSV = (data) => {
    const headers = [
      "File Name",
      "File Type",
      "File Size",
      "Extracted At",
      "Status",
      "Confidence (%)",
      "Fields Extracted",
      "Total Time (ms)",
      "Upload Time (ms)",
      "Upload Status",
      "Upload Variation",
      "AI Analysis Time (ms)",
      "AI Analysis Status",
      "AI Analysis Variation",
      "Data Extraction Time (ms)",
      "Data Extraction Status",
      "Data Extraction Variation",
      "Output Rendering Time (ms)",
      "Output Rendering Status",
      "Output Rendering Variation",
      "Error Message",
    ];

    const rows = data.map((item) => [
      item.fileName,
      item.fileType,
      item.fileSize,
      formatDateForExport(item.extractedAt),
      item.status,
      item.confidence,
      item.fieldsExtracted,
      item.totalTime,
      item.stages.uploading.time,
      item.stages.uploading.status,
      item.stages.uploading.variation,
      item.stages.aiAnalysis.time,
      item.stages.aiAnalysis.status,
      item.stages.aiAnalysis.variation,
      item.stages.dataExtraction.time,
      item.stages.dataExtraction.status,
      item.stages.dataExtraction.variation,
      item.stages.outputRendering.time,
      item.stages.outputRendering.status,
      item.stages.outputRendering.variation,
      item.errorMessage || "",
    ]);

    const csvContent = [
      headers.join(","),
      ...rows.map((row) => row.map((cell) => `"${cell}"`).join(",")),
    ].join("\n");

    return csvContent;
  };

  const downloadFile = (content, fileName, mimeType) => {
    const blob = new Blob([content], { type: mimeType });
    const url = URL.createObjectURL(blob);
    const link = document.createElement("a");
    link.href = url;
    link.download = fileName;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    URL.revokeObjectURL(url);
  };

  const handleExportCSV = () => {
    setIsExporting(true);
    setTimeout(() => {
      const csvContent = generateCSV(filteredHistory);
      downloadFile(
        csvContent,
        `extraction_history_${new Date().toISOString().slice(0, 10)}.csv`,
        "text/csv;charset=utf-8;"
      );
      toastSuccess("CSV exported successfully");
      setIsExporting(false);
    }, 500);
  };

  const generateExcelXML = (data) => {
    const headers = [
      "File Name",
      "File Type",
      "File Size",
      "Extracted At",
      "Status",
      "Confidence (%)",
      "Fields Extracted",
      "Total Time (ms)",
      "Upload Time (ms)",
      "Upload Status",
      "Upload Variation",
      "AI Analysis Time (ms)",
      "AI Analysis Status",
      "AI Analysis Variation",
      "Data Extraction Time (ms)",
      "Data Extraction Status",
      "Data Extraction Variation",
      "Output Rendering Time (ms)",
      "Output Rendering Status",
      "Output Rendering Variation",
      "Error Message",
    ];

    const rows = data.map((item) => [
      item.fileName,
      item.fileType,
      item.fileSize,
      formatDateForExport(item.extractedAt),
      item.status,
      item.confidence,
      item.fieldsExtracted,
      item.totalTime,
      item.stages.uploading.time,
      item.stages.uploading.status,
      item.stages.uploading.variation,
      item.stages.aiAnalysis.time,
      item.stages.aiAnalysis.status,
      item.stages.aiAnalysis.variation,
      item.stages.dataExtraction.time,
      item.stages.dataExtraction.status,
      item.stages.dataExtraction.variation,
      item.stages.outputRendering.time,
      item.stages.outputRendering.status,
      item.stages.outputRendering.variation,
      item.errorMessage || "",
    ]);

    let xml = `<?xml version="1.0" encoding="UTF-8"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Worksheet ss:Name="Extraction History">
<Table>
<Row>`;

    headers.forEach((header) => {
      xml += `<Cell><Data ss:Type="String">${header}</Data></Cell>`;
    });
    xml += `</Row>`;

    rows.forEach((row) => {
      xml += `<Row>`;
      row.forEach((cell) => {
        const type = typeof cell === "number" ? "Number" : "String";
        xml += `<Cell><Data ss:Type="${type}">${cell}</Data></Cell>`;
      });
      xml += `</Row>`;
    });

    xml += `</Table></Worksheet></Workbook>`;
    return xml;
  };

  const handleExportExcel = () => {
    setIsExporting(true);
    setTimeout(() => {
      const excelContent = generateExcelXML(filteredHistory);
      downloadFile(
        excelContent,
        `extraction_history_${new Date().toISOString().slice(0, 10)}.xls`,
        "application/vnd.ms-excel"
      );
      toastSuccess("Excel file exported successfully");
      setIsExporting(false);
    }, 500);
  };

  const handleExportSingleReport = (item, format) => {
    if (format === "csv") {
      const csvContent = generateCSV([item]);
      downloadFile(
        csvContent,
        `${item.fileName.replace(/\.[^/.]+$/, "")}_report.csv`,
        "text/csv;charset=utf-8;"
      );
      toastSuccess("Report exported as CSV");
    } else {
      const excelContent = generateExcelXML([item]);
      downloadFile(
        excelContent,
        `${item.fileName.replace(/\.[^/.]+$/, "")}_report.xls`,
        "application/vnd.ms-excel"
      );
      toastSuccess("Report exported as Excel");
    }
  };

  return (
    <div className="min-h-screen bg-[#FAFAFA]">
      {/* Header */}
      <header className="bg-white border-b border-slate-200/80 sticky top-0 z-40">
        <div className="px-8 py-4">
          <h1 className="text-xl font-bold text-slate-900 tracking-tight">
            Extraction History
          </h1>
          <p className="text-sm text-slate-500 mt-0.5">
            View detailed reports and performance metrics for all extractions
          </p>
        </div>
      </header>

      {/* Content */}
      <div className="p-8">
        {/* Filters */}
        <div className="flex items-center gap-4 mb-6">
          <div className="relative flex-1 max-w-md">
            <Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-slate-400" />
            <Input
              placeholder="Search by file name..."
              value={searchQuery}
              onChange={(e) => setSearchQuery(e.target.value)}
              className="pl-10 h-11 rounded-xl border-slate-200"
            />
          </div>
          <Select
            value={selectedStatus}
            onValueChange={(value) => setSelectedStatus(value)}
          >
            <SelectTrigger className="w-40 h-11 rounded-xl border-slate-200">
              <Filter className="h-4 w-4 mr-2 text-slate-400" />
              <SelectValue placeholder="Status" />
            </SelectTrigger>
            <SelectContent>
              <SelectItem value="all">All Status</SelectItem>
              <SelectItem value="completed">Completed</SelectItem>
              <SelectItem value="failed">Failed</SelectItem>
            </SelectContent>
          </Select>

          {/* Export All Button */}
          <DropdownMenu>
            <DropdownMenuTrigger asChild>
              <Button
                className="h-11 px-4 rounded-xl bg-gradient-to-r from-indigo-600 to-violet-600 hover:from-indigo-700 hover:to-violet-700 shadow-lg shadow-indigo-500/25"
                disabled={isExporting || filteredHistory.length === 0}
              >
                {isExporting ? (
                  <motion.div
                    animate={{ rotate: 360 }}
                    transition={{
                      duration: 1,
                      repeat: Infinity,
                      ease: "linear",
                    }}
                    className="mr-2"
                  >
                    <Download className="h-4 w-4" />
                  </motion.div>
                ) : (
                  <Download className="h-4 w-4 mr-2" />
                )}
                Export All
              </Button>
            </DropdownMenuTrigger>
            <DropdownMenuContent
              align="end"
              className="w-48 rounded-xl p-2"
            >
              <DropdownMenuItem
                className="rounded-lg cursor-pointer"
                onClick={handleExportCSV}
              >
                <Table2 className="h-4 w-4 mr-2 text-emerald-600" />
                Export as CSV
              </DropdownMenuItem>
              <DropdownMenuItem
                className="rounded-lg cursor-pointer"
                onClick={handleExportExcel}
              >
                <FileSpreadsheet className="h-4 w-4 mr-2 text-green-600" />
                Export as Excel
              </DropdownMenuItem>
              <DropdownMenuSeparator />
              <div className="px-2 py-1.5 text-xs text-slate-500">
                {filteredHistory.length} records will be exported
              </div>
            </DropdownMenuContent>
          </DropdownMenu>
        </div>

        {/* Stats Overview */}
        <div className="grid grid-cols-1 md:grid-cols-4 gap-4 mb-8">
          {(() => {
            const total = history.length;
            const completed = history.filter((h) => h.status === "completed").length;
            const successRate = total > 0 ? ((completed / total) * 100).toFixed(1) : 0;
            const avgTime = history.length > 0
              ? history.reduce((sum, h) => sum + (h.totalTime || 0), 0) / history.length
              : 0;
            const totalFields = history.reduce((sum, h) => sum + (h.fieldsExtracted || 0), 0);

            return [
              {
                label: "Total Extractions",
                value: total.toString(),
                change: "",
                color: "indigo",
              },
              {
                label: "Success Rate",
                value: `${successRate}%`,
                change: total > 0 ? `${completed}/${total} successful` : "No data",
                color: "emerald",
              },
              {
                label: "Avg. Processing Time",
                value: avgTime >= 1000 ? `${(avgTime / 1000).toFixed(1)}s` : `${Math.round(avgTime)}ms`,
                change: "",
                color: "violet",
              },
              {
                label: "Fields Extracted",
                value: totalFields.toLocaleString(),
                change: "",
                color: "amber",
              },
            ].map((stat, index) => (
            <motion.div
              key={stat.label}
              initial={{ opacity: 0, y: 20 }}
              animate={{ opacity: 1, y: 0 }}
              transition={{ delay: index * 0.1 }}
              className="bg-white rounded-2xl border border-slate-200 p-5"
            >
              <p className="text-sm text-slate-500 mb-1">{stat.label}</p>
              <p className="text-2xl font-bold text-slate-900">{stat.value}</p>
              <p className={`text-xs text-${stat.color}-600 mt-1`}>
                {stat.change}
              </p>
            </motion.div>
            ));
          })()}
        </div>

        {/* Loading State */}
        {isLoading && (
          <div className="text-center py-16">
            <motion.div
              animate={{ rotate: 360 }}
              transition={{ duration: 1, repeat: Infinity, ease: "linear" }}
              className="h-16 w-16 mx-auto rounded-2xl bg-indigo-100 flex items-center justify-center mb-4"
            >
              <Cpu className="h-8 w-8 text-indigo-600" />
            </motion.div>
            <p className="text-slate-500">Loading extraction history...</p>
          </div>
        )}

        {/* History List */}
        {!isLoading && (
          <div className="space-y-4">
          {filteredHistory.map((item, index) => (
            <motion.div
              key={item.id}
              initial={{ opacity: 0, y: 20 }}
              animate={{ opacity: 1, y: 0 }}
              transition={{ delay: index * 0.05 }}
              className="bg-white rounded-2xl border border-slate-200 overflow-hidden"
            >
              {/* Main Row */}
              <div
                className="p-5 cursor-pointer hover:bg-slate-50/50 transition-colors"
                onClick={() =>
                  setExpandedReport(
                    expandedReport === item.id ? null : item.id
                  )
                }
              >
                <div className="flex items-center gap-4">
                  {/* File Icon */}
                  <div
                    className={cn(
                      "h-12 w-12 rounded-xl flex items-center justify-center",
                      item.status === "completed" ? "bg-indigo-50" : "bg-red-50"
                    )}
                  >
                    <FileText
                      className={cn(
                        "h-6 w-6",
                        item.status === "completed"
                          ? "text-indigo-600"
                          : "text-red-500"
                      )}
                    />
                  </div>

                  {/* File Info */}
                  <div className="flex-1 min-w-0">
                    <div className="flex items-center gap-2">
                      <h3 className="font-semibold text-slate-900 truncate">
                        {item.fileName}
                      </h3>
                      <Badge variant="secondary" className="text-xs">
                        {item.fileType}
                      </Badge>
                    </div>
                    <div className="flex items-center gap-4 mt-1 text-sm text-slate-500">
                      <span>{item.fileSize}</span>
                      <span className="flex items-center gap-1">
                        <Calendar className="h-3 w-3" />
                        {formatDate(item.extractedAt)}
                      </span>
                    </div>
                  </div>

                  {/* Stats */}
                  <div className="hidden md:flex items-center gap-6">
                    <div className="text-center">
                      <p className="text-xs text-slate-400">Time</p>
                      <p className="font-semibold text-slate-700">
                        {formatTime(item.totalTime)}
                      </p>
                    </div>
                    <div className="text-center">
                      <p className="text-xs text-slate-400">Fields</p>
                      <p className="font-semibold text-slate-700">
                        {item.fieldsExtracted}
                      </p>
                    </div>
                    <div className="text-center">
                      <p className="text-xs text-slate-400">Confidence</p>
                      <p
                        className={cn(
                          "font-semibold",
                          item.confidence >= 95
                            ? "text-emerald-600"
                            : item.confidence >= 90
                            ? "text-amber-600"
                            : "text-red-600"
                        )}
                      >
                        {item.confidence > 0 ? `${item.confidence}%` : "-"}
                      </p>
                    </div>
                  </div>

                  {/* Status & Actions */}
                  <div className="flex items-center gap-3">
                    <Badge
                      className={cn(
                        "capitalize",
                        item.status === "completed"
                          ? "bg-emerald-50 text-emerald-700 border-emerald-200"
                          : "bg-red-50 text-red-700 border-red-200"
                      )}
                    >
                      {item.status === "completed" ? (
                        <CheckCircle2 className="h-3 w-3 mr-1" />
                      ) : (
                        <AlertCircle className="h-3 w-3 mr-1" />
                      )}
                      {item.status}
                    </Badge>
                    <ChevronRight
                      className={cn(
                        "h-5 w-5 text-slate-400 transition-transform",
                        expandedReport === item.id && "rotate-90"
                      )}
                    />
                  </div>
                </div>
              </div>

              {/* Expanded Report */}
              <AnimatePresence>
                {expandedReport === item.id && (
                  <motion.div
                    initial={{ height: 0, opacity: 0 }}
                    animate={{ height: "auto", opacity: 1 }}
                    exit={{ height: 0, opacity: 0 }}
                    transition={{ duration: 0.2 }}
                    className="overflow-hidden"
                  >
                    <div className="px-5 pb-5 pt-2 border-t border-slate-100">
                      {/* Error Message */}
                      {item.errorMessage && (
                        <div className="mb-4 p-4 bg-red-50 border border-red-100 rounded-xl">
                          <div className="flex items-center gap-2 text-red-700">
                            <AlertCircle className="h-4 w-4" />
                            <span className="font-medium">Error Details</span>
                          </div>
                          <p className="text-sm text-red-600 mt-1">
                            {item.errorMessage}
                          </p>
                        </div>
                      )}

                      {/* Performance Report Header */}
                      <div className="flex items-center justify-between mb-4">
                        <h4 className="font-semibold text-slate-800">
                          Performance Report
                        </h4>
                        <div className="flex items-center gap-2">
                          <Button
                            variant="ghost"
                            size="sm"
                            className="h-8 text-xs"
                          >
                            <Eye className="h-3 w-3 mr-1" />
                            View Output
                          </Button>
                          <DropdownMenu>
                            <DropdownMenuTrigger asChild>
                              <Button
                                variant="outline"
                                size="sm"
                                className="h-8 text-xs"
                              >
                                <Download className="h-3 w-3 mr-1" />
                                Export Report
                              </Button>
                            </DropdownMenuTrigger>
                            <DropdownMenuContent
                              align="end"
                              className="w-44 rounded-xl p-2"
                            >
                              <DropdownMenuItem
                                className="rounded-lg cursor-pointer text-xs"
                                onClick={(e) => {
                                  e.stopPropagation();
                                  handleExportSingleReport(item, "csv");
                                }}
                              >
                                <Table2 className="h-3 w-3 mr-2 text-emerald-600" />
                                Download CSV
                              </DropdownMenuItem>
                              <DropdownMenuItem
                                className="rounded-lg cursor-pointer text-xs"
                                onClick={(e) => {
                                  e.stopPropagation();
                                  handleExportSingleReport(item, "excel");
                                }}
                              >
                                <FileSpreadsheet className="h-3 w-3 mr-2 text-green-600" />
                                Download Excel
                              </DropdownMenuItem>
                            </DropdownMenuContent>
                          </DropdownMenu>
                        </div>
                      </div>

                      {/* Stage Timing Cards */}
                      <div className="grid grid-cols-1 md:grid-cols-4 gap-4">
                        {Object.entries(item.stages).map(
                          ([stageKey, stageData]) => {
                            const config = stageConfig[stageKey];
                            const variationInfo =
                              variationConfig[stageData.variation];
                            const Icon = config.icon;
                            const VariationIcon = variationInfo.icon;

                            return (
                              <div
                                key={stageKey}
                                className={cn(
                                  "relative p-4 rounded-xl border",
                                  stageData.status === "completed"
                                    ? "bg-slate-50 border-slate-200"
                                    : stageData.status === "failed"
                                    ? "bg-red-50 border-red-200"
                                    : "bg-slate-50/50 border-slate-100"
                                )}
                              >
                                <div className="flex items-center gap-2 mb-3">
                                  <div
                                    className={cn(
                                      "h-8 w-8 rounded-lg flex items-center justify-center",
                                      `bg-${config.color}-100`
                                    )}
                                  >
                                    <Icon
                                      className={cn(
                                        "h-4 w-4",
                                        `text-${config.color}-600`
                                      )}
                                    />
                                  </div>
                                  <span className="text-sm font-medium text-slate-700">
                                    {config.label}
                                  </span>
                                </div>

                                <div className="flex items-end justify-between">
                                  <div>
                                    <p
                                      className={cn(
                                        "text-2xl font-bold",
                                        stageData.status === "skipped"
                                          ? "text-slate-300"
                                          : stageData.status === "failed"
                                          ? "text-red-600"
                                          : "text-slate-900"
                                      )}
                                    >
                                      {stageData.status === "skipped"
                                        ? "-"
                                        : formatTime(stageData.time)}
                                    </p>
                                    {stageData.status !== "skipped" && (
                                      <div className="flex items-center gap-1 mt-1">
                                        <VariationIcon
                                          className={cn(
                                            "h-3 w-3",
                                            variationInfo.color
                                          )}
                                        />
                                        <span
                                          className={cn(
                                            "text-xs",
                                            variationInfo.color
                                          )}
                                        >
                                          {variationInfo.label}
                                        </span>
                                      </div>
                                    )}
                                  </div>

                                  {stageData.status === "completed" && (
                                    <CheckCircle2 className="h-5 w-5 text-emerald-500" />
                                  )}
                                  {stageData.status === "failed" && (
                                    <X className="h-5 w-5 text-red-500" />
                                  )}
                                </div>

                                {/* Progress bar */}
                                <div className="mt-3 h-1.5 bg-slate-200 rounded-full overflow-hidden">
                                  <motion.div
                                    initial={{ width: 0 }}
                                    animate={{
                                      width:
                                        stageData.status === "completed"
                                          ? "100%"
                                          : stageData.status === "failed"
                                          ? "60%"
                                          : "0%",
                                    }}
                                    transition={{ duration: 0.5, delay: 0.2 }}
                                    className={cn(
                                      "h-full rounded-full",
                                      stageData.status === "failed"
                                        ? "bg-red-500"
                                        : `bg-${config.color}-500`
                                    )}
                                  />
                                </div>
                              </div>
                            );
                          }
                        )}
                      </div>

                      {/* Total Time Summary */}
                      <div className="mt-4 flex items-center justify-between p-4 bg-gradient-to-r from-indigo-50 to-violet-50 rounded-xl border border-indigo-100">
                        <div className="flex items-center gap-3">
                          <Clock className="h-5 w-5 text-indigo-600" />
                          <div>
                            <p className="text-sm font-medium text-slate-700">
                              Total Processing Time
                            </p>
                            <p className="text-xs text-slate-500">
                              From upload to output ready
                            </p>
                          </div>
                        </div>
                        <div className="text-right">
                          <p className="text-2xl font-bold text-indigo-600">
                            {formatTime(item.totalTime)}
                          </p>
                          <p className="text-xs text-slate-500">
                            {item.status === "completed"
                              ? "Completed successfully"
                              : "Process failed"}
                          </p>
                        </div>
                      </div>
                    </div>
                  </motion.div>
                )}
              </AnimatePresence>
            </motion.div>
          ))}
          {filteredHistory.length === 0 && !error && (
            <div className="text-center py-16">
              <div className="h-20 w-20 mx-auto rounded-2xl bg-slate-100 flex items-center justify-center mb-4">
                <FileText className="h-10 w-10 text-slate-300" />
              </div>
              <p className="text-slate-500 mb-2">
                {history.length === 0
                  ? "No extraction history yet"
                  : "No extractions match your filters"}
              </p>
              {history.length === 0 && (
                <p className="text-sm text-slate-400">
                  Upload a document to get started
                </p>
              )}
            </div>
          )}
        </div>
        )}
      </div>
    </div>
  );
}