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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
"""
ICH Screening Web Application
==============================
Features:
  1. Upload a .dcm file -> run AI model -> display screening report
  2. Browse past screening reports with date, outcome, band, urgency filters
  3. View execution logs from inference runs

Run:
    python webapp/app.py
    Open http://127.0.0.1:7860
"""

from __future__ import annotations
import run_interface as ri
import csv
import datetime
import json
import math
import logging
import os
import shutil
import sys
import tempfile
import threading
import time
import uuid
import zipfile
from collections import Counter
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any

try:
    from dotenv import load_dotenv
except Exception:
    load_dotenv = None

try:
    import blackbox_recorder as bbr
except Exception:
    class _NoopRecorder:
        def configure(self, **_kwargs):
            return None

        def start(self):
            return None

        def stop(self):
            return None

        def save_report(self, _path: str):
            return None

        def save_json(self, _path: str):
            return None

    bbr = _NoopRecorder()

from flask import (
    Flask, abort, flash, g, jsonify, redirect,
    render_template, request, send_from_directory, url_for,
)
from werkzeug.utils import secure_filename


# ══════════════════════════════════════════════════════════════════════════
#  PATH CONFIGURATION
# ══════════════════════════════════════════════════════════════════════════

BASE_DIR    = Path(__file__).resolve().parent        # webapp/
PROJECT_DIR = BASE_DIR                               # project root
TEST_DIR    = BASE_DIR
MODEL_DIR   = BASE_DIR / "download_imp"
OUTPUT_DIR  = MODEL_DIR / "outputs"
REPORTS_DIR = OUTPUT_DIR / "reports"
SUMMARY_CSV = OUTPUT_DIR / "report_summary.csv"
CALIB_JSON  = MODEL_DIR / "calibration_params.json"
NORM_JSON   = MODEL_DIR / "normalization_stats.json"
MODEL_PATH  = MODEL_DIR / "best_model_fold4.pth"
UPLOAD_DIR  = BASE_DIR / "uploads"
LOGS_DIR    = BASE_DIR / "logs"


def _env_bool(name: str, default: bool) -> bool:
    raw = os.environ.get(name)
    if raw is None:
        return default
    return raw.strip().lower() in ("1", "true", "yes", "on")


def _env_int(name: str, default: int, *, minimum: int | None = None) -> int:
    raw = os.environ.get(name)
    if raw is None:
        return default
    try:
        value = int(raw)
    except ValueError:
        return default
    if minimum is not None and value < minimum:
        return default
    return value


# ══════════════════════════════════════════════════════════════════════════
#  FLASK SETUP
# ══════════════════════════════════════════════════════════════════════════

if load_dotenv is not None:
    load_dotenv(BASE_DIR / ".env")

APP_DEBUG = _env_bool("ICH_APP_DEBUG", True)
APP_PORT = _env_int("ICH_APP_PORT", 7860, minimum=1)
MAX_UPLOAD_MB = _env_int("ICH_MAX_UPLOAD_MB", 2048, minimum=1)
LOG_LEVEL_NAME = os.environ.get("ICH_LOG_LEVEL", "INFO").strip().upper()
LOG_LEVEL = getattr(logging, LOG_LEVEL_NAME, logging.INFO)
SECRET_KEY = os.environ.get("ICH_SECRET_KEY", "").strip()

app = Flask(__name__, template_folder="templates", static_folder="static")
app.secret_key = SECRET_KEY or os.urandom(24)
app.config["MAX_CONTENT_LENGTH"] = MAX_UPLOAD_MB * 1024 * 1024

# Local mode: enables server-side directory scanning.
# Auto-detected (running from source) or forced via env var.
LOCAL_MODE = _env_bool("ICH_LOCAL_MODE", True)

logging.basicConfig(
    level=LOG_LEVEL,
    format="%(asctime)s | %(levelname)s | %(message)s",
)
logger = logging.getLogger("ich_app")


# ══════════════════════════════════════════════════════════════════════════
#  BLACKBOX RECORDER β€” traces inference function calls
#
#  We configure it once at module level.  start()/stop() bracket each
#  inference run.  After each run, the trace is saved to logs/ as both a
#  human-readable .txt and a structured .json.
# ══════════════════════════════════════════════════════════════════════════

LOGS_DIR.mkdir(parents=True, exist_ok=True)

bbr.configure(
    include=["run_interface", "app"],
    capture_args=True,
    capture_returns=True,
    sampling_rate=1.0,
)


