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d4ec
6fc43b2
 
 
 
 
 
e8a2d3b
6fc43b2
 
e8a2d3b
cafd4ec
 
6fc43b2
cafd4ec
d12d95f
cafd4ec
d12d95f
 
cafd4ec
6fc43b2
cafd4ec
 
6fc43b2
d12d95f
 
 
 
 
 
 
6fc43b2
 
 
d12d95f
6fc43b2
 
cafd4ec
6fc43b2
 
 
 
d12d95f
e8a2d3b
6fc43b2
 
 
 
e8a2d3b
cafd4ec
6fc43b2
 
e8a2d3b
cafd4ec
6fc43b2
 
 
e8a2d3b
cafd4ec
 
6fc43b2
 
 
cafd4ec
 
6fc43b2
e8a2d3b
 
 
cafd4ec
6fc43b2
 
 
 
 
 
e8a2d3b
6fc43b2
 
e8a2d3b
cafd4ec
 
d12d95f
 
cafd4ec
d12d95f
 
6fc43b2
 
 
 
 
d12d95f
 
 
 
 
 
cafd4ec
6fc43b2
cafd4ec
d12d95f
cafd4ec
6fc43b2
cafd4ec
6fc43b2
 
 
 
cafd4ec
6fc43b2
cafd4ec
6fc43b2
 
 
e8a2d3b
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
import asyncio
import os
import shutil
import uuid
import re
import time
import json
import traceback
import inflect
from datetime import datetime, timezone
from urllib.parse import urlparse
from typing import List
from contextlib import asynccontextmanager
from collections import OrderedDict

from fastapi import FastAPI, UploadFile, File, Form, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
import cloudinary
import cloudinary.uploader
import cloudinary.api
from pinecone import Pinecone, ServerlessSpec

# ── loguru for pretty local console logs (optional dep) ──────────
try:
    from loguru import logger as _loguru
    _loguru.remove()
    _loguru.add(
        lambda msg: print(msg, end=""),
        format="<green>{time:HH:mm:ss}</green> | <level>{level:<8}</level> | {message}",
        level="DEBUG", colorize=True,
    )
    _log_fn = _loguru.log
except ImportError:
    import logging as _logging
    _logging.basicConfig(level=_logging.INFO)
    _stdlib = _logging.getLogger("el")
    def _log_fn(level, msg): _stdlib.log(getattr(_logging, level, 20), msg)

# ── Deferred imports ─────────────────────────────────────────────
ai = None
p  = inflect.engine()

MAX_CONCURRENT_INFERENCES = int(os.getenv("MAX_CONCURRENT_INFERENCES", "1"))  # InsightFace ONNX is NOT thread-safe
_inference_sem: asyncio.Semaphore
_pinecone_pool = OrderedDict()
_POOL_MAX      = 64
IDX_FACES      = "enterprise-faces"
MAX_FILES_PER_UPLOAD = 20  # cap to prevent memory corruption on large batches
IDX_OBJECTS    = "enterprise-objects"

# ════════════════════════════════════════════════════════════════
#  SUPABASE LOGGING  —  async, fire-and-forget, never crashes API
#  HF Space Secrets needed:
#    SUPABASE_URL          → https://xxxx.supabase.co
#    SUPABASE_SERVICE_KEY  → your Supabase service_role key (not anon!)
# ════════════════════════════════════════════════════════════════
SUPABASE_URL         = os.getenv("SUPABASE_URL",         "")
SUPABASE_SERVICE_KEY = os.getenv("SUPABASE_SERVICE_KEY", "")

async def _supabase_log_push(level: str, event: str, data: dict):
    """Fire-and-forget insert into Supabase app_logs table."""
    if not (SUPABASE_URL and SUPABASE_SERVICE_KEY):
        return
    try:
        import aiohttp
        row = {
            "level":       level.upper(),
            "event":       event,
            "user_id":     str(data.get("user_id", "anonymous")),
            "ip":          str(data.get("ip", "")),
            "mode":        str(data.get("mode", "")),
            "page":        str(data.get("page", "")),
            "duration_ms": int(data["duration_ms"]) if "duration_ms" in data else None,
            "error":       str(data["error"]) if "error" in data else None,
            "data":        data,
        }
        headers = {
            "Content-Type":  "application/json",
            "apikey":        SUPABASE_SERVICE_KEY,
            "Authorization": f"Bearer {SUPABASE_SERVICE_KEY}",
            "Prefer":        "return=minimal",
        }
        async with aiohttp.ClientSession() as s:
            async with s.post(
                f"{SUPABASE_URL}/rest/v1/app_logs",
                json=row, headers=headers,
                timeout=aiohttp.ClientTimeout(total=5)
            ) as r:
                if r.status not in (200, 201):
                    body = await r.text()
                    _log_fn("WARNING", f"Supabase log insert failed {r.status}: {body[:200]}")
    except Exception as exc:
        _log_fn("DEBUG", f"Supabase log push skipped: {exc}")

