File size: 41,558 Bytes
f9170e1
 
 
 
 
 
 
 
64b5214
ce1bfe9
64b5214
 
 
 
 
 
 
 
 
 
 
f9170e1
5735a6e
64b5214
 
 
f9170e1
64b5214
 
 
 
 
 
 
 
f9170e1
 
64b5214
 
5735a6e
64b5214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5735a6e
 
f9170e1
 
 
 
 
 
 
 
 
 
 
 
64b5214
 
f9170e1
 
64b5214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5735a6e
64b5214
5735a6e
64b5214
 
 
 
 
5735a6e
64b5214
5735a6e
 
 
 
64b5214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce1bfe9
64b5214
ce1bfe9
64b5214
ce1bfe9
f9170e1
ce1bfe9
64b5214
 
 
ce1bfe9
64b5214
ce1bfe9
64b5214
 
 
 
 
 
 
 
 
 
 
 
ce1bfe9
f9170e1
ce1bfe9
64b5214
ce1bfe9
64b5214
ce1bfe9
64b5214
 
ce1bfe9
f9170e1
ce1bfe9
64b5214
ce1bfe9
64b5214
ce1bfe9
64b5214
 
ce1bfe9
f9170e1
ce1bfe9
64b5214
ce1bfe9
5735a6e
ce1bfe9
5735a6e
 
ce1bfe9
f9170e1
ce1bfe9
5735a6e
64b5214
 
f9170e1
 
64b5214
 
 
 
 
 
 
 
 
 
f9170e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64b5214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9170e1
64b5214
 
 
 
f9170e1
 
128e72e
f9170e1
64b5214
 
 
 
 
 
 
 
 
 
 
 
f9170e1
64b5214
 
 
 
 
 
 
 
 
 
 
 
 
 
f9170e1
 
 
64b5214
 
f9170e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64b5214
 
 
f9170e1
64b5214
f9170e1
64b5214
 
f9170e1
64b5214
 
f9170e1
 
 
 
 
 
64b5214
ce1bfe9
64b5214
f9170e1
 
 
64b5214
 
f9170e1
64b5214
 
 
 
 
 
 
 
 
 
 
 
 
ce1bfe9
64b5214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9170e1
64b5214
 
f9170e1
64b5214
 
 
 
 
 
 
5735a6e
64b5214
 
 
 
 
 
 
 
5735a6e
128e72e
64b5214
 
128e72e
5735a6e
128e72e
5735a6e
128e72e
64b5214
 
128e72e
64b5214
 
 
 
 
 
 
 
 
 
 
 
 
 
5735a6e
 
64b5214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5735a6e
64b5214
f9170e1
64b5214
 
 
 
 
 
 
 
 
5735a6e
64b5214
 
 
5735a6e
64b5214
f9170e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64b5214
128e72e
 
 
ce1bfe9
64b5214
f9170e1
64b5214
 
f9170e1
64b5214
 
 
 
 
 
 
 
128e72e
64b5214
 
 
 
 
 
 
 
 
 
 
 
f9170e1
ce1bfe9
 
 
 
 
64b5214
f9170e1
 
 
 
9e3254e
128e72e
64b5214
f9170e1
64b5214
 
 
f9170e1
64b5214
 
 
 
 
f9170e1
64b5214
 
 
 
f9170e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64b5214
 
 
 
 
 
 
 
 
5735a6e
64b5214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9170e1
64b5214
 
f9170e1
64b5214
 
 
 
 
 
 
 
 
 
 
 
 
f9170e1
64b5214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9170e1
64b5214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9170e1
64b5214
 
 
 
 
ce1bfe9
 
f9170e1
 
 
 
 
64b5214
 
5735a6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce1bfe9
5735a6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9170e1
 
 
5735a6e
128e72e
5735a6e
 
 
 
 
 
 
 
 
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
# Smart Office Attendance β€” Patched + Tabs (+ Anomalies) β€” Silent Post-Run Video + Stable CSV
# Patches:
#   (1) Run inference silently; write annotated frames to processed_output.mp4.
#       If the file exceeds 200 MB, split into sequential parts and expose Play buttons.
#   (2) Cache CSV bytes in st.session_state.csv_data so download clicks don't wipe results.
#
# Everything else is unchanged from your latest version: tabs, counters, attendance, reports,
# anomalies, face DB (auto folder scan), YOLOv8 + MediaPipe, metrics, event batching (SF disabled).

import os, io, json, math, time, tempfile, traceback, uuid
from pathlib import Path
from dataclasses import dataclass
from datetime import datetime, date

import cv2
import numpy as np
import pandas as pd
import streamlit as st
from collections import deque

# ------------------------- Runtime switches -------------------------
ENABLE_SF = False  # keep SF code paths but do not call API

MAX_PREVIEW_W = 1280
FACE_UPDATE_EVERY_N = 4
EVENT_COOLDOWN_SEC = 1.0
PHONE_PERSIST_N = 2
SLEEP_IDLE_SECONDS = 60.0
CHECKOUT_MISS_FRAMES = 90

DEFAULT_INSTANCE = "HQ-Instance-01"
DEFAULT_FLOOR = "1"
DEFAULT_ZONE  = "Work Area"
DEFAULT_CAMERA_ID = "CAM01"

SPLIT_MAX_BYTES = 200 * 1024 * 1024  # 200 MB threshold for processed video splitting

# ------------------------- Page & Tabs -------------------------
st.set_page_config(page_title="Smart Office Attendance", page_icon="πŸŽ₯", layout="wide")
tabs = st.tabs(["🟒 Live", "πŸ‘₯ Employees", "πŸ“ˆ Reports", "🚩 Anomalies"])

# ------------------------- lazy model imports -------------------------
@st.cache_resource(show_spinner=False)
def _lazy_ultralytics():
    from ultralytics import YOLO
    return YOLO