def _save_trace(image_id: str) -> dict:
    """
    Save the current blackbox trace to logs/ and return metadata about it.
    Called immediately after bbr.stop().
    """
    ts = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
    base = f"{ts}_{image_id}"
    txt_path  = LOGS_DIR / f"{base}.txt"
    json_path = LOGS_DIR / f"{base}.json"

    try:
        bbr.save_report(str(txt_path))
    except Exception:
        logger.warning("Could not save text trace for %s", image_id)

    try:
        bbr.save_json(str(json_path))
    except Exception:
        logger.warning("Could not save JSON trace for %s", image_id)

    return {
        "timestamp": ts,
        "image_id":  image_id,
        "txt_file":  txt_path.name if txt_path.exists() else None,
        "json_file": json_path.name if json_path.exists() else None,
    }


# ══════════════════════════════════════════════════════════════════════════
#  BATCH PROCESSING STATE
#
#  Each batch job is a background thread processing a list of .dcm paths.
#  The UI polls /batch/status/<id> for live progress.
# ══════════════════════════════════════════════════════════════════════════

_BATCHES: dict[str, dict[str, Any]] = {}
_BATCHES_LOCK = threading.Lock()


def _new_batch(total: int, temp_dir: str | None = None) -> str:
    """Create a fresh batch record and return its unique ID."""
    batch_id = uuid.uuid4().hex[:12]
    with _BATCHES_LOCK:
        _BATCHES[batch_id] = {
            "status":       "running",     # running | completed | failed
            "total":        total,
            "processed":    0,
            "succeeded":    0,
            "failed_ids":   [],
            "current_file": "",
            "image_ids":    [],            # successfully processed IDs
            "started_at":   datetime.datetime.now().isoformat(),
            "finished_at":  None,
            "error":        None,
            "temp_dir":     temp_dir,      # cleaned up after completion
        }
    return batch_id


def _batch_update(batch_id: str, **kw):
    """Thread-safe update of a batch record."""
    with _BATCHES_LOCK:
        if batch_id in _BATCHES:
            _BATCHES[batch_id].update(kw)


def _run_batch_worker(batch_id: str, dcm_paths: list[Path]):
    """
    Background thread: process a list of .dcm files sequentially.
    Updates the batch record after each file for real-time UI feedback.
    """
    succeeded_ids: list[str] = []
    failed_ids: list[str] = []

    for i, path in enumerate(dcm_paths, 1):
        image_id = path.stem
        _batch_update(batch_id, current_file=image_id, processed=i - 1)

        try:
            report, _trace = _run_inference_on_dcm(path)
            if report is not None:
                succeeded_ids.append(image_id)
            else:
                failed_ids.append(image_id)
        except Exception as e:
            logger.error("Batch %s: failed %s β€” %s", batch_id, image_id, e)
            failed_ids.append(image_id)

        _batch_update(
            batch_id,
            processed=i,
            succeeded=len(succeeded_ids),
            image_ids=list(succeeded_ids),
            failed_ids=list(failed_ids),
        )

    # Clean up temp directory if one was used (ZIP extraction)
    with _BATCHES_LOCK:
        b = _BATCHES.get(batch_id, {})
        td = b.get("temp_dir")
    if td and Path(td).exists():
        shutil.rmtree(td, ignore_errors=True)

    _batch_update(
        batch_id,
        status="completed",
        current_file="",
        finished_at=datetime.datetime.now().isoformat(),
    )
    # Force cache reload on next page view
    _CACHE["data_signature"] = None
    logger.info(
        "Batch %s complete: %d/%d succeeded, %d failed",
        batch_id, len(succeeded_ids), len(dcm_paths), len(failed_ids),
    )


def _start_batch(dcm_paths: list[Path], temp_dir: str | None = None) -> str:
    """Create a batch job & launch its worker thread. Returns batch_id."""
    batch_id = _new_batch(total=len(dcm_paths), temp_dir=temp_dir)
    t = threading.Thread(
        target=_run_batch_worker,
        args=(batch_id, dcm_paths),
        daemon=True,
        name=f"batch-{batch_id}",
    )
    t.start()
    return batch_id


# ══════════════════════════════════════════════════════════════════════════
#  IN-MEMORY CACHE
# ══════════════════════════════════════════════════════════════════════════

_CACHE: dict[str, Any] = {
    "data_signature":       None,
    "cases":                {},
    "rows_sorted":          [],
    "data_last_refresh_ms": None,
    "data_last_cache_hit":  False,
    "calib_signature":      None,
    "calib":                {},
    "norm_signature":       None,
    "norm":                 {},
}


# ══════════════════════════════════════════════════════════════════════════
#  MODEL STATE β€” lazy-loaded on first upload
# ══════════════════════════════════════════════════════════════════════════

_MODEL: dict[str, Any] = {
    "loaded":        False,
    "model":         None,
    "grad_cam":      None,
    "loaded_folds":  [],
    "transform":     None,
    "device":        None,
    "temperature":   None,
    "calib_cfg":     None,
    "inference_mod": None,
}