def log(level: str, event: str, **data):
    """
    Log to console + Supabase app_logs table (background task).
    Usage: log("INFO", "upload.complete", user_id="x", files=3, duration_ms=340)
    """
    clean = {k: v for k, v in data.items()}
    _log_fn(level.upper(), f"[{event}] {json.dumps(clean, default=str)}")
    try:
        loop = asyncio.get_event_loop()
        if loop.is_running():
            asyncio.create_task(_supabase_log_push(level, event, data))
    except Exception:
        pass


# ════════════════════════════════════════════════════════════════
#  HELPERS
# ════════════════════════════════════════════════════════════════
def get_ip(request: Request) -> str:
    xff = request.headers.get("X-Forwarded-For", "")
    return xff.split(",")[0].strip() if xff else getattr(request.client, "host", "unknown")

def is_guest(key: str) -> bool:
    default = os.getenv("DEFAULT_PINECONE_KEY", "")
    return bool(default) and key.strip() == default.strip()

def _get_pinecone(api_key: str) -> Pinecone:
    if api_key not in _pinecone_pool:
        if len(_pinecone_pool) >= _POOL_MAX:
            _pinecone_pool.popitem(last=False)
        _pinecone_pool[api_key] = Pinecone(api_key=api_key)
    _pinecone_pool.move_to_end(api_key)
    return _pinecone_pool[api_key]

def _cld_upload(tmp_path, folder, creds):
    return cloudinary.uploader.upload(tmp_path, folder=folder,
        api_key=creds["api_key"], api_secret=creds["api_secret"], cloud_name=creds["cloud_name"])

def _cld_ping(creds):
    return cloudinary.api.ping(
        api_key=creds["api_key"], api_secret=creds["api_secret"], cloud_name=creds["cloud_name"])

def _cld_root_folders(creds):
    return cloudinary.api.root_folders(
        api_key=creds["api_key"], api_secret=creds["api_secret"], cloud_name=creds["cloud_name"])

def get_cloudinary_creds(env_url: str) -> dict:
    if not env_url: return {}
    parsed = urlparse(env_url)
    return {"api_key": parsed.username, "api_secret": parsed.password, "cloud_name": parsed.hostname}

def standardize_category_name(name: str) -> str:
    clean = re.sub(r'\s+', '_', name.strip().lower())
    clean = re.sub(r'[^\w]', '', clean)
    return p.singular_noun(clean) or clean

def sanitize_filename(filename: str) -> str:
    return re.sub(r'[^\w.\-]', '', re.sub(r'\s+', '_', filename))

DEFAULT_PC_KEY  = os.getenv("DEFAULT_PINECONE_KEY",  "")
DEFAULT_CLD_URL = os.getenv("DEFAULT_CLOUDINARY_URL", "")

def _is_default_key(key: str, default: str) -> bool:
    return bool(default) and key.strip() == default.strip()


# ════════════════════════════════════════════════════════════════
#  APP STARTUP / SHUTDOWN
# ════════════════════════════════════════════════════════════════
@asynccontextmanager
async def lifespan(app: FastAPI):
    global ai, _inference_sem
    from src.models import AIModelManager
    log("INFO", "server.startup", message="Loading AI models...")
    loop = asyncio.get_event_loop()
    ai   = await loop.run_in_executor(None, AIModelManager)
    _inference_sem = asyncio.Semaphore(MAX_CONCURRENT_INFERENCES)
    log("INFO", "server.ready", message="All models loaded. API ready.")
    yield
    log("INFO", "server.shutdown", message="API shutting down.")

app = FastAPI(lifespan=lifespan)
app.add_middleware(CORSMiddleware,
    allow_origins=["*"], allow_credentials=True,
    allow_methods=["*"], allow_headers=["*"])
os.makedirs("temp_uploads", exist_ok=True)


# ════════════════════════════════════════════════════════════════
#  FRONTEND EVENT LOG  —  React calls this for client-side events
#  Logs: page visits, tab switches, mode toggles, search/upload
#  initiated, settings changes, errors caught in UI
# ════════════════════════════════════════════════════════════════
@app.post("/api/log")
async def frontend_log(
    request:  Request,
    event:    str = Form(...),           # e.g. "page.visit", "search.initiated"
    user_id:  str = Form(""),
    page:     str = Form(""),
    metadata: str = Form("{}"),          # JSON string with extra context
):
    ip = get_ip(request)
    try:
        meta = json.loads(metadata) if metadata else {}
    except Exception:
        meta = {}
    log("INFO", f"frontend.{event}",
        user_id = user_id or "anonymous",
        page    = page,
        ip      = ip,
        ua      = request.headers.get("User-Agent", "")[:120],
        **meta,
    )
    return {"ok": True}