@st.cache_resource(show_spinner=False)
def _lazy_mediapipe():
    import mediapipe as mp
    return mp

YOLO = _lazy_ultralytics()
mp   = _lazy_mediapipe()

@st.cache_resource(show_spinner=False)
def load_yolo_model():
    return YOLO("yolov8n.pt")  # nano model for speed

@st.cache_resource(show_spinner=False)
def load_mp_face():
    mpfd = mp.solutions.face_detection
    det = mpfd.FaceDetection(model_selection=1, min_detection_confidence=0.5)
    return det

model   = load_yolo_model()
mp_face = load_mp_face()

# ------------------------- Session State -------------------------
st.session_state.setdefault("events", pd.DataFrame(
    columns=["id","ts","camera","employee","activity","zone","confidence","run_id"]))
st.session_state.setdefault("current_run_id", None)
st.session_state.setdefault("prev_boxes_by_name", {})
st.session_state.setdefault("act_votes", {})
st.session_state.setdefault("on_phone_start_ns", {})
st.session_state.setdefault("on_phone_accum_ns", {})
st.session_state.setdefault("emp_counters", {})           # name -> dict(working, idle, sleep)
st.session_state.setdefault("emp_first_seen", set())       # names seen (attendance)
st.session_state.setdefault("last_seen_frame", {})         # name -> frame last seen
st.session_state.setdefault("idle_start_ts", {})           # name -> epoch when became idle
st.session_state.setdefault("did_checkout", set())         # names checked-out already this run
st.session_state.setdefault("last_emit_map", {})
st.session_state.setdefault("attendance_rows", [])         # local CSV for attendance
st.session_state.setdefault("metric_rows", [])             # local CSV for metrics (session appends)
st.session_state.setdefault("anomalies", [])               # anomaly tickets (local workflow)
st.session_state.setdefault("anomaly_counter", 1)          # incremental id
# NEW: stable post-run assets (video parts + csvs)
st.session_state.setdefault("last_run_assets", {
    "video_parts": [],    # list of absolute file paths to processed video parts
    "summary": "",        # paths for quick display if needed
})
st.session_state.setdefault("csv_data", {
    "events": None,
    "summary": None,
    "attendance": None,
    "metrics": None,
    "anomalies": None,
})

def log_ui(line, ok=True):
    # kept for internal use if needed; no visible log box rendered
    pass

# ------------------------- Face DB -------------------------
def _list_employee_images(root="employees"):
    """Return {employee_name: [image paths...]} for ALL subfolders under root."""
    exts = {".jpg", ".jpeg", ".png", ".bmp", ".webp"}
    rootp = Path(root)
    mapping = {}
    if not rootp.exists():
        return mapping
    for dirpath, _, filenames in os.walk(rootp):
        rel = Path(dirpath).relative_to(rootp)
        if rel == Path("."):
            continue
        emp_name = rel.parts[0]               # first folder under /employees
        for fn in filenames:
            if Path(fn).suffix.lower() in exts:
                mapping.setdefault(emp_name, []).append(str(Path(dirpath) / fn))
    print("βœ… Loaded face DB for:", list(mapping.keys()))
    return mapping

def _face_embed_gray(frame_bgr, bbox_xyxy):
    """Crop face to 112x112 gray & L2-normalize as simple embedding."""
    x1,y1,x2,y2 = [max(0,int(v)) for v in bbox_xyxy]
    crop = frame_bgr[y1:y2, x1:x2]
    if crop.size == 0: return None
    g = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY)
    g = cv2.resize(g, (112,112), interpolation=cv2.INTER_AREA)
    vec = g.astype(np.float32).reshape(-1)
    n = np.linalg.norm(vec) + 1e-6
    return (vec / n)

def _detect_faces_mediapipe(frame_bgr):
    h, w = frame_bgr.shape[:2]
    rgb = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
    res = mp_face.process(rgb)
    out = []
    if res.detections:
        for d in res.detections:
            bb = d.location_data.relative_bounding_box
            x1 = int(bb.xmin * w)
            y1 = int(bb.ymin * h)
            x2 = int((bb.xmin + bb.width) * w)
            y2 = int((bb.ymin + bb.height) * h)
            out.append((x1,y1,x2,y2, float(d.score[0] if d.score else 0.0)))
    return out

@st.cache_resource(show_spinner=False)
def build_employee_db():
    img_map = _list_employee_images("employees")
    db = {}   # name -> [embeddings]
    for name, paths in img_map.items():
        vecs = []
        for path in paths[:12]:  # collect up to 12 samples / employee
            try:
                bgr = cv2.imread(path)
                if bgr is None:
                    continue
                faces = _detect_faces_mediapipe(bgr)
                if faces:
                    # use largest face
                    faces.sort(key=lambda t: (t[2]-t[0])*(t[3]-t[1]), reverse=True)
                    emb = _face_embed_gray(bgr, faces[0][:4])
                    if emb is not None:
                        vecs.append(emb)
                else:
                    # fallback: central crop to avoid empty employees
                    h, w = bgr.shape[:2]
                    cx1 = max(0, int(w*0.25)); cy1 = max(0, int(h*0.20))
                    cx2 = min(w, int(w*0.75)); cy2 = min(h, int(h*0.80))
                    emb = _face_embed_gray(bgr, (cx1, cy1, cx2, cy2))
                    if emb is not None:
                        vecs.append(emb)
            except Exception:
                pass
        if vecs:
            db[name] = np.stack(vecs, axis=0)
    return db

face_db = build_employee_db()

# ------------------------- Utils -------------------------
ALLOWED_ACT = {"Working":"Working","Idle":"Idle","On Phone":"On Phone","Sleep":"Sleep","Away":"Away"}
ACT_COLORS = {"Working":(40,180,70), "Idle":(210,160,40), "On Phone":(60,120,240), "Sleep":(150,80,180), "Away":(120,120,120)}