def _ensure_model_loaded() -> bool:
    """Lazy-load the ML model on first inference request."""
    if _MODEL["loaded"]:
        return True

    try:
        import torch

        sys.path.insert(0, str(BASE_DIR))

        device = "cuda" if torch.cuda.is_available() else "cpu"
        fold_selection = os.environ.get("ICH_FOLD_SELECTION", "ensemble")

        with open(CALIB_JSON) as f:
            calib_cfg = json.load(f)

        if NORM_JSON.exists():
            with open(NORM_JSON) as f:
                norm = json.load(f)
            mean = norm.get("mean_3ch", [0.162136, 0.141483, 0.183675])
            std = norm.get("std_3ch", [0.312067, 0.283885, 0.305968])
        else:
            mean, std = [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]

        models, grad_cams, loaded_folds = ri.load_runtime_models(device, fold_selection)
        if not models:
            logger.error("No fold checkpoints could be loaded from %s", MODEL_DIR)
            return False

        transform = ri.T.Compose([
            ri.T.ToPILImage(),
            ri.T.ToTensor(),
            ri.T.Normalize(mean=mean, std=std),
        ])

        _MODEL.update({
            "loaded":        True,
            "model":         models,
            "grad_cam":      grad_cams,
            "loaded_folds":  loaded_folds,
            "transform":     transform,
            "device":        device,
            "temperature":   float(calib_cfg.get("temperature", 1.0)),
            "calib_cfg":     calib_cfg,
            "inference_mod": ri,
        })
        logger.info(
            "Model loaded (device=%s, fold_selection=%s, folds=%s)",
            device,
            fold_selection,
            loaded_folds,
        )
        return True

    except Exception as e:
        logger.error("Model loading failed: %s", e, exc_info=True)
        return False


def _run_inference_on_dcm(dcm_path: Path) -> tuple[dict | None, dict | None]:
    """
    Run inference on one .dcm file, with blackbox tracing.
    Returns (report_dict, trace_metadata) or (None, None) on failure.
    """
    if not _ensure_model_loaded():
        return None, None

    ri = _MODEL["inference_mod"]
    image_id = dcm_path.stem

    # Start tracing this inference run
    bbr.start()

    try:
        img_rgb = ri.dicom_to_rgb(str(dcm_path), size=ri.IMG_SIZE)

        inference = ri.infer_single(
            img_rgb,
            _MODEL["model"],
            _MODEL["grad_cam"],
            _MODEL["transform"],
            _MODEL["device"],
            _MODEL["temperature"],
        )

        REPORTS_DIR.mkdir(parents=True, exist_ok=True)
        report = ri.build_report(
            image_id, inference, _MODEL["calib_cfg"],
            REPORTS_DIR, img_rgb, true_label=None,
        )
        pred = report.get("prediction", {})
        pred.setdefault("raw_probability", inference.get("raw_prob_any"))
        pred.setdefault("calibrated_probability", inference.get("cal_prob_any"))
        pred.setdefault("decision_threshold", pred.get("decision_threshold_any"))
        report["prediction"] = pred

        report_path = REPORTS_DIR / f"{image_id}_report.json"
        with open(report_path, "w") as f:
            json.dump(report, f, indent=2)

        _append_to_summary_csv(image_id, report)
        _CACHE["data_signature"] = None

    except Exception:
        bbr.stop()
        raise

    # Stop tracing and save the execution log
    bbr.stop()
    trace_meta = _save_trace(image_id)

    return report, trace_meta


def _append_to_summary_csv(image_id: str, report: dict):
    """Append one report row to the summary CSV."""
    pred = report["prediction"]
    row = {
        "image_id":          image_id,
        "true_label":        "",
        "screening_outcome": pred["screening_outcome"],
        "raw_prob":          pred["raw_probability"],
        "cal_prob":          pred["calibrated_probability"],
        "confidence_band":   pred["confidence_band"],
        "triage_action":     report["triage"]["action"],
        "urgency":           report["triage"]["urgency"],
        "generated_at":      report.get("generated_at", ""),
    }

    file_exists = SUMMARY_CSV.exists()
    OUTPUT_DIR.mkdir(parents=True, exist_ok=True)

    with open(SUMMARY_CSV, "a", newline="", encoding="utf-8") as f:
        writer = csv.DictWriter(f, fieldnames=row.keys())
        if not file_exists:
            writer.writeheader()
        writer.writerow(row)


# ══════════════════════════════════════════════════════════════════════════
#  DATA MODEL
# ══════════════════════════════════════════════════════════════════════════