# ════════════════════════════════════════════════════════════════
#  1. VERIFY KEYS & AUTO-BUILD INDEXES
# ════════════════════════════════════════════════════════════════
@app.post("/api/verify-keys")
async def verify_keys(
    request:        Request,
    pinecone_key:   str = Form(""),
    cloudinary_url: str = Form(""),
    user_id:        str = Form(""),
):
    ip    = get_ip(request)
    mode  = "guest" if is_guest(pinecone_key) else "personal"
    start = time.perf_counter()

    log("INFO", "settings.verify_keys.start",
        user_id=user_id or "anonymous", mode=mode, ip=ip)

    if cloudinary_url:
        try:
            creds_v = get_cloudinary_creds(cloudinary_url)
            if not creds_v.get("cloud_name"): raise ValueError("bad url")
            await asyncio.to_thread(_cld_ping, creds_v)
        except HTTPException: raise
        except Exception as e:
            log("ERROR", "settings.verify_keys.cloudinary_fail",
                user_id=user_id or "anonymous", ip=ip, error=str(e),
                duration_ms=round((time.perf_counter()-start)*1000))
            raise HTTPException(400, "Invalid Cloudinary Environment URL.")

    indexes_created = []
    if pinecone_key:
        try:
            pc       = _get_pinecone(pinecone_key)
            existing = {idx.name for idx in await asyncio.to_thread(pc.list_indexes)}
            tasks    = []
            if IDX_OBJECTS not in existing:
                tasks.append(asyncio.to_thread(pc.create_index, name=IDX_OBJECTS, dimension=1536,
                    metric="cosine", spec=ServerlessSpec(cloud="aws", region="us-east-1")))
                indexes_created.append(IDX_OBJECTS)
            if IDX_FACES not in existing:
                tasks.append(asyncio.to_thread(pc.create_index, name=IDX_FACES, dimension=512,
                    metric="cosine", spec=ServerlessSpec(cloud="aws", region="us-east-1")))
                indexes_created.append(IDX_FACES)
            if tasks: await asyncio.gather(*tasks)
        except Exception as e:
            err = str(e)
            clean = ("Invalid Pinecone API Key. Please check your key and try again."
                     if "401" in err or "unauthorized" in err.lower()
                     else f"Pinecone Error: {err}")
            log("ERROR", "settings.verify_keys.pinecone_fail",
                user_id=user_id or "anonymous", ip=ip, error=clean,
                duration_ms=round((time.perf_counter()-start)*1000))
            raise HTTPException(400, clean)

    log("INFO", "settings.verify_keys.success",
        user_id=user_id or "anonymous", mode=mode, ip=ip,
        indexes_created=indexes_created,
        duration_ms=round((time.perf_counter()-start)*1000))
    return {"message": "Keys verified and indexes ready!"}