@dataclass
class DetBox:
    cls: str
    conf: float
    box: tuple

def run_yolo(model, frame_bgr, conf_thres=0.28):
    res = model.predict(frame_bgr, verbose=False, conf=conf_thres)[0]
    names = res.names
    out = []
    if res.boxes is None:
        return out
    for b in res.boxes:
        c = int(b.cls.item())
        conf = float(b.conf.item() if b.conf is not None else 0.0)
        xyxy = tuple(map(int, b.xyxy[0].tolist()))
        out.append(DetBox(names[c], conf, xyxy))
    return out

def iou(a, b):
    xA, yA = max(a[0], b[0]), max(a[1], b[1])
    xB, yB = min(a[2], b[2]), min(a[3], b[3])
    inter = max(0, xB-xA) * max(0, yB-yA)
    if inter == 0: return 0.0
    areaA = (a[2]-a[0])*(a[3]-a[1]); areaB = (b[2]-b[0])*(b[3]-b[1])
    return inter / float(areaA + areaB - inter + 1e-6)

def center_speed(cur_box, prev_box):
    if prev_box is None or cur_box is None:
        return 0.0
    cx0 = (prev_box[0]+prev_box[2]) * 0.5; cy0 = (prev_box[1]+prev_box[3]) * 0.5
    cx1 = (cur_box[0]+cur_box[2]) * 0.5; cy1 = (cur_box[1]+cur_box[3]) * 0.5
    return math.hypot(cx1 - cx0, cy1 - cy0)