@dataclass
class CaseRow:
    image_id:     str        = ""
    outcome:      str        = "Unknown"
    raw_prob:     float|None = None
    cal_prob:     float|None = None
    band:         str        = "N/A"
    triage:       str        = "N/A"
    urgency:      str        = "N/A"
    true_label:   str        = ""
    generated_at: str        = ""     # ISO timestamp from report JSON
    report_file:  str|None   = None
    gradcam_file: str|None   = None

    @property
    def date_display(self) -> str:
        """Format the ISO timestamp as a short readable date."""
        if not self.generated_at:
            return "β€”"
        try:
            dt = datetime.datetime.fromisoformat(self.generated_at)
            return dt.strftime("%Y-%m-%d %H:%M")
        except (ValueError, TypeError):
            return self.generated_at[:16]

    @property
    def is_positive(self) -> bool:
        return "no hemorrhage" not in self.outcome.lower()


# ══════════════════════════════════════════════════════════════════════════
#  UTILITIES
# ══════════════════════════════════════════════════════════════════════════

def _to_float(value: Any) -> float | None:
    try:
        return float(value) if value not in (None, "") else None
    except (TypeError, ValueError):
        return None


def _file_mtime(path: Path) -> int:
    try:
        return path.stat().st_mtime_ns if path.exists() else -1
    except OSError:
        return -1


def _data_signature() -> tuple[int, int]:
    return _file_mtime(REPORTS_DIR), _file_mtime(SUMMARY_CSV)


def _parse_positive_int(value: str | None, default: int) -> int:
    try:
        n = int(value or default)
        return n if n > 0 else default
    except (TypeError, ValueError):
        return default


# ══════════════════════════════════════════════════════════════════════════
#  DATA LOADING
# ══════════════════════════════════════════════════════════════════════════

def _load_summary_csv() -> dict[str, dict[str, Any]]:
    """Read report_summary.csv into memory, keyed by image_id."""
    if not SUMMARY_CSV.exists():
        return {}
    rows: dict[str, dict[str, Any]] = {}
    with SUMMARY_CSV.open("r", encoding="utf-8") as f:
        for row in csv.DictReader(f):
            iid = (row.get("image_id") or "").strip()
            if not iid:
                continue
            rows[iid] = {
                "image_id":     iid,
                "outcome":      row.get("screening_outcome", "Unknown"),
                "raw_prob":     _to_float(row.get("raw_prob")),
                "cal_prob":     _to_float(row.get("cal_prob")),
                "band":         row.get("confidence_band") or "N/A",
                "triage":       row.get("triage_action")   or "N/A",
                "urgency":      row.get("urgency")         or "N/A",
                "true_label":   row.get("true_label", ""),
                "generated_at": row.get("generated_at", ""),
            }
    return rows


def _scan_report_assets() -> tuple[set[str], set[str]]:
    """One dir walk to find which image IDs have JSON and PNG files."""
    report_ids:  set[str] = set()
    gradcam_ids: set[str] = set()
    if not REPORTS_DIR.exists():
        return report_ids, gradcam_ids
    for path in REPORTS_DIR.iterdir():
        if not path.is_file():
            continue
        if path.name.endswith("_report.json"):
            report_ids.add(path.name[:-12])
        elif path.name.endswith("_gradcam.png"):
            gradcam_ids.add(path.name[:-12])
    return report_ids, gradcam_ids


def _read_generated_at(image_id: str) -> str:
    """Read the generated_at timestamp from a report JSON file."""
    path = REPORTS_DIR / f"{image_id}_report.json"
    if not path.exists():
        return ""
    try:
        data = json.loads(path.read_text("utf-8"))
        return data.get("generated_at", "")
    except (json.JSONDecodeError, OSError):
        return ""


def _load_cases_from_json() -> dict[str, CaseRow]:
    """Fallback: read each *_report.json when CSV is unavailable."""
    summary = _load_summary_csv()
    cases: dict[str, CaseRow] = {}
    for rp in sorted(REPORTS_DIR.glob("*_report.json")):
        try:
            payload = json.loads(rp.read_text("utf-8"))
        except (json.JSONDecodeError, OSError):
            continue
        iid  = str(payload.get("image_id", rp.stem.replace("_report", ""))).strip()
        pred = payload.get("prediction", {})
        tri  = payload.get("triage", {})
        expl = payload.get("explainability", {})
        sr   = summary.get(iid, {})
        gc   = Path(str(expl.get("heatmap_path", ""))).name or None
        cases[iid] = CaseRow(
            image_id=iid,
            outcome=pred.get("screening_outcome", sr.get("outcome", "Unknown")),
            raw_prob=_to_float(pred.get("raw_probability", sr.get("raw_prob"))),
            cal_prob=_to_float(pred.get("calibrated_probability", sr.get("cal_prob"))),
            band=pred.get("confidence_band", sr.get("band", "N/A")),
            triage=tri.get("action", sr.get("triage", "N/A")),
            urgency=tri.get("urgency", sr.get("urgency", "N/A")),
            true_label=str(payload.get("ground_truth_label", sr.get("true_label", ""))),
            generated_at=payload.get("generated_at", ""),
            report_file=rp.name,
            gradcam_file=gc,
        )
    return cases