# ════════════════════════════════════════════════════════════════
#  2. UPLOAD
# ════════════════════════════════════════════════════════════════
@app.post("/api/upload")
async def upload_new_images(
    request:             Request,
    files:               List[UploadFile] = File(...),
    folder_name:         str  = Form(...),
    detect_faces:        bool = Form(True),
    user_pinecone_key:   str  = Form(""),
    user_cloudinary_url: str  = Form(""),
    user_id:             str  = Form(""),
):
    ip             = get_ip(request)
    start          = time.perf_counter()
    actual_pc_key  = user_pinecone_key   or os.getenv("DEFAULT_PINECONE_KEY",  "")
    actual_cld_url = user_cloudinary_url or os.getenv("DEFAULT_CLOUDINARY_URL", "")
    mode           = "guest" if is_guest(actual_pc_key) else "personal"

    log("INFO", "upload.start",
        user_id=user_id or "anonymous", ip=ip, mode=mode,
        folder=folder_name, file_count=len(files),
        file_names=[f.filename for f in files][:10],
        detect_faces=detect_faces)

    if not actual_pc_key or not actual_cld_url:
        log("ERROR", "upload.missing_keys", user_id=user_id or "anonymous", ip=ip, mode=mode)
        raise HTTPException(400, "API Keys are missing. If you are a guest, the server is missing its DEFAULT_ secrets in Hugging Face.")

    if len(files) > MAX_FILES_PER_UPLOAD:
        raise HTTPException(400, f"Maximum {MAX_FILES_PER_UPLOAD} files per upload. Please split into smaller batches.")

    folder = standardize_category_name(folder_name)
    creds  = get_cloudinary_creds(actual_cld_url)
    if not creds.get("cloud_name"):
        log("ERROR", "upload.bad_cloudinary_url", user_id=user_id or "anonymous", ip=ip)
        raise HTTPException(400, "Invalid Cloudinary URL format.")

    pc               = _get_pinecone(actual_pc_key)
    idx_obj          = pc.Index(IDX_OBJECTS)
    idx_face         = pc.Index(IDX_FACES)
    uploaded_urls    = []
    face_vec_total   = 0
    object_vec_total = 0

    for file in files:
        tmp_path   = f"temp_uploads/{uuid.uuid4().hex}_{sanitize_filename(file.filename)}"
        file_start = time.perf_counter()
        try:
            with open(tmp_path, "wb") as buf:
                shutil.copyfileobj(file.file, buf)

            res       = await asyncio.to_thread(_cld_upload, tmp_path, folder, creds)
            image_url = res["secure_url"]
            uploaded_urls.append(image_url)

            async with _inference_sem:
                vectors = await ai.process_image_async(tmp_path, is_query=False, detect_faces=detect_faces)

            face_upserts  = []
            object_upserts = []

            for v in vectors:
                vec_list = v["vector"].tolist() if hasattr(v["vector"], "tolist") else v["vector"]
                if v["type"] == "face":
                    # ── FACE STORE: ArcFace 512-D embedding
                    # Metadata includes original image_url so we can
                    # retrieve the full image after a face match
                    face_upserts.append({
                        "id":     str(uuid.uuid4()),
                        "values": vec_list,
                        "metadata": {
                            "image_url": image_url,   # original full image
                            "url":       image_url,   # alias for compatibility
                            "folder":    folder,
                            "face_idx":  v.get("face_idx", 0),
                            "bbox":      str(v.get("bbox", [])),
                            "face_crop": v.get("face_crop", ""),  # base64 thumb for UI
                            "det_score": v.get("det_score", 1.0),
                        }
                    })
                else:
                    # ── OBJECT STORE: SigLIP+DINOv2 1536-D fused embedding
                    # Always stores full image — includes all crops + full image
                    object_upserts.append({
                        "id":     str(uuid.uuid4()),
                        "values": vec_list,
                        "metadata": {
                            "image_url": image_url,
                            "url":       image_url,
                            "folder":    folder,
                        }
                    })

            # Always upsert to BOTH indexes:
            # - face index gets face embeddings (if any faces detected)
            # - object index ALWAYS gets full image embedding
            face_vec_total   += len(face_upserts)
            object_vec_total += len(object_upserts)

            upsert_tasks = []
            if face_upserts:
                upsert_tasks.append(asyncio.to_thread(idx_face.upsert, vectors=face_upserts))
            if object_upserts:
                upsert_tasks.append(asyncio.to_thread(idx_obj.upsert, vectors=object_upserts))
            if upsert_tasks:
                await asyncio.gather(*upsert_tasks)

            log("INFO", "upload.file.success",
                user_id=user_id or "anonymous", ip=ip, mode=mode,
                filename=file.filename, folder=folder, image_url=image_url,
                face_vectors=len(face_upserts), obj_vectors=len(object_upserts),
                detect_faces=detect_faces,
                duration_ms=round((time.perf_counter()-file_start)*1000))

        except Exception as e:
            log("ERROR", "upload.file.error",
                user_id=user_id or "anonymous", ip=ip, mode=mode,
                filename=file.filename, folder=folder, error=str(e),
                traceback=traceback.format_exc()[-800:],
                duration_ms=round((time.perf_counter()-file_start)*1000))
            err = str(e)
            if "not found" in err.lower() or "404" in err:
                raise HTTPException(404, "Pinecone index not found. Please go to Settings and click 'Verify & Save' to recreate your indexes.")
            raise HTTPException(500, f"Upload processing failed: {err}")
        finally:
            if os.path.exists(tmp_path): os.remove(tmp_path)

    log("INFO", "upload.complete",
        user_id=user_id or "anonymous", ip=ip, mode=mode,
        folder=folder, files_uploaded=len(uploaded_urls),
        face_vectors=face_vec_total, object_vectors=object_vec_total,
        detect_faces=detect_faces,
        duration_ms=round((time.perf_counter()-start)*1000))

    return {"message": "Done!", "urls": uploaded_urls}