def phone_near_head(person_box, phone_boxes, face_boxes):
    """Phone considered 'near head' if overlaps any expanded face box or upper half of the person box."""
    px1, py1, px2, py2 = person_box
    head_h = int(py1 + 0.55 * (py2 - py1))     # a bit deeper into upper body for robustness
    head_box = (px1, py1, px2, head_h)
    exp_faces = []
    for f in face_boxes:
        fx1, fy1, fx2, fy2 = f
        w = fx2 - fx1; h = fy2 - fy1
        exp_faces.append((fx1 - w//5, fy1 - h//5, fx2 + w//5, fy2 + h//5))
    for ph in phone_boxes:
        if any(iou(ph.box, f) > 0.04 for f in exp_faces):   # slightly looser
            return True
        if iou(ph.box, head_box) > 0.06:
            return True
    return False

def recognize_from_db(face_emb, db, threshold=0.70):
    best_name, best = None, -1.0
    for nm, arr in db.items():
        sims = np.dot(arr, face_emb) / (np.linalg.norm(arr, axis=1) * (np.linalg.norm(face_emb)+1e-8))
        m = float(np.mean(sims))
        if m > best:
            best, best_name = m, nm
    if best >= threshold:
        return best_name, best
    return None, best

def fmt_secs_short(secs_float):
    return str(int(round(max(0.0, secs_float))))

# ------------------------- CSV helpers -------------------------
def save_events_csv(run_id: str):
    df = st.session_state.events
    if df.empty: return ""
    df = df[df["run_id"] == run_id].copy()
    if df.empty: return ""
    out_path = f"/tmp/events_{run_id}.csv"
    df.to_csv(out_path, index=False)
    st.session_state.csv_data["events"] = df.to_csv(index=False).encode("utf-8")  # cached bytes
    return out_path

def save_run_summary_csv(run_id: str, site_floor: str, camera_name: str):
    df = st.session_state.events
    if df.empty: return ""
    df = df[df["run_id"] == run_id].copy()
    if df.empty: return ""
    counts = df.groupby(["employee","activity"]).size().unstack(fill_value=0)
    for col in ["Working","On Phone","Idle","Away","Sleep"]:
        if col not in counts.columns: counts[col] = 0
    counts["total"] = counts.sum(axis=1)
    pct = counts[["Working","On Phone","Idle","Away","Sleep"]].div(counts["total"].replace(0,1), axis=0) * 100.0
    pct = pct.round(2)
    out = counts.join(pct.add_suffix(" %")).reset_index()
    out.insert(0,"camera", camera_name)
    out.insert(0,"site_floor", site_floor)
    out.insert(0,"run_id", run_id)
    out.insert(0,"date", date.today().isoformat())
    out_path = f"/tmp/run_summary_{run_id}.csv"
    out.to_csv(out_path, index=False)
    st.session_state.csv_data["summary"] = out.to_csv(index=False).encode("utf-8")  # cached bytes
    return out_path

def save_attendance_csv() -> str:
    rows = st.session_state.attendance_rows
    if not rows: return ""
    df = pd.DataFrame(rows)
    out_path = "/tmp/attendance_today.csv"
    df.to_csv(out_path, index=False)
    st.session_state.csv_data["attendance"] = df.to_csv(index=False).encode("utf-8")
    return out_path

def save_metrics_csv() -> str:
    rows = st.session_state.metric_rows
    if not rows: return ""
    df = pd.DataFrame(rows)
    out_path = "/tmp/metrics_today.csv"
    df.to_csv(out_path, index=False)
    st.session_state.csv_data["metrics"] = df.to_csv(index=False).encode("utf-8")
    return out_path

def save_anomalies_csv() -> str:
    rows = st.session_state.anomalies
    if not rows: return ""
    df = pd.DataFrame(rows)
    out_path = "/tmp/anomalies_today.csv"
    df.to_csv(out_path, index=False)
    st.session_state.csv_data["anomalies"] = df.to_csv(index=False).encode("utf-8")
    return out_path

# ------------------------- β€œSalesforce” shims (disabled) -------------------------
def push_events_batch(rows):
    # (SF disabled) no-op; rows still collected in events DF
    pass

def push_metric_batch(rows):
    if not rows: return
    st.session_state.metric_rows.extend(rows)

def push_attendance_once(kind: str, employee_name: str, ts_iso: str):
    st.session_state.attendance_rows.append({
        "Type": kind, "EmployeeName": employee_name, "InTime": ts_iso
    })

# ------------------------- Shared helpers -------------------------
def _overlay_label(img, x1, y1, text):
    """Draw a white box sized to text using cv2.getTextSize and put text on top."""
    font = cv2.FONT_HERSHEY_SIMPLEX
    scale = 0.45
    thickness = 1
    (tw, th), baseline = cv2.getTextSize(text, font, scale, thickness)
    pad_x, pad_y = 8, 6
    box_w = tw + 2*pad_x
    box_h = th + baseline + 2*pad_y
    top = max(0, y1 - (box_h + 4))
    cv2.rectangle(img, (x1, top), (x1 + box_w, top + box_h), (250,250,250), -1)
    cv2.putText(img, text, (x1 + pad_x, top + pad_y + th), font, scale, (30,30,30), thickness, cv2.LINE_AA)

def ensure_counter(name: str):
    if name not in st.session_state.emp_counters:
        st.session_state.emp_counters[name] = {"state": None, "working":0.0, "idle":0.0, "sleep":0.0}

def map_activity(act_raw: str) -> str:
    if act_raw in ALLOWED_ACT: return act_raw
    low = act_raw.lower()
    if low == "on phone": return "On Phone"
    if low == "sleeping": return "Sleep"
    if low == "away": return "Away"
    if low == "working": return "Working"
    return "Idle"

def _open_ticket_if_sleep(name: str, seconds_sleep: float):
    if seconds_sleep >= SLEEP_IDLE_SECONDS:
        open_exists = any((t["employee"] == name and t["status"] == "Open") for t in st.session_state.anomalies)
        if not open_exists:
            ticket_id = st.session_state.anomaly_counter
            st.session_state.anomaly_counter += 1
            st.session_state.anomalies.append({
                "ticket_id": ticket_id,
                "employee": name,
                "type": "Sleep",
                "observed_sec": int(seconds_sleep),
                "raised_by": "Admin",
                "raised_at": datetime.utcnow().isoformat() + "Z",
                "status": "Open",
                "history": [{"at": datetime.utcnow().isoformat()+"Z", "by": "System", "msg": f"Auto-detected Sleep >= {SLEEP_IDLE_SECONDS}s"}],
            })

# ------------------------- VIDEO writer helpers (silent mode) -------------------------
def _init_videowriter(out_path: str, fps: float, frame_size):
    # MP4 with H264 fallback; if cv2 can't use h264, mp4v is reliable.
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    return cv2.VideoWriter(out_path, fourcc, max(1.0, fps), frame_size)

def _split_file_if_needed(full_path: str) -> list:
    """If file size exceeds SPLIT_MAX_BYTES, split into parts and return the list of part paths.
       Splitting is done on raw bytes (not re-encoding). Many players can handle split sequences.
       We still prefer re-encode-per-part for safe playback, so we implement chunked copy and keep .mp4 parts.
    """
    parts = []
    size = os.path.getsize(full_path)
    if size <= SPLIT_MAX_BYTES:
        return [full_path]

    # chunk read and write to sequential part files
    base = Path(full_path).with_suffix("")  # remove .mp4
    with open(full_path, "rb") as fin:
        idx = 1
        while True:
            chunk = fin.read(SPLIT_MAX_BYTES)
            if not chunk:
                break
            part_path = f"{base}_part{idx}.mp4"
            with open(part_path, "wb") as fout:
                fout.write(chunk)
            parts.append(part_path)
            idx += 1
    # keep the original full as first (optional), but per request we will play parts sequentially instead
    return parts

# ------------------------- LIVE TAB (silent processing) -------------------------
with tabs[0]:
    st.title("🟒 Live")
    qp = st.query_params
    INSTANCE_NAME = str(qp.get("instance", [DEFAULT_INSTANCE])[0])
    FLOOR_TEXT    = str(qp.get("floor", [DEFAULT_FLOOR])[0])
    ZONE_TEXT     = str(qp.get("zone",  [DEFAULT_ZONE])[0])
    ZONE_TYPE     = "Work Area" if ZONE_TEXT.lower().strip() in ("work area","workarea","desk") else "Corridor"
    CAMERA_ID     = str(qp.get("camera", [DEFAULT_CAMERA_ID])[0])

    st.caption(f"Instance **{INSTANCE_NAME}** β€’ Floor **{FLOOR_TEXT}** β€’ Zone **{ZONE_TEXT}** β€’ Camera **{CAMERA_ID}**")
    st.caption("Loaded face DB for: " + (", ".join(sorted(face_db.keys())) if face_db else "β€” none β€”"))

    with st.expander("Advanced (ML)", expanded=False):
        conf_thres = st.slider("YOLO confidence", 0.1, 0.7, 0.28, 0.02, key="adv_conf")
        target_fps_fraction = st.slider("Process fraction of FPS (0.3–1.0)", 0.3, 1.0, 0.8, 0.05, key="adv_fps")
        idle_motion_px = st.slider("Idle motion threshold (px)", 2, 25, 8, key="adv_idle")
        st.caption("Frames are paced to video FPS; UI shows results only after processing.")

    up = st.file_uploader("Upload a video (MP4/AVI/MOV/MKV)", type=["mp4","avi","mov","mkv"], key="video_upl")
    process_btn = st.button("Process Video", disabled=False, key="process_btn")

    # the β€œPlay Processed Video” buttons appear after a successful run
    # and persist because we store paths in session_state.last_run_assets

    def process_video_silent(file_or_path, conf_thres: float, fps_fraction: float, idle_motion_px: int):
        st.session_state.current_run_id = datetime.utcnow().strftime("%Y%m%d-%H%M%S")
        run_id = st.session_state.current_run_id

        # reset per-run
        st.session_state.prev_boxes_by_name = {}
        st.session_state.act_votes = {}
        st.session_state.on_phone_start_ns = {}
        st.session_state.on_phone_accum_ns = {}
        st.session_state.emp_first_seen = set()
        st.session_state.last_seen_frame = {}
        st.session_state.idle_start_ts = {}
        st.session_state.did_checkout = set()
        st.session_state.last_run_assets = {"video_parts": [], "summary": ""}

        # write queue for events
        event_write_buffer = []

        # open
        if isinstance(file_or_path, (str, Path)):
            cap = cv2.VideoCapture(str(file_or_path))
        else:
            with tempfile.NamedTemporaryFile(delete=False, suffix=Path(file_or_path.name).suffix) as t:
                t.write(file_or_path.read())
                tmp_path = t.name
            cap = cv2.VideoCapture(tmp_path)

        src_fps = cap.get(cv2.CAP_PROP_FPS) or 25.0
        # keep real-time pacing but silent
        dt_target = 1.0 / (src_fps if src_fps>0 else 25.0)

        total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        prog = st.progress(0.0)

        # Prepare output video writer once we know frame size
        ret, first_frame = cap.read()
        if not ret:
            st.error("Could not read video.")
            return
        cap.set(cv2.CAP_PROP_POS_FRAMES, 0)  # rewind

        H, W = first_frame.shape[:2]
        # Ensure an output size that matches original width (or resized?) β€” keep original W,H
        out_name = f"/tmp/processed_output_{run_id}.mp4"
        vw = _init_videowriter(out_name, src_fps, (W, H))
        if not vw.isOpened():
            st.error("Failed to initialize video writer.")
            return

        frame_no = 0
        last_face_refresh = -999
        t_last = time.time()

        # SILENT LOOP: we do all inference and draw overlays on frames, but do NOT show them live.
        while cap.isOpened():
            ret, frame = cap.read()
            if not ret: break
            frame_no += 1

            # pace to FPS (silent)
            now = time.time()
            slip = dt_target - (now - t_last)
            if slip > 0: time.sleep(slip)
            t_last = time.time()

            infer = frame
            scale = 1.0
            if W > MAX_PREVIEW_W:
                # we KEEP output at original size; only inference could use downscale if desired
                # but to keep everything unchanged, we infer on original frame
                pass

            def inv_box(b):
                return b  # no scale change in silent path (kept consistent)

            # detections
            dets = run_yolo(model, infer, conf_thres=conf_thres)
            persons = [DetBox(d.cls, d.conf, inv_box(d.box)) for d in dets if d.cls == "person"]
            phones  = [DetBox(d.cls, d.conf, inv_box(d.box)) for d in dets if d.cls in ("cell phone","mobile phone","phone","cellphone")]

            # faces
            face_boxes = []
            if (frame_no - last_face_refresh) >= FACE_UPDATE_EVERY_N:
                faces_rel = _detect_faces_mediapipe(infer)
                face_boxes = [inv_box(b[:4]) for b in faces_rel]
                last_face_refresh = frame_no

            vis = frame.copy()

            # per person
            for idx, p in enumerate(persons):
                # pick a face inside the person box
                name = None
                for fb in face_boxes:
                    fx1,fy1,fx2,fy2 = fb
                    cx = (fx1+fx2)/2.0; cy=(fy1+fy2)/2.0
                    if p.box[0] <= cx <= p.box[2] and p.box[1] <= cy <= p.box[3]:
                        emb = _face_embed_gray(frame, fb)
                        if emb is not None:
                            nm, score = recognize_from_db(emb, face_db, threshold=0.70)
                            if nm: name = nm; break
                if not name:
                    name = f"Unknown #{idx+1}"

                # base activity by motion
                prev_box = st.session_state.prev_boxes_by_name.get(name)
                spd = center_speed(p.box, prev_box)
                st.session_state.prev_boxes_by_name[name] = p.box
                base_act = "Idle" if spd < float(idle_motion_px) else "Working"

                # phone persistence
                on_head = phone_near_head(p.box, phones, face_boxes)
                dq = st.session_state.act_votes.get(name)
                if dq is None:
                    dq = deque(maxlen=PHONE_PERSIST_N)
                    st.session_state.act_votes[name] = dq
                dq.append("phone" if on_head else "not")
                on_phone_stable = (dq.count("phone") >= PHONE_PERSIST_N)
                act = "On Phone" if on_phone_stable else base_act

                # counters
                ensure_counter(name)
                c = st.session_state.emp_counters[name]
                dt = dt_target
                if base_act == "Idle":
                    c["idle"] = c.get("idle",0.0) + dt
                    if name not in st.session_state.idle_start_ts:
                        st.session_state.idle_start_ts[name] = time.time()
                    else:
                        if (time.time() - st.session_state.idle_start_ts[name]) >= SLEEP_IDLE_SECONDS:
                            act = "Sleep"
                            _open_ticket_if_sleep(name, c.get("sleep", 0.0))
                else:
                    st.session_state.idle_start_ts[name] = time.time()

                if act == "Working":
                    c["working"] = c.get("working",0.0) + dt
                elif act == "Sleep":
                    c["sleep"] = c.get("sleep",0.0) + dt

                # phone nanosecond timer (continuous accumulation)
                now_ns = time.time_ns()
                if act == "On Phone":
                    if st.session_state.on_phone_start_ns.get(name) is None:
                        st.session_state.on_phone_start_ns[name] = now_ns
                    else:
                        delta = now_ns - int(st.session_state.on_phone_start_ns[name])
                        st.session_state.on_phone_accum_ns[name] = int(st.session_state.on_phone_accum_ns.get(name,0)) + delta
                        st.session_state.on_phone_start_ns[name] = now_ns
                else:
                    if st.session_state.on_phone_start_ns.get(name) is not None:
                        st.session_state.on_phone_accum_ns[name] = int(st.session_state.on_phone_accum_ns.get(name,0)) + (now_ns - int(st.session_state.on_phone_start_ns[name]))
                        st.session_state.on_phone_start_ns[name] = None

                # attendance once per run for known names
                if ("Unknown" not in name) and (name not in st.session_state.emp_first_seen):
                    st.session_state.emp_first_seen.add(name)
                    push_attendance_once("CheckIn", name, datetime.utcnow().isoformat()+"Z")

                # record last seen
                st.session_state.last_seen_frame[name] = frame_no

                # event cooldown emit
                key = f"{DEFAULT_CAMERA_ID}|{DEFAULT_ZONE}|{name}|{act}"
                can_emit = True
                last = st.session_state.last_emit_map
                last_t = last.get(key); now_t2 = time.time()
                if last_t and (now_t2 - last_t) < EVENT_COOLDOWN_SEC:
                    can_emit = False
                if can_emit:
                    st.session_state.events = pd.concat([st.session_state.events, pd.DataFrame([{
                        "id": f"evt-{len(st.session_state.events)+1}",
                        "ts": datetime.utcnow().isoformat()+"Z",
                        "camera": DEFAULT_CAMERA_ID,
                        "employee": name,
                        "activity": act if act in ALLOWED_ACT else "Idle",
                        "zone": DEFAULT_ZONE,
                        "confidence": int(p.conf*100),
                        "run_id": run_id
                    }])], ignore_index=True)

                    event_write_buffer.append({
                        "Activity__c": act if act in ALLOWED_ACT else "Idle",
                        "Timestamp__c": datetime.utcnow().isoformat()+"Z",
                        "Confidence__c": int(p.conf*100),
                        "Edge_Event_ID__c": str(uuid.uuid4()),
                        "EmployeeNameText__c": name if ("Unknown" not in name) else None,
                        "OnPhoneNs__c": int(st.session_state.on_phone_accum_ns.get(name, 0)),
                        "IdleUs__c": int(c.get("idle", 0.0) * 1_000_000),
                        "Working_Seconds__c": int(c.get("working", 0.0)),
                    })
                    last[key] = now_t2

                # draw overlay (white label covers full text using measured width)
                draw_act = act if act in ALLOWED_ACT else "Idle"
                color = ACT_COLORS.get(draw_act, (120,120,120))
                x1,y1,x2,y2 = p.box
                cv2.rectangle(vis,(x1,y1),(x2,y2), color, 2)

                wv = fmt_secs_short(c.get("working",0.0))
                iv = fmt_secs_short(c.get("idle",0.0))
                pn_ns = int(st.session_state.on_phone_accum_ns.get(name,0))
                if st.session_state.on_phone_start_ns.get(name) is not None:
                    pn_ns = int(st.session_state.on_phone_accum_ns.get(name,0))
                pv = fmt_secs_short(pn_ns/1e9)
                sv = fmt_secs_short(c.get("sleep",0.0))
                tag = f"{name}  [{draw_act}]  W|I|P|S = {wv}|{iv}|{pv}|{sv}"
                _overlay_label(vis, x1, y1, tag)

            # checkouts (not seen recently)
            gone = []
            for nm, lastf in list(st.session_state.last_seen_frame.items()):
                if (frame_no - lastf) >= CHECKOUT_MISS_FRAMES and nm in st.session_state.emp_first_seen and nm not in st.session_state.did_checkout:
                    st.session_state.did_checkout.add(nm)
                    push_attendance_once("CheckOut", nm, datetime.utcnow().isoformat()+"Z")
                    gone.append(nm)
            for nm in gone:
                st.session_state.last_seen_frame.pop(nm, None)

            # push events buffer periodically (SF disabled – just placeholder)
            if len(event_write_buffer) >= 30:
                push_events_batch(event_write_buffer[:30])
                del event_write_buffer[:30]

            # write frame to output video (silent)
            vw.write(vis)

            # progress
            if total > 0 and frame_no % 10 == 0:
                prog.progress(min(1.0, frame_no/total))

        cap.release()
        vw.release()
        prog.progress(1.0)

        # flush remaining events (SF disabled – just placeholder)
        if event_write_buffer:
            push_events_batch(event_write_buffer)

        # write metrics per employee (to local CSV list)
        metric_rows = []
        for emp, c in st.session_state.emp_counters.items():
            pn_ns = int(st.session_state.on_phone_accum_ns.get(emp,0))
            if st.session_state.on_phone_start_ns.get(emp) is not None:
                pn_ns = int(st.session_state.on_phone_accum_ns.get(emp,0))
            metric_rows.append({
                "As_Of_Date__c": date.today().isoformat(),
                "Working_Sec__c": int(c.get("working",0.0)),
                "Idle_Us__c": int(c.get("idle",0.0) * 1_000_000),
                "On_Phones__c": int(pn_ns/1e9),
                "Total_Events__c": int((st.session_state.events["employee"]==emp).sum()),
                "Window__c": "Session",
                "Unique_Key__c": f"{run_id}_{emp}",
                "Notes__c": json.dumps({"instance":DEFAULT_INSTANCE, "floor":DEFAULT_FLOOR, "zone":DEFAULT_ZONE, "camera":DEFAULT_CAMERA_ID, "run_id": run_id})[:131000]
            })
        push_metric_batch(metric_rows)

        # CSV artifacts (+ cache bytes)
        ev_csv = save_events_csv(run_id)
        sum_csv = save_run_summary_csv(run_id, f"Floor {DEFAULT_FLOOR}", DEFAULT_CAMERA_ID)
        att_csv = save_attendance_csv()
        met_csv = save_metrics_csv()
        an_csv  = save_anomalies_csv()

        # handle processed video (split if >200MB)
        parts = _split_file_if_needed(out_name)
        st.session_state.last_run_assets["video_parts"] = parts
        st.session_state.last_run_assets["summary"] = sum_csv

        st.success(f"Processed frames: {frame_no}")

    # Run
    if process_btn:
        try:
            if up is not None:
                process_video_silent(up, conf_thres=conf_thres, fps_fraction=target_fps_fraction, idle_motion_px=idle_motion_px)
            else:
                fallback = "VID_2.mp4"
                if not Path(fallback).exists():
                    st.error("No video uploaded and VID_2.mp4 not found in app root.")
                else:
                    process_video_silent(fallback, conf_thres=conf_thres, fps_fraction=target_fps_fraction, idle_motion_px=idle_motion_px)
        except Exception as e:
            st.error(f"Run failed: {e}")
            st.code(traceback.format_exc())

    # Post-run UI: Play Processed Video (no autoplay) + CSV downloads from cached bytes
    if st.session_state.last_run_assets["video_parts"]:
        st.subheader("Processed Video")
        # One play button per part, in order
        for i, vp in enumerate(st.session_state.last_run_assets["video_parts"], start=1):
            with st.container():
                if st.button(f"Play Processed Video (Part {i})", key=f"play_part_{i}"):
                    with open(vp, "rb") as vf:
                        st.video(vf.read())

    # Download buttons β€” use cached bytes so reruns never wipe results
    c1,c2,c3,c4,c5 = st.columns(5)
    if st.session_state.csv_data["events"]:
        c1.download_button("Download Events CSV", data=st.session_state.csv_data["events"], file_name="events.csv")
    if st.session_state.csv_data["summary"]:
        c2.download_button("Download Summary CSV", data=st.session_state.csv_data["summary"], file_name="summary.csv")
    if st.session_state.csv_data["attendance"]:
        c3.download_button("Download Attendance CSV", data=st.session_state.csv_data["attendance"], file_name="attendance.csv")
    if st.session_state.csv_data["metrics"]:
        c4.download_button("Download Metrics CSV", data=st.session_state.csv_data["metrics"], file_name="metrics.csv")
    if st.session_state.csv_data["anomalies"]:
        c5.download_button("Download Anomalies CSV", data=st.session_state.csv_data["anomalies"], file_name="anomalies.csv")

# ------------------------- EMPLOYEES TAB -------------------------
with tabs[1]:
    st.title("πŸ‘₯ Employees")
    st.caption("Live counters built from the current session (W | I | P | S). Search & export supported.")

    rows = []
    for name, c in sorted(st.session_state.emp_counters.items()):
        pn_ns = int(st.session_state.on_phone_accum_ns.get(name,0))
        if st.session_state.on_phone_start_ns.get(name) is not None:
            pn_ns = int(st.session_state.on_phone_accum_ns.get(name,0))
        rows.append({
            "Employee": name,
            "Working (sec)": int(c.get("working",0.0)),
            "Idle (sec)": int(c.get("idle",0.0)),
            "On Phone (sec)": int(pn_ns/1e9),
            "Sleep (sec)": int(c.get("sleep",0.0)),
            "At Desk?": "Yes" if name in st.session_state.last_seen_frame else "No",
            "Last Seen Frame": int(st.session_state.last_seen_frame.get(name, -1)),
        })
    emp_df = pd.DataFrame(rows) if rows else pd.DataFrame(columns=["Employee","Working (sec)","Idle (sec)","On Phone (sec)","Sleep (sec)","At Desk?","Last Seen Frame"])

    q = st.text_input("Search employees", "", key="emp_search")
    if q:
        emp_df = emp_df[emp_df["Employee"].str.contains(q, case=False, na=False)]
    st.dataframe(emp_df, use_container_width=True, hide_index=True)

    col1, _ = st.columns(2)
    if not emp_df.empty:
        csv_bytes = emp_df.to_csv(index=False).encode("utf-8")
        col1.download_button("Export Employees CSV", data=csv_bytes, file_name="employees_live.csv")

    st.markdown("**Face DB:** " + (", ".join(sorted(face_db.keys())) if face_db else "β€” none β€”"))

# ------------------------- REPORTS TAB -------------------------
with tabs[2]:
    st.title("πŸ“ˆ Reports")
    st.caption("Charts & exports created from real session events and attendance/metrics CSV queues.")

    ev = st.session_state.events.copy()

    c1,c2,c3,c4,c5 = st.columns(5)
    c1.metric("Present (unique people)", value=ev['employee'].nunique() if not ev.empty else 0)
    c2.metric("Events", value=len(ev))

    met_df = pd.DataFrame(st.session_state.metric_rows) if st.session_state.metric_rows else pd.DataFrame(columns=["Working_Sec__c","Idle_Us__c","On_Phones__c","Unique_Key__c"])
    if not met_df.empty:
        c3.metric("Avg Working (sec)", int(met_df["Working_Sec__c"].mean()))
        c4.metric("Avg Idle (sec)", int((met_df["Idle_Us__c"]/1_000_000).mean()))
        c5.metric("On Phone (sec avg)", int(met_df["On_Phones__c"].mean()))
    else:
        c3.metric("Avg Working (sec)", 0)
        c4.metric("Avg Idle (sec)", 0)
        c5.metric("On Phone (sec avg)", 0)

    st.divider()

    if not ev.empty:
        pivot = ev.groupby(["employee","activity"]).size().unstack(fill_value=0)
        st.subheader("By Employee Γ— Activity")
        st.dataframe(pivot, use_container_width=True)
        st.bar_chart(pivot, use_container_width=True)
    else:
        st.info("No events yet for this session.")

    st.divider()

    if not ev.empty:
        ev["minute"] = ev["ts"].str.slice(0,16)
        trend = ev.groupby("minute").size()
        st.subheader("Events β€” Trend")
        st.line_chart(trend, use_container_width=True)
    else:
        st.info("Trend will appear after you run a session.")

    st.divider()
    st.subheader("Report Builder")
    left, right = st.columns(2)
    start_date = left.date_input("Date range start", value=date.today())
    end_date   = right.date_input("Date range end", value=date.today())

    ecol1, ecol2, ecol3 = st.columns(3)
    all_emps = sorted(ev["employee"].unique().tolist()) if not ev.empty else []
    sel_emp = ecol1.selectbox("Employee", options=["All"]+all_emps, index=0)
    sel_act = ecol2.selectbox("Activity", options=["All"]+list(ALLOWED_ACT.keys()), index=0)
    sel_zone = ecol3.selectbox("Zone", options=["All"]+sorted(ev["zone"].unique()) if not ev.empty else ["All"], index=0)

    if not ev.empty:
        dfb = ev.copy()
        dfb["d"] = dfb["ts"].str.slice(0,10)
        dfb = dfb[(dfb["d"] >= start_date.isoformat()) & (dfb["d"] <= end_date.isoformat())]
        if sel_emp != "All": dfb = dfb[dfb["employee"] == sel_emp]
        if sel_act != "All": dfb = dfb[dfb["activity"] == sel_act]
        if sel_zone != "All": dfb = dfb[dfb["zone"] == sel_zone]
        dfb = dfb.drop(columns=["d"])
        st.dataframe(dfb, use_container_width=True, hide_index=True, height=280)
        st.download_button("Export CSV", data=dfb.to_csv(index=False).encode("utf-8"), file_name="report_builder.csv")
    else:
        st.info("Run a session to populate the report builder table.")

    st.divider()
    colA, colB, colC = st.columns(3)
    att_csv = save_attendance_csv()
    met_csv = save_metrics_csv()
    if st.session_state.csv_data["attendance"]:
        colA.download_button("Download Attendance CSV (today)", data=st.session_state.csv_data["attendance"], file_name="attendance_today.csv")
    if st.session_state.csv_data["metrics"]:
        colB.download_button("Download Metrics CSV (today)", data=st.session_state.csv_data["metrics"], file_name="metrics_today.csv")
    if not (st.session_state.csv_data["attendance"] or st.session_state.csv_data["metrics"]):
        st.info("Attendance/Metrics CSVs will show after a Live run.")

# ------------------------- ANOMALIES TAB -------------------------
with tabs[3]:
    st.title("🚩 Anomalies (Admin)")
    st.caption("Sleep cases auto-surface here. Admin can raise to employee, add notes, and mark resolved.")

    with st.expander("Raise manual anomaly", expanded=False):
        name_opt = sorted(list(st.session_state.emp_counters.keys()))
        emp = st.selectbox("Employee", options=name_opt if name_opt else ["β€”"], index=0 if name_opt else 0, key="an_raise_emp")
        typ = st.selectbox("Type", options=["Sleep"], index=0, key="an_raise_type")
        sec = st.number_input("Observed seconds", min_value=0, value=60, step=5, key="an_raise_sec")
        note = st.text_input("Note (optional)", "", key="an_raise_note")
        if st.button("Create Ticket", key="an_raise_btn", disabled=not name_opt):
            ticket_id = st.session_state.anomaly_counter
            st.session_state.anomaly_counter += 1
            st.session_state.anomalies.append({
                "ticket_id": ticket_id,
                "employee": emp,
                "type": typ,
                "observed_sec": int(sec),
                "raised_by": "Admin",
                "raised_at": datetime.utcnow().isoformat() + "Z",
                "status": "Open",
                "history": [{"at": datetime.utcnow().isoformat()+"Z", "by":"Admin", "msg": note or f"Manual {typ}"}],
            })
            st.success(f"Ticket #{ticket_id} created for {emp}")

    an_rows = st.session_state.anomalies
    if an_rows:
        df_an = pd.DataFrame([
            {
                "Ticket": r["ticket_id"],
                "Employee": r["employee"],
                "Type": r["type"],
                "Observed (sec)": r.get("observed_sec", 0),
                "Raised By": r["raised_by"],
                "Raised At": r["raised_at"],
                "Status": r["status"],
            } for r in an_rows
        ])
        st.dataframe(df_an, use_container_width=True, hide_index=True)

        st.subheader("Ticket actions")
        ids = [r["ticket_id"] for r in an_rows]
        sel = st.selectbox("Select ticket", options=ids, index=0)
        action_col1, action_col2 = st.columns(2)
        with action_col1:
            msg = st.text_input("Add note / employee response", "", key="an_msg")
            if st.button("Add Note", key="an_add_note"):
                for r in an_rows:
                    if r["ticket_id"] == sel:
                        r["history"].append({"at": datetime.utcnow().isoformat()+"Z", "by":"Admin", "msg": msg or "(no text)"})
                        st.success("Note added")
                        break
        with action_col2:
            if st.button("Mark Resolved", key="an_resolve"):
                for r in an_rows:
                    if r["ticket_id"] == sel:
                        r["status"] = "Resolved"
                        r["history"].append({"at": datetime.utcnow().isoformat()+"Z", "by":"Admin", "msg":"Resolved"})
                        st.success("Ticket resolved")
                        break

        # Export anomalies CSV from cached bytes (ensured on run)
        if st.session_state.csv_data["anomalies"]:
            st.download_button("Export Anomalies CSV", data=st.session_state.csv_data["anomalies"], file_name="anomalies_today.csv")

        st.divider()
        st.subheader("Ticket history")
        hsel = st.selectbox("Select ticket to view history", options=ids, index=0, key="an_hist_sel")
        for r in an_rows:
            if r["ticket_id"] == hsel:
                for entry in r["history"]:
                    st.write(f"- **{entry['at']}** β€’ {entry['by']}: {entry['msg']}")
                break
    else:
        st.info("No anomalies yet. Sleep events will auto-create tickets once thresholds are crossed.")