def load_cases_cached() -> dict[str, CaseRow]:
    """Return all cases, re-reading from disk only when files change."""
    sig = _data_signature()
    if _CACHE["data_signature"] == sig:
        _CACHE["data_last_cache_hit"] = True
        return _CACHE["cases"]

    start   = time.perf_counter()
    summary = _load_summary_csv()

    if summary:
        report_ids, gradcam_ids = _scan_report_assets()
        cases = {}
        for iid, sr in summary.items():
            # Resolve generated_at: prefer CSV value, fall back to JSON file
            gen_at = sr.get("generated_at", "")
            if not gen_at and iid in report_ids:
                gen_at = _read_generated_at(iid)

            cases[iid] = CaseRow(
                image_id=iid,
                outcome=sr.get("outcome", "Unknown"),
                raw_prob=_to_float(sr.get("raw_prob")),
                cal_prob=_to_float(sr.get("cal_prob")),
                band=sr.get("band", "N/A"),
                triage=sr.get("triage", "N/A"),
                urgency=sr.get("urgency", "N/A"),
                true_label=sr.get("true_label", ""),
                generated_at=gen_at,
                report_file=f"{iid}_report.json" if iid in report_ids else None,
                gradcam_file=f"{iid}_gradcam.png" if iid in gradcam_ids else None,
            )
    elif REPORTS_DIR.exists():
        cases = _load_cases_from_json()
    else:
        cases = {}

    elapsed_ms = (time.perf_counter() - start) * 1000
    _CACHE.update({
        "data_signature":       sig,
        "cases":                cases,
        "rows_sorted":          sorted(cases.values(), key=lambda c: c.image_id),
        "data_last_refresh_ms": elapsed_ms,
        "data_last_cache_hit":  False,
    })
    logger.info("Cache refresh: %d cases in %.1f ms", len(cases), elapsed_ms)
    return cases


def load_case_payload(image_id: str) -> dict[str, Any] | None:
    """Load full JSON report for one case (Raw JSON button)."""
    path = REPORTS_DIR / f"{image_id}_report.json"
    if not path.exists():
        return None
    try:
        return json.loads(path.read_text("utf-8"))
    except (json.JSONDecodeError, OSError):
        return None


def compute_stats(rows: list[CaseRow]) -> dict[str, Any]:
    """Compute summary statistics for the dashboard cards."""
    total    = len(rows)
    positive = sum(1 for r in rows if r.is_positive)
    urgent   = sum(1 for r in rows if r.urgency.upper() == "URGENT")
    heatmaps = sum(1 for r in rows if r.gradcam_file)
    cal_probs = [r.cal_prob for r in rows if r.cal_prob is not None]
    avg_cal   = sum(cal_probs) / len(cal_probs) if cal_probs else 0.0
    pos_rate  = (positive / total * 100) if total else 0.0

    # Date range
    dates = sorted(r.generated_at for r in rows if r.generated_at)
    newest = dates[-1] if dates else ""
    oldest = dates[0]  if dates else ""

    return {
        "total":          total,
        "positive":       positive,
        "negative":       total - positive,
        "urgent":         urgent,
        "heatmaps":       heatmaps,
        "avg_cal_prob":   avg_cal,
        "pos_rate":       pos_rate,
        "band_counts":    dict(Counter(r.band.upper()    for r in rows)),
        "urgency_counts": dict(Counter(r.urgency.upper() for r in rows)),
        "newest_date":    newest,
        "oldest_date":    oldest,
    }


def _load_json_cached(path: Path, sig_key: str, data_key: str, label: str) -> dict[str, Any]:
    """Mtime-based JSON cache loader for calibration/normalization."""
    sig = _file_mtime(path)
    if _CACHE[sig_key] == sig:
        return _CACHE[data_key]
    data: dict[str, Any] = {}
    if path.exists():
        try:
            data = json.loads(path.read_text("utf-8"))
        except (json.JSONDecodeError, OSError):
            logger.warning("Could not read %s", path)
    _CACHE[sig_key] = sig
    _CACHE[data_key] = data
    return data