# ════════════════════════════════════════════════════════════════
#  3. SEARCH
# ════════════════════════════════════════════════════════════════
@app.post("/api/search")
async def search_database(
    request:             Request,
    file:                UploadFile = File(...),
    detect_faces:        bool = Form(True),
    user_pinecone_key:   str  = Form(""),
    user_cloudinary_url: str  = Form(""),
    user_id:             str  = Form(""),
):
    ip            = get_ip(request)
    start         = time.perf_counter()
    actual_pc_key = user_pinecone_key or os.getenv("DEFAULT_PINECONE_KEY", "")
    mode          = "guest" if is_guest(actual_pc_key) else "personal"

    log("INFO", "search.start",
        user_id=user_id or "anonymous", ip=ip, mode=mode,
        filename=file.filename, detect_faces=detect_faces)

    if not actual_pc_key:
        log("ERROR", "search.missing_keys", user_id=user_id or "anonymous", ip=ip, mode=mode)
        raise HTTPException(400, "Pinecone Key is missing.")

    tmp_path = f"temp_uploads/query_{uuid.uuid4().hex}_{sanitize_filename(file.filename)}"
    try:
        with open(tmp_path, "wb") as buf:
            shutil.copyfileobj(file.file, buf)

        async with _inference_sem:
            vectors = await ai.process_image_async(tmp_path, is_query=True, detect_faces=detect_faces)

        inference_ms = round((time.perf_counter() - start) * 1000)
        lanes_used   = list({v["type"] for v in vectors})
        log("INFO", "search.inference_done",
            user_id=user_id or "anonymous", ip=ip, mode=mode,
            vector_count=len(vectors), lanes=lanes_used, inference_ms=inference_ms)

        pc       = _get_pinecone(actual_pc_key)
        idx_obj  = pc.Index(IDX_OBJECTS)
        idx_face = pc.Index(IDX_FACES)

        async def _query_one(vec_dict: dict):
            vec_list   = vec_dict["vector"].tolist() if hasattr(vec_dict["vector"], "tolist") else vec_dict["vector"]
            target_idx = idx_face if vec_dict["type"] == "face" else idx_obj
            try:
                res = await asyncio.to_thread(target_idx.query, vector=vec_list, top_k=10, include_metadata=True)
            except Exception as e:
                if "404" in str(e):
                    raise HTTPException(404, "Pinecone Index not found. Please log in and click 'Verify Keys' in Settings.")
                raise e
            out = []
            for match in res.get("matches", []):
                score   = match["score"]
                is_face = vec_dict["type"] == "face"
                if is_face:
                    if score < 0.35: continue
                    ui_score = min(0.99, 0.75 + ((score - 0.35) / 0.65) * 0.24)
                else:
                    if score < 0.45: continue
                    ui_score = score
                out.append({"url":   match["metadata"].get("url") or match["metadata"].get("image_url", ""),
                            "score": round(ui_score, 4),
                            "caption": "👤 Verified Identity" if is_face else match["metadata"].get("folder", "🎯 Object Match")})
            return out

        # ── V3: separate face vectors from object vectors ────────
        face_vectors   = [v for v in vectors if v["type"] == "face"]
        object_vectors = [v for v in vectors if v["type"] == "object"]

        if detect_faces and face_vectors:
            # ══════════════════════════════════════════════════════
            # FACE MODE — Two-step retrieval (from cloud_db.py):
            # Step 1: Query enterprise-faces index with ArcFace vector
            #         → get matched image_urls + face_crop thumbnails
            # Step 2: De-duplicate by image_url, rank by face score
            #         → return full original images grouped per face
            # ══════════════════════════════════════════════════════
            async def _query_face_group(face_vec: dict) -> dict:
                vec_list = face_vec["vector"].tolist() if hasattr(face_vec["vector"], "tolist") else face_vec["vector"]
                try:
                    # Query enterprise-FACES index (512-D ArcFace)
                    res = await asyncio.to_thread(
                        idx_face.query,
                        vector=vec_list,
                        top_k=20,  # fetch more, filter below
                        include_metadata=True,
                    )
                except Exception as e:
                    if "404" in str(e):
                        raise HTTPException(404, "Pinecone index not found. Go to Settings → Verify & Save.")
                    raise e

                matches    = []
                seen_urls  = set()

                for match in res.get("matches", []):
                    raw_score = match["score"]

                    # ArcFace cosine similarity threshold
                    # 0.35 = same person (from cloud_db.py RAW_THRESHOLD)
                    if raw_score < 0.35:
                        continue

                    # Get the ORIGINAL full image URL (not face crop)
                    # Both "url" and "image_url" keys stored for compatibility
                    image_url_match = (
                        match["metadata"].get("url") or
                        match["metadata"].get("image_url", "")
                    )
                    if not image_url_match or image_url_match in seen_urls:
                        continue
                    seen_urls.add(image_url_match)

                    # Remap raw ArcFace score (0.35–1.0) → UI score (75%–99%)
                    # Matches cloud_db.py remapping exactly
                    ui_score = 0.75 + ((raw_score - 0.35) / (1.0 - 0.35)) * 0.24
                    ui_score = min(0.99, ui_score)

                    matches.append({
                        "url":        image_url_match,      # full original image
                        "score":      round(ui_score, 4),
                        "raw_score":  round(raw_score, 4),
                        "face_crop":  match["metadata"].get("face_crop", ""),  # matched face thumb
                        "folder":     match["metadata"].get("folder", ""),
                        "caption":    "👤 Verified Identity",
                    })

                return {
                    "query_face_idx":  face_vec.get("face_idx", 0),
                    "query_face_crop": face_vec.get("face_crop", ""),
                    "det_score":       face_vec.get("det_score", 1.0),
                    "matches":         sorted(matches, key=lambda x: x["score"], reverse=True)[:10],
                }

            face_groups = await asyncio.gather(*[_query_face_group(fv) for fv in face_vectors])
            face_groups = [g for g in face_groups if g["matches"]]

            duration_ms   = round((time.perf_counter() - start) * 1000)
            total_matches = sum(len(g["matches"]) for g in face_groups)
            log("INFO", "search.complete",
                user_id=user_id or "anonymous", ip=ip, mode=mode,
                lanes=["face"], detect_faces=detect_faces,
                face_groups=len(face_groups), results_count=total_matches,
                top_score=face_groups[0]["matches"][0]["score"] if face_groups and face_groups[0]["matches"] else 0,
                duration_ms=duration_ms)

            return {
                "mode":        "face",
                "face_groups": list(face_groups),
                "results":     [],  # empty — face mode returns groups only
            }

        else:
            # ── OBJECT MODE: original flat results ──────────────
            nested     = await asyncio.gather(*[_query_one(v) for v in vectors])
            all_results = [r for sub in nested for r in sub]
            seen = {}
            for r in all_results:
                url = r["url"]
                if url not in seen or r["score"] > seen[url]["score"]:
                    seen[url] = r
            final = sorted(seen.values(), key=lambda x: x["score"], reverse=True)[:10]

            duration_ms = round((time.perf_counter() - start) * 1000)
            log("INFO", "search.complete",
                user_id=user_id or "anonymous", ip=ip, mode=mode,
                lanes=lanes_used, detect_faces=detect_faces,
                results_count=len(final), top_score=final[0]["score"] if final else 0,
                duration_ms=duration_ms)

            return {"mode": "object", "results": final, "face_groups": []}

    except HTTPException: raise
    except Exception as e:
        log("ERROR", "search.error",
            user_id=user_id or "anonymous", ip=ip, mode=mode,
            error=str(e), traceback=traceback.format_exc()[-800:],
            duration_ms=round((time.perf_counter()-start)*1000))
        raise HTTPException(500, str(e))
    finally:
        if os.path.exists(tmp_path): os.remove(tmp_path)


# ════════════════════════════════════════════════════════════════
#  4. CATEGORIES
# ════════════════════════════════════════════════════════════════
@app.post("/api/categories")
async def get_categories(
    request:             Request,
    user_cloudinary_url: str = Form(""),
    user_id:             str = Form(""),
):
    ip         = get_ip(request)
    actual_url = user_cloudinary_url or os.getenv("DEFAULT_CLOUDINARY_URL", "")
    if not actual_url: return {"categories": []}
    try:
        creds = get_cloudinary_creds(actual_url)
        if not creds.get("cloud_name"): return {"categories": []}
        result     = await asyncio.to_thread(_cld_root_folders, creds)
        categories = [f["name"] for f in result.get("folders", [])]
        log("INFO", "categories.fetched",
            user_id=user_id or "anonymous", ip=ip, category_count=len(categories))
        return {"categories": categories}
    except Exception as e:
        log("ERROR", "categories.error", user_id=user_id or "anonymous", ip=ip, error=str(e))
        return {"categories": []}


@app.get("/")
async def root():
    return {"status": "ok"}


@app.get("/api/health")
async def health():
    return {"status": "ok", "timestamp": datetime.now(timezone.utc).isoformat()}


# ════════════════════════════════════════════════════════════════
#  5. LIST FOLDER IMAGES
# ════════════════════════════════════════════════════════════════
def _cld_list_folder_images(folder: str, creds: dict, next_cursor: str = None):
    kwargs = dict(type="upload", prefix=f"{folder}/", max_results=500,
        api_key=creds["api_key"], api_secret=creds["api_secret"], cloud_name=creds["cloud_name"])
    if next_cursor: kwargs["next_cursor"] = next_cursor
    return cloudinary.api.resources(**kwargs)

@app.post("/api/cloudinary/folder-images")
async def list_folder_images(
    request:             Request,
    user_cloudinary_url: str = Form(""),
    folder_name:         str = Form(...),
    user_id:             str = Form(""),
):
    ip         = get_ip(request)
    actual_url = user_cloudinary_url or os.getenv("DEFAULT_CLOUDINARY_URL", "")
    creds      = get_cloudinary_creds(actual_url)
    if not creds.get("cloud_name"): raise HTTPException(400, "Invalid Cloudinary URL.")
    images, next_cursor = [], None
    while True:
        result      = await asyncio.to_thread(_cld_list_folder_images, folder_name, creds, next_cursor)
        for r in result.get("resources", []):
            images.append({"url": r["secure_url"], "public_id": r["public_id"]})
        next_cursor = result.get("next_cursor")
        if not next_cursor: break
    log("INFO", "explorer.folder_opened",
        user_id=user_id or "anonymous", ip=ip,
        folder_name=folder_name, image_count=len(images))
    return {"images": images, "count": len(images)}


# ════════════════════════════════════════════════════════════════
#  6. DELETE SINGLE IMAGE
# ════════════════════════════════════════════════════════════════
def url_to_public_id(image_url: str, cloud_name: str) -> str:
    try:
        path       = urlparse(image_url).path
        parts      = path.split("/")
        upload_idx = parts.index("upload")
        after      = parts[upload_idx + 1:]
        if after and after[0].startswith("v") and after[0][1:].isdigit(): after = after[1:]
        return "/".join(after).rsplit(".", 1)[0]
    except Exception: return ""

def _cld_delete_resource(public_id: str, creds: dict):
    return cloudinary.uploader.destroy(public_id,
        api_key=creds["api_key"], api_secret=creds["api_secret"], cloud_name=creds["cloud_name"])

@app.post("/api/delete-image")
async def delete_image(
    request:             Request,
    user_pinecone_key:   str = Form(""),
    user_cloudinary_url: str = Form(""),
    image_url:           str = Form(""),
    public_id:           str = Form(""),
    user_id:             str = Form(""),
):
    ip             = get_ip(request)
    actual_pc_key  = user_pinecone_key   or os.getenv("DEFAULT_PINECONE_KEY",  "")
    actual_cld_url = user_cloudinary_url or os.getenv("DEFAULT_CLOUDINARY_URL", "")
    creds          = get_cloudinary_creds(actual_cld_url)
    if not creds.get("cloud_name"): raise HTTPException(400, "Invalid Cloudinary URL.")
    pid = public_id or url_to_public_id(image_url, creds["cloud_name"])
    if not pid: raise HTTPException(400, "Could not determine public_id.")
    await asyncio.to_thread(_cld_delete_resource, pid, creds)
    if actual_pc_key and image_url:
        try:
            pc = _get_pinecone(actual_pc_key)
            for idx_name in [IDX_OBJECTS, IDX_FACES]:
                await asyncio.to_thread(pc.Index(idx_name).delete, filter={"url": {"$eq": image_url}})
        except Exception as e:
            _log_fn("WARNING", f"Pinecone delete warning: {e}")
    log("INFO", "explorer.image_deleted",
        user_id=user_id or "anonymous", ip=ip,
        image_url=image_url, public_id=pid)
    return {"message": "Image deleted successfully."}