def load_calibration() -> dict[str, Any]:
    calib = _load_json_cached(CALIB_JSON, "calib_signature", "calib", "Calibration")
    if not calib:
        return {}
    # Backward-compatible aliases expected by templates.
    return {
        **calib,
        "method": calib.get("method", calib.get("best_method", "N/A")),
        "temperature": calib.get("temperature", 1.0),
        "raw_ece": calib.get("ece_raw", 0.0),
        "cal_ece": calib.get("ece_isotonic", calib.get("ece_temp", 0.0)),
        "raw_brier": calib.get("brier_raw", 0.0),
        "cal_brier": calib.get("brier_isotonic", calib.get("brier_temp", 0.0)),
        "calibrated_threshold": calib.get("threshold_at_spec90", 0.5),
        "base_threshold": calib.get("base_threshold", 0.5),
        "high_threshold": calib.get("high_threshold", calib.get("triage_high_thresh", 0.7)),
        "low_threshold": calib.get("low_threshold", calib.get("triage_low_thresh", 0.3)),
    }


def load_normalization() -> dict[str, Any]:
    return _load_json_cached(NORM_JSON, "norm_signature", "norm", "Normalization")


def filter_cases(
    rows: list[CaseRow],
    q: str,
    band: str,
    urgency: str,
    outcome: str,
    sort_by: str,
) -> list[CaseRow]:
    """Apply text search, dropdown filters, and sorting."""
    if q:
        ql = q.lower()
        rows = [r for r in rows if ql in r.image_id.lower() or ql in r.outcome.lower()]
    if band:
        rows = [r for r in rows if r.band.upper() == band.upper()]
    if urgency:
        rows = [r for r in rows if r.urgency.upper() == urgency.upper()]
    if outcome == "POSITIVE":
        rows = [r for r in rows if r.is_positive]
    elif outcome == "NEGATIVE":
        rows = [r for r in rows if not r.is_positive]

    if sort_by == "date_desc":
        rows = sorted(rows, key=lambda r: r.generated_at or "", reverse=True)
    elif sort_by == "date_asc":
        rows = sorted(rows, key=lambda r: r.generated_at or "")
    elif sort_by == "prob_desc":
        rows = sorted(rows, key=lambda r: r.cal_prob or 0, reverse=True)
    elif sort_by == "prob_asc":
        rows = sorted(rows, key=lambda r: r.cal_prob or 0)
    # default: sorted by image_id (already the case from cache)

    return rows


def load_logs() -> list[dict]:
    """Scan the logs/ directory and return metadata for each trace."""
    if not LOGS_DIR.exists():
        return []

    log_files: dict[str, dict] = {}  # base_name -> {txt_file, json_file, ...}

    for path in sorted(LOGS_DIR.iterdir(), reverse=True):
        if not path.is_file():
            continue
        stem = path.stem  # e.g. "20260228_153000_ID_abc123"
        if path.suffix == ".txt":
            log_files.setdefault(stem, {})["txt_file"] = path.name
            # Parse out timestamp and image_id from filename
            parts = stem.split("_", 2)
            if len(parts) >= 3:
                log_files[stem]["timestamp"] = f"{parts[0]}_{parts[1]}"
                log_files[stem]["image_id"]  = parts[2]
            log_files[stem]["size_kb"] = round(path.stat().st_size / 1024, 1)
        elif path.suffix == ".json":
            log_files.setdefault(stem, {})["json_file"] = path.name

    entries = []
    for stem in sorted(log_files, reverse=True):
        info = log_files[stem]
        ts_raw = info.get("timestamp", "")
        try:
            dt = datetime.datetime.strptime(ts_raw, "%Y%m%d_%H%M%S")
            display = dt.strftime("%Y-%m-%d %H:%M:%S")
        except ValueError:
            display = ts_raw
        entries.append({
            "stem":      stem,
            "timestamp": display,
            "image_id":  info.get("image_id", ""),
            "txt_file":  info.get("txt_file"),
            "json_file": info.get("json_file"),
            "size_kb":   info.get("size_kb", 0),
        })

    return entries


# ══════════════════════════════════════════════════════════════════════════
#  MIDDLEWARE
# ══════════════════════════════════════════════════════════════════════════

@app.before_request
def _start_timer():
    g._start_time = time.perf_counter()


@app.after_request
def _log_timing(response):
    elapsed = (time.perf_counter() - getattr(g, "_start_time", time.perf_counter())) * 1000
    logger.info("%s %s -> %s (%.1f ms)", request.method, request.path, response.status_code, elapsed)
    return response


# ══════════════════════════════════════════════════════════════════════════
#  ROUTES
# ══════════════════════════════════════════════════════════════════════════

@app.route("/")
def home():
    """Landing page with quick stats and navigation."""
    load_cases_cached()
    all_rows = _CACHE["rows_sorted"]
    stats = compute_stats(all_rows)
    log_count = len(list(LOGS_DIR.glob("*.txt"))) if LOGS_DIR.exists() else 0
    return render_template("home.html", stats=stats, log_count=log_count)