# ════════════════════════════════════════════════════════════════
#  7. DELETE ENTIRE FOLDER
# ════════════════════════════════════════════════════════════════
def _cld_delete_folder(folder: str, creds: dict):
    return cloudinary.api.delete_resources_by_prefix(f"{folder}/",
        api_key=creds["api_key"], api_secret=creds["api_secret"], cloud_name=creds["cloud_name"])

def _cld_remove_folder(folder: str, creds: dict):
    try:
        return cloudinary.api.delete_folder(folder,
            api_key=creds["api_key"], api_secret=creds["api_secret"], cloud_name=creds["cloud_name"])
    except Exception: pass

def _cld_delete_all_paginated(creds: dict):
    """Delete ALL Cloudinary resources in batches of 100 until none left."""
    deleted = 0
    while True:
        try:
            res = cloudinary.api.resources(
                type="upload", max_results=100,
                api_key=creds["api_key"], api_secret=creds["api_secret"],
                cloud_name=creds["cloud_name"],
            )
            resources = res.get("resources", [])
            if not resources:
                break
            public_ids = [r["public_id"] for r in resources]
            cloudinary.api.delete_resources(
                public_ids,
                api_key=creds["api_key"], api_secret=creds["api_secret"],
                cloud_name=creds["cloud_name"],
            )
            deleted += len(public_ids)
            print(f"🗑️  Deleted {deleted} resources so far...")
            if not res.get("next_cursor"):
                break
        except Exception as e:
            print(f"Cloudinary batch delete error: {e}")
            break
    return deleted

@app.post("/api/delete-folder")
async def delete_folder(
    request:             Request,
    user_pinecone_key:   str = Form(""),
    user_cloudinary_url: str = Form(""),
    folder_name:         str = Form(...),
    user_id:             str = Form(""),
):
    ip             = get_ip(request)
    actual_pc_key  = user_pinecone_key   or os.getenv("DEFAULT_PINECONE_KEY",  "")
    actual_cld_url = user_cloudinary_url or os.getenv("DEFAULT_CLOUDINARY_URL", "")
    creds          = get_cloudinary_creds(actual_cld_url)
    if not creds.get("cloud_name"): raise HTTPException(400, "Invalid Cloudinary URL.")
    all_images, next_cursor = [], None
    while True:
        result      = await asyncio.to_thread(_cld_list_folder_images, folder_name, creds, next_cursor)
        all_images.extend(result.get("resources", []))
        next_cursor = result.get("next_cursor")
        if not next_cursor: break
    await asyncio.to_thread(_cld_delete_folder, folder_name, creds)
    await asyncio.to_thread(_cld_remove_folder, folder_name, creds)
    if actual_pc_key:
        try:
            pc = _get_pinecone(actual_pc_key)
            for idx_name in [IDX_OBJECTS, IDX_FACES]:
                idx = pc.Index(idx_name)
                try:
                    await asyncio.to_thread(idx.delete, filter={"folder": {"$eq": folder_name}})
                except Exception:
                    for img in all_images:
                        try:
                            if img.get("secure_url"):
                                await asyncio.to_thread(idx.delete, filter={"url": {"$eq": img["secure_url"]}})
                        except Exception: pass
        except Exception as e:
            _log_fn("WARNING", f"Pinecone folder delete warning: {e}")
    log("INFO", "explorer.folder_deleted",
        user_id=user_id or "anonymous", ip=ip,
        folder_name=folder_name, deleted_count=len(all_images))
    return {"message": f"Folder '{folder_name}' and all its contents deleted.", "deleted_count": len(all_images)}