@app.route("/upload")
def upload():
    return render_template("upload.html", local_mode=LOCAL_MODE)


@app.route("/analyze", methods=["POST"])
def analyze():
    """
    Accept one or more .dcm files (or a .zip) and run inference.

    Single file  β†’ synchronous, redirect straight to the report.
    Multiple     β†’ asynchronous batch, redirect to progress page.
    """
    files = request.files.getlist("file")
    files = [f for f in files if f.filename]

    if not files:
        flash("No files were uploaded.", "error")
        return redirect(url_for("upload"))

    UPLOAD_DIR.mkdir(parents=True, exist_ok=True)

    # ── Collect all .dcm paths (expand .zip archives) ────────────────
    dcm_paths: list[Path] = []
    temp_dir: str | None = None      # set if a zip needed extraction

    for f in files:
        fname = f.filename.lower()

        if fname.endswith(".zip"):
            temp_dir = tempfile.mkdtemp(prefix="ich_zip_")
            zip_save = Path(temp_dir) / secure_filename(f.filename)
            f.save(str(zip_save))
            try:
                with zipfile.ZipFile(zip_save, "r") as zf:
                    zf.extractall(temp_dir)
            except zipfile.BadZipFile:
                shutil.rmtree(temp_dir, ignore_errors=True)
                flash("The uploaded ZIP file is corrupted.", "error")
                return redirect(url_for("upload"))
            # Recursively find .dcm inside extracted tree
            dcm_paths.extend(sorted(Path(temp_dir).rglob("*.dcm")))

        elif fname.endswith(".dcm"):
            safe = secure_filename(f.filename)
            save_path = UPLOAD_DIR / safe
            f.save(str(save_path))
            dcm_paths.append(save_path)

        else:
            # skip non-dcm / non-zip silently
            continue

    if not dcm_paths:
        flash("No .dcm files found in the upload.", "error")
        if temp_dir:
            shutil.rmtree(temp_dir, ignore_errors=True)
        return redirect(url_for("upload"))

    # ── Single file β†’ synchronous (fast path) ────────────────────────
    if len(dcm_paths) == 1 and temp_dir is None:
        single_path = dcm_paths[0]
        try:
            report, trace = _run_inference_on_dcm(single_path)
            if report is None:
                flash("Model failed to load. Check server logs.", "error")
                return redirect(url_for("upload"))
            return redirect(url_for("case_detail", image_id=single_path.stem))
        except Exception as e:
            logger.error("Analysis failed for %s: %s", single_path.name, e, exc_info=True)
            flash(f"Analysis failed: {e}", "error")
            return redirect(url_for("upload"))
        finally:
            if single_path.exists() and single_path.parent == UPLOAD_DIR:
                single_path.unlink()

    # ── Multiple files β†’ asynchronous batch ──────────────────────────
    batch_id = _start_batch(dcm_paths, temp_dir=temp_dir)
    logger.info("Batch %s started: %d files", batch_id, len(dcm_paths))
    return redirect(url_for("batch_progress", batch_id=batch_id))


@app.route("/analyze/directory", methods=["POST"])
def analyze_directory():
    """
    Local-only route: scan a server-side directory for .dcm files and
    start a batch job.  Disabled when LOCAL_MODE is off.
    """
    if not LOCAL_MODE:
        abort(403)

    dir_path_str = request.form.get("dir_path", "").strip()
    if not dir_path_str:
        flash("Please enter a directory path.", "error")
        return redirect(url_for("upload"))

    scan_dir = Path(dir_path_str)
    if not scan_dir.is_dir():
        flash(f"Directory not found: {dir_path_str}", "error")
        return redirect(url_for("upload"))

    dcm_paths = sorted(scan_dir.rglob("*.dcm"))
    if not dcm_paths:
        flash(f"No .dcm files found in: {dir_path_str}", "error")
        return redirect(url_for("upload"))

    batch_id = _start_batch(dcm_paths)
    logger.info("Directory batch %s started: %d files from %s", batch_id, len(dcm_paths), dir_path_str)
    return redirect(url_for("batch_progress", batch_id=batch_id))


@app.route("/batch/progress/<batch_id>")
def batch_progress(batch_id: str):
    """Batch progress page β€” polls /batch/status/<id> via JS."""
    with _BATCHES_LOCK:
        batch = _BATCHES.get(batch_id)
    if not batch:
        abort(404)
    return render_template("batch_progress.html", batch_id=batch_id, batch=batch)