# ════════════════════════════════════════════════════════════════
#  8. RESET DATABASE  ⚠️  DESTRUCTIVE — triple-logged
# ════════════════════════════════════════════════════════════════
@app.post("/api/reset-database")
async def reset_database(
    request:             Request,
    user_pinecone_key:   str = Form(""),
    user_cloudinary_url: str = Form(""),
    user_id:             str = Form(""),
):
    ip    = get_ip(request)
    start = time.perf_counter()

    log("WARNING", "danger.reset_database.attempt",
        user_id=user_id or "anonymous", ip=ip)

    if _is_default_key(user_pinecone_key, DEFAULT_PC_KEY) or _is_default_key(user_cloudinary_url, DEFAULT_CLD_URL):
        log("WARNING", "danger.reset_database.blocked_shared_db",
            user_id=user_id or "anonymous", ip=ip)
        raise HTTPException(403, "Reset is not allowed on the shared demo database.")

    creds = get_cloudinary_creds(user_cloudinary_url)
    if not creds.get("cloud_name"): raise HTTPException(400, "Invalid Cloudinary URL.")

    # ── Cloudinary: paginated delete ALL resources then folders ────
    try:
        deleted = await asyncio.to_thread(_cld_delete_all_paginated, creds)
        _log_fn("INFO", f"Cloudinary: deleted {deleted} resources")
    except Exception as e:
        _log_fn("WARNING", f"Cloudinary wipe: {e}")

    try:
        folders_res = await asyncio.to_thread(_cld_root_folders, creds)
        # Delete all folders in parallel
        folder_tasks = [
            asyncio.to_thread(_cld_remove_folder, f["name"], creds)
            for f in folders_res.get("folders", [])
        ]
        if folder_tasks:
            await asyncio.gather(*folder_tasks, return_exceptions=True)
    except Exception as e:
        _log_fn("WARNING", f"Cloudinary folder cleanup: {e}")

    # ── Pinecone: delete both indexes + recreate ─────────────────
    try:
        pc       = _get_pinecone(user_pinecone_key)
        existing = {idx.name for idx in await asyncio.to_thread(pc.list_indexes)}
        tasks    = []
        if IDX_OBJECTS in existing: tasks.append(asyncio.to_thread(pc.delete_index, IDX_OBJECTS))
        if IDX_FACES   in existing: tasks.append(asyncio.to_thread(pc.delete_index, IDX_FACES))
        if tasks: await asyncio.gather(*tasks)
        await asyncio.sleep(3)  # wait for Pinecone to fully delete
        await asyncio.gather(
            asyncio.to_thread(pc.create_index, name=IDX_OBJECTS, dimension=1536, metric="cosine",
                spec=ServerlessSpec(cloud="aws", region="us-east-1")),
            asyncio.to_thread(pc.create_index, name=IDX_FACES,   dimension=512,  metric="cosine",
                spec=ServerlessSpec(cloud="aws", region="us-east-1")),
        )
    except Exception as e:
        log("ERROR", "danger.reset_database.pinecone_error",
            user_id=user_id or "anonymous", ip=ip, error=str(e))
        raise HTTPException(500, f"Pinecone reset error: {e}")

    log("WARNING", "danger.reset_database.complete",
        user_id=user_id or "anonymous", ip=ip,
        duration_ms=round((time.perf_counter()-start)*1000))
    return {"message": "Database reset complete. All data wiped and indexes recreated."}


# ════════════════════════════════════════════════════════════════
#  9. DELETE ACCOUNT  ⚠️  DESTRUCTIVE — triple-logged
# ════════════════════════════════════════════════════════════════
@app.post("/api/delete-account")
async def delete_account(
    request:             Request,
    user_pinecone_key:   str = Form(""),
    user_cloudinary_url: str = Form(""),
    user_id:             str = Form(""),
):
    ip    = get_ip(request)
    start = time.perf_counter()

    log("WARNING", "danger.delete_account.attempt",
        user_id=user_id or "anonymous", ip=ip)

    if _is_default_key(user_pinecone_key, DEFAULT_PC_KEY) or _is_default_key(user_cloudinary_url, DEFAULT_CLD_URL):
        log("WARNING", "danger.delete_account.blocked_shared_db",
            user_id=user_id or "anonymous", ip=ip)
        raise HTTPException(403, "Account deletion is not allowed on the shared demo database.")

    creds = get_cloudinary_creds(user_cloudinary_url)

    # ── Cloudinary: paginated delete ALL resources then folders ────
    try:
        deleted = await asyncio.to_thread(_cld_delete_all_paginated, creds)
        _log_fn("INFO", f"Account delete Cloudinary: {deleted} resources removed")
    except Exception as e:
        _log_fn("WARNING", f"Account delete Cloudinary: {e}")

    try:
        folders_res = await asyncio.to_thread(_cld_root_folders, creds)
        folder_tasks = [
            asyncio.to_thread(_cld_remove_folder, f["name"], creds)
            for f in folders_res.get("folders", [])
        ]
        if folder_tasks:
            await asyncio.gather(*folder_tasks, return_exceptions=True)
    except Exception as e:
        _log_fn("WARNING", f"Account delete Cloudinary folders: {e}")

    # ── Pinecone: delete both indexes ────────────────────────────
    try:
        pc       = _get_pinecone(user_pinecone_key)
        existing = {idx.name for idx in await asyncio.to_thread(pc.list_indexes)}
        tasks    = []
        if IDX_OBJECTS in existing: tasks.append(asyncio.to_thread(pc.delete_index, IDX_OBJECTS))
        if IDX_FACES   in existing: tasks.append(asyncio.to_thread(pc.delete_index, IDX_FACES))
        if tasks: await asyncio.gather(*tasks)
    except Exception as e:
        _log_fn("WARNING", f"Account delete Pinecone: {e}")

    log("WARNING", "danger.delete_account.complete",
        user_id=user_id or "anonymous", ip=ip,
        duration_ms=round((time.perf_counter()-start)*1000))
    return {"message": "Account data deleted. Sign out initiated."}