@app.route("/batch/status/<batch_id>")
def batch_status(batch_id: str):
    """JSON endpoint polled by the progress page for live updates."""
    with _BATCHES_LOCK:
        batch = _BATCHES.get(batch_id)
    if not batch:
        return jsonify({"error": "not found"}), 404
    # Return a safe copy (no Path objects)
    return jsonify({
        "status":       batch["status"],
        "total":        batch["total"],
        "processed":    batch["processed"],
        "succeeded":    batch["succeeded"],
        "failed_count": len(batch["failed_ids"]),
        "failed_ids":   batch["failed_ids"][:20],   # cap for payload size
        "current_file": batch["current_file"],
        "image_ids":    batch["image_ids"][-5:],     # last 5 for display
        "started_at":   batch["started_at"],
        "finished_at":  batch["finished_at"],
    })


@app.route("/reports")
def reports():
    """Past reports page with filtering, sorting, and pagination."""
    route_start = time.perf_counter()

    load_cases_cached()
    all_rows = _CACHE["rows_sorted"]

    # Read all filter/sort/pagination params from query string
    q         = request.args.get("q", "").strip()
    band      = request.args.get("band", "").strip()
    urgency   = request.args.get("urgency", "").strip()
    outcome   = request.args.get("outcome", "").strip()
    sort_by   = request.args.get("sort", "").strip()
    page      = _parse_positive_int(request.args.get("page"), 1)
    page_size = _parse_positive_int(request.args.get("page_size"), 50)
    if page_size not in (10, 50, 100):
        page_size = 50

    filtered    = filter_cases(all_rows, q, band, urgency, outcome, sort_by)
    stats       = compute_stats(filtered)
    total       = len(filtered)
    total_pages = max(1, math.ceil(total / page_size))
    page        = min(page, total_pages)
    start_idx   = (page - 1) * page_size
    rows        = filtered[start_idx: start_idx + page_size]
    route_ms    = (time.perf_counter() - route_start) * 1000

    return render_template(
        "reports.html",
        rows=rows,
        stats=stats,
        calib=load_calibration(),
        q=q, band=band, urgency=urgency, outcome=outcome, sort=sort_by,
        page=page,
        page_size=page_size,
        page_start=start_idx,
        total_pages=total_pages,
        total_items=total,
        total_cases=len(all_rows),
        route_compute_ms=route_ms,
        data_refresh_ms=_CACHE["data_last_refresh_ms"],
        data_cache_hit=_CACHE["data_last_cache_hit"],
    )


@app.route("/case/<image_id>")
def case_detail(image_id: str):
    """Individual case report page."""
    cases = load_cases_cached()
    row = cases.get(image_id)
    if not row:
        abort(404)
    payload = load_case_payload(image_id)
    return render_template("detail.html", row=row, payload=payload)


@app.route("/logs")
def logs_page():
    """Execution logs page."""
    entries = load_logs()
    return render_template("logs.html", logs=entries)


@app.route("/logs/view/<path:filename>")
def serve_log(filename: str):
    """Serve a log file (txt or json) for viewing."""
    if not LOGS_DIR.exists():
        abort(404)
    return send_from_directory(LOGS_DIR, filename)


@app.route("/evaluation")
def evaluation():
    load_cases_cached()
    all_rows = _CACHE["rows_sorted"]

    cal_probs = [r.cal_prob for r in all_rows if r.cal_prob is not None]
    bins = [0] * 10
    for p in cal_probs:
        bins[min(int(p * 10), 9)] += 1

    band_data = {}
    for bnd in ("HIGH", "MEDIUM", "LOW"):
        subset   = [r for r in all_rows if r.band.upper() == bnd]
        positive = sum(1 for r in subset if r.is_positive)
        band_data[bnd] = {
            "total":    len(subset),
            "positive": positive,
            "negative": len(subset) - positive,
        }

    return render_template(
        "evaluation.html",
        stats=compute_stats(all_rows),
        calib=load_calibration(),
        norm=load_normalization(),
        bins=bins,
        band_data=band_data,
        total=len(all_rows),
    )


@app.route("/about")
def about():
    return render_template("about.html", calib=load_calibration())


@app.route("/gradcam/<path:filename>")
def serve_gradcam(filename: str):
    if not REPORTS_DIR.exists():
        abort(404)
    return send_from_directory(REPORTS_DIR, filename)


@app.route("/report-json/<path:filename>")
def serve_report_json(filename: str):
    if not REPORTS_DIR.exists():
        abort(404)
    return send_from_directory(REPORTS_DIR, filename)


# ══════════════════════════════════════════════════════════════════════════
#  ENTRY POINT
# ══════════════════════════════════════════════════════════════════════════

if __name__ == "__main__":
    print("=" * 60)
    print("  ICH Screening Web Application")
    print(f"  Data  ->  {OUTPUT_DIR}")
    print(f"  Logs  ->  {LOGS_DIR}")
    print(f"  Open  ->  http://127.0.0.1:{APP_PORT}")
    print("=" * 60)
    app.run(debug=APP_DEBUG, port=APP_PORT)