File size: 40,761 Bytes
1ce9686
 
429eb05
 
1ce9686
51c405f
1ce9686
429eb05
 
51c405f
1ce9686
 
 
 
51c405f
1ce9686
 
8a28ed2
51c405f
1ce9686
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d7e197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ce9686
 
 
 
51c405f
77f3077
 
1ce9686
 
 
 
 
 
 
 
77f3077
1ce9686
 
 
 
 
 
 
 
51c405f
1ce9686
 
 
 
 
51c405f
 
 
 
 
 
 
 
 
 
 
 
 
764ec4e
 
 
 
 
 
 
 
 
 
 
 
 
 
51c405f
 
 
780873f
51c405f
 
764ec4e
 
51c405f
 
 
 
 
 
 
780873f
 
51c405f
764ec4e
 
51c405f
 
 
764ec4e
51c405f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ce9686
 
 
 
 
51c405f
 
1ce9686
51c405f
 
 
 
1ce9686
51c405f
764ec4e
 
 
 
 
 
 
51c405f
 
 
 
 
 
 
 
 
 
 
c6de484
 
 
 
51c405f
 
 
 
 
77f3077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9247bd
51c405f
d9247bd
 
 
 
 
 
 
 
77f3077
 
d9247bd
 
 
 
 
 
 
 
764ec4e
 
d9247bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
764ec4e
 
 
d9247bd
 
764ec4e
d9247bd
 
 
 
 
 
 
764ec4e
1ce9686
 
140b1ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7227f58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59038fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a28ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc88453
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f27c023
 
 
 
 
 
 
 
 
 
 
63016cd
f27c023
 
 
db5904d
e160679
f27c023
 
 
 
 
 
 
 
 
e160679
f27c023
 
63016cd
429eb05
63016cd
 
 
 
 
 
 
 
 
e160679
 
 
 
db5904d
 
 
e160679
 
 
 
 
 
 
 
 
db5904d
 
 
e160679
db5904d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e160679
db5904d
e160679
 
f27c023
 
 
 
 
 
 
1ce9686
764ec4e
d9247bd
 
764ec4e
 
 
 
1ce9686
 
 
764ec4e
d9247bd
 
 
 
 
 
 
 
1017e67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b22579
 
 
 
 
 
d9247bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9600c00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9247bd
 
9600c00
d9247bd
 
1017e67
d9247bd
140b1ad
7227f58
59038fc
f27c023
9600c00
 
d9247bd
 
 
 
9600c00
d9247bd
 
9600c00
d9247bd
59038fc
d9247bd
1017e67
f27c023
140b1ad
59038fc
d9247bd
 
 
9600c00
 
 
 
f27c023
 
59038fc
 
7227f58
 
140b1ad
 
1017e67
 
d9247bd
 
 
 
 
 
59038fc
 
 
d9247bd
59038fc
9600c00
 
 
59038fc
 
 
9600c00
 
 
59038fc
 
 
 
 
 
 
d9247bd
59038fc
d9247bd
764ec4e
 
 
 
1ce9686
 
51c405f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ce9686
51c405f
 
 
1ce9686
 
 
51c405f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ce9686
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
import time
import os
import re
import logging
from contextlib import asynccontextmanager
from typing import Optional, Dict, Any, List

logger = logging.getLogger("LookupApp")

from fastapi import FastAPI, Query, HTTPException, Request, Header, Depends
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.gzip import GZipMiddleware
from fastapi.responses import HTMLResponse, FileResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel, Field

from engine import LookupEngine
from external_services import ServiceGateway
from auth import KeyManager, ADMIN_MASTER_KEY, INTERNAL_BOT_KEY

start_time = time.time()
_engine: Optional[LookupEngine] = None


def get_engine() -> LookupEngine:
    global _engine
    if _engine is None:
        _engine = LookupEngine()
        _engine.warm_up()
    return _engine


@asynccontextmanager
async def lifespan(app: FastAPI):
    get_engine()
    # Auto-launch Telegram Bot if TELEGRAM_BOT_TOKEN is provided in Space Secrets
    bot_token = os.environ.get("TELEGRAM_BOT_TOKEN", "").strip()
    if bot_token:
        import threading
        from bot import create_bot

        def _bot_worker():
            try:
                b = create_bot(bot_token)
                b_info = b.get_me()
                print(f"[+] Telegram Bot Daemon online as @{b_info.username}", flush=True)
                b.infinity_polling(timeout=20, long_polling_timeout=15)
            except Exception as e:
                print(f"[!] Telegram Bot Daemon error: {e}", flush=True)

        t = threading.Thread(target=_bot_worker, daemon=True)
        t.start()
        print("[+] Background Telegram Bot thread dispatched.", flush=True)

    yield


app = FastAPI(
    title="HyperLookup API & SaaS Engine",
    description="Enterprise-grade ultra-fast lookup API for 2.5B+ Indian Records (Phone & Aadhaar) and 65M+ Telegram Master Database with API Key Auth & Quota Controls.",
    version="2.2.0",
    lifespan=lifespan,
    docs_url="/docs",
    redoc_url="/redoc"
)

# Enable GZip compression
app.add_middleware(GZipMiddleware, minimum_size=1000)

# Enable CORS for all frontends, bots, and external apps
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Mount static directory if exists
static_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "static")
if os.path.exists(static_dir):
    app.mount("/static", StaticFiles(directory=static_dir), name="static")


# ---------------------------------------------------------
# Request Models
# ---------------------------------------------------------
class CreateKeyRequest(BaseModel):
    name: str = Field(..., example="Client Alpha")
    days_valid: Optional[int] = Field(30, description="Validity period in days. Pass 0 or null for lifetime.")
    rate_limit_per_min: Optional[int] = Field(60, description="Max allowed requests per minute.")
    total_credits: Optional[int] = Field(1000, description="Total number of search queries allowed. Pass -1 for unlimited.")


# ---------------------------------------------------------
# Authentication Dependency
# ---------------------------------------------------------
def get_client_ip(request: Request) -> str:
    """Extracts client IP considering Cloudflare, proxies, and standard headers."""
    cf_ip = request.headers.get("cf-connecting-ip")
    if cf_ip:
        return cf_ip.strip()
    forwarded = request.headers.get("x-forwarded-for")
    if forwarded:
        return forwarded.split(",")[0].strip()
    real_ip = request.headers.get("x-real-ip")
    if real_ip:
        return real_ip.strip()
    return request.client.host if request.client else "127.0.0.1"


def verify_api_key(
    request: Request,
    api_key_query: Optional[str] = Query(None, alias="api_key"),
    key_query: Optional[str] = Query(None, alias="key"),
    x_api_key: Optional[str] = Header(None, alias="X-API-Key"),
    authorization: Optional[str] = Header(None)
) -> Dict[str, Any]:
    """Extracts and verifies API key or applies IP-based daily free search with anti-DDoS."""
    token = None
    if x_api_key:
        token = x_api_key
    elif authorization and authorization.lower().startswith("bearer "):
        token = authorization.split(" ", 1)[1]
    elif api_key_query:
        token = api_key_query
    elif key_query:
        token = key_query

    client_ip = get_client_ip(request)
    is_valid, code, msg, meta = KeyManager.validate_request(token, client_ip)
    if not is_valid:
        raise HTTPException(status_code=code, detail=msg)

    return meta


def verify_admin_key(
    x_admin_key: Optional[str] = Header(None, alias="X-Admin-Key"),
    admin_key_query: Optional[str] = Query(None, alias="admin_key")
):
    """Requires the master admin key to access administrative operations."""
    key = x_admin_key or admin_key_query
    if key != ADMIN_MASTER_KEY:
        raise HTTPException(status_code=401, detail="Unauthorized. Master Admin Key required.")
    return key


# ---------------------------------------------------------
# Public Endpoints
# ---------------------------------------------------------
@app.get("/", response_class=HTMLResponse)
async def serve_home():
    index_file = os.path.join(static_dir, "index.html")
    if os.path.exists(index_file):
        return FileResponse(index_file)
    return HTMLResponse("<h1>HyperLookup Engine API is Online</h1><p>API documentation available at <a href='/docs'>/docs</a>.</p>")


@app.get("/api/health")
async def health_check():
    """Liveness probe for monitoring services."""
    return {"status": "ok", "timestamp": time.time()}


@app.get("/api/public/quota")
async def check_public_quota(request: Request):
    """Returns current daily free search quota for the caller IP."""
    client_ip = get_client_ip(request)
    return KeyManager.get_public_quota(client_ip)


@app.get("/api/stats")
async def get_system_stats():
    """Returns engine health, cache status, and database metrics."""
    eng = get_engine()
    uptime_sec = round(time.time() - start_time, 2)
    return {
        "status": "healthy",
        "uptime_seconds": uptime_sec,
        "engine_warmed": eng.is_warmed,
        "cache_entries": len(eng.cache),
        "database_info": {
            "total_records_indexed": "478,800,000+ Master DB Records",
            "active_services": 11,
            "engine": "DuckDB Vectorized Core",
            "cluster_state": "Operational"
        }
    }


# ---------------------------------------------------------
# Telegram Search Endpoints (Isolated & Ultra-Fast)
# ---------------------------------------------------------
@app.get("/api/telegram/lookup")
@app.get("/api/tg/lookup")
@app.get("/tg/lookup")
@app.get("/api/tg")
@app.get("/tg")
@app.get("/telegram")
async def search_telegram(
    user_id: Optional[str] = Query(None, description="Telegram User ID (e.g. 74252719)"),
    id: Optional[str] = Query(None, description="Alias for user_id"),
    q: Optional[str] = Query(None, description="Alias for user_id"),
    auth_meta: Dict[str, Any] = Depends(verify_api_key)
):
    target = user_id or id or q
    if not target:
        raise HTTPException(status_code=400, detail="Missing required query parameter: 'user_id'")

    res = get_engine().query_by_telegram(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/telegram/{user_id}")
@app.get("/api/tg/{user_id}")
@app.get("/tg/{user_id}")
async def lookup_telegram_path(user_id: str, auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    res = get_engine().query_by_telegram(user_id)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


# ---------------------------------------------------------
# Individual Master Database Search Endpoints
# ---------------------------------------------------------
@app.get("/api/truecaller/{phone_number}")
async def lookup_truecaller(phone_number: str, auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    """Search Truecaller 285M Master Database by 10-digit mobile."""
    res = get_engine().query_truecaller(phone_number)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/airtel/{phone_number}")
async def lookup_airtel(phone_number: str, auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    """Search Airtel India 73M Master Database by 10-digit mobile."""
    res = get_engine().query_airtel(phone_number)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/hdfc/{phone_number}")
async def lookup_hdfc(phone_number: str, auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    """Search HDFC Bank Master Database (CIBIL, Highmark, Loans) by 10-digit mobile."""
    res = get_engine().query_hdfc(phone_number)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/bsnl/{phone_number}")
async def lookup_bsnl(phone_number: str, auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    """Search BSNL 19M Master Database by 10-digit mobile."""
    res = get_engine().query_bsnl(phone_number)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/paytm/{phone_number}")
async def lookup_paytm(phone_number: str, auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    """Search Paytm India Master Database by 10-digit mobile."""
    res = get_engine().query_paytm(phone_number)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/flipkart/{phone_number}")
async def lookup_flipkart(phone_number: str, auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    """Search Flipkart India Master Database by 10-digit mobile."""
    res = get_engine().query_flipkart(phone_number)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/instagram/{query}")
async def lookup_instagram(query: str, auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    """Search Instagram 16.7M Master Database by Phone or Username."""
    res = get_engine().query_instagram(query)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/twitter/{query}")
async def lookup_twitter(query: str, auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    """Search Twitter / X 5.48M Master Database by User ID or Username."""
    res = get_engine().query_twitter(query)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/sbi/{query}")
@app.get("/api/sbi")
async def lookup_sbi(
    query: Optional[str] = None,
    q: Optional[str] = Query(None, description="Search query (phone, aadhaar, pan, name, email)"),
    phone: Optional[str] = Query(None, description="10-digit mobile number"),
    auth_meta: Dict[str, Any] = Depends(verify_api_key)
):
    """Search SBI POSP & Indian Banking Master Database by Phone, Aadhaar, PAN, Name, or Email."""
    target = query or q or phone
    if not target:
        raise HTTPException(status_code=400, detail="Missing required parameter: provide 'query', 'q', or 'phone'.")
    res = get_engine().query_sbi(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/ind/{query}")
@app.get("/api/ind")
async def lookup_ind(
    query: Optional[str] = None,
    q: Optional[str] = Query(None, description="Search query (phone, email, aadhaar, pan, name)"),
    phone: Optional[str] = Query(None, description="10-digit mobile number"),
    email: Optional[str] = Query(None, description="Email address"),
    auth_meta: Dict[str, Any] = Depends(verify_api_key)
):
    """Search IND Education, Admissions & KYC Master Database by Phone, Email, Aadhaar, PAN, or Name."""
    target = query or q or phone or email
    if not target:
        raise HTTPException(status_code=400, detail="Missing required parameter: provide 'query', 'q', 'phone', or 'email'.")
    res = get_engine().query_ind(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/upi/{query}")
@app.get("/api/upi")
async def lookup_upi(
    query: Optional[str] = None,
    q: Optional[str] = Query(None, description="UPI ID / VPA (e.g. 9826287975@paytm) or 10-digit mobile number"),
    vpa: Optional[str] = Query(None, description="UPI VPA address"),
    phone: Optional[str] = Query(None, description="10-digit Indian Mobile"),
    mode: Optional[str] = Query("full", description="Lookup mode: 'full' (360° KYC Dossier), 'upi_to_num' (VPA to Phone), or 'num_to_upi' (Phone to active UPI IDs)"),
    auth_meta: Dict[str, Any] = Depends(verify_api_key)
):
    """
    UPI & VPA 360° OSINT & KYC Resolver:
    - Mode 'full': Resolves UPI ID / Mobile to complete 360° cross-database KYC profile, 
                   active UPI handles, bank linkages, Aadhaar/PAN, and full address.
    - Mode 'upi_to_num': Extracts registered phone number, PSP application, acquiring bank & IFSC.
    - Mode 'num_to_upi': Generates and scores active UPI IDs across PhonePe, Google Pay, Paytm, BHIM, etc.
    """
    target = query or q or vpa or phone
    if not target:
        raise HTTPException(status_code=400, detail="Missing required parameter: provide 'query', 'q', 'vpa', or 'phone'.")

    res = get_engine().query_upi(target, mode=mode)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


# ---------------------------------------------------------
# Banking, Business, Vehicle & Network Intelligence APIs
# ---------------------------------------------------------
@app.get("/api/ifsc/{code}")
@app.get("/api/ifsc")
async def lookup_ifsc(code: Optional[str] = None, ifsc: Optional[str] = Query(None), q: Optional[str] = Query(None), auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    target = code or ifsc or q
    if not target:
        raise HTTPException(status_code=400, detail="Missing required parameter: provide 'code', 'ifsc', or 'q'.")
    res = ServiceGateway.get_ifsc(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/pincode/{pin}")
@app.get("/api/pincode")
async def lookup_pincode(pin: Optional[str] = None, pincode: Optional[str] = Query(None), q: Optional[str] = Query(None), auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    target = pin or pincode or q
    if not target:
        raise HTTPException(status_code=400, detail="Missing required parameter: provide 'pin', 'pincode', or 'q'.")
    res = ServiceGateway.get_pincode(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/ip/{ip_addr}")
@app.get("/api/ip")
async def lookup_ip(ip_addr: Optional[str] = None, ip: Optional[str] = Query(None), q: Optional[str] = Query(None), auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    target = ip_addr or ip or q
    if not target:
        raise HTTPException(status_code=400, detail="Missing required parameter: provide 'ip_addr', 'ip', or 'q'.")
    res = ServiceGateway.get_ip(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/vehicle/{vehicle_no}")
@app.get("/api/rc/{vehicle_no}")
@app.get("/api/rc")
@app.get("/api/vehicle")
async def lookup_vehicle(vehicle_no: Optional[str] = None, vehicle: Optional[str] = Query(None), rc: Optional[str] = Query(None), owner: Optional[str] = Query(None), q: Optional[str] = Query(None), auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    target = vehicle_no or vehicle or rc or owner or q
    if not target:
        raise HTTPException(status_code=400, detail="Missing required parameter: provide vehicle registration number.")
    res = ServiceGateway.get_vehicle_rc(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/challan/{vehicle_no}")
@app.get("/api/challan")
async def lookup_challan(vehicle_no: Optional[str] = None, vehicle: Optional[str] = Query(None), q: Optional[str] = Query(None), auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    target = vehicle_no or vehicle or q
    if not target:
        raise HTTPException(status_code=400, detail="Missing required parameter: provide vehicle registration number.")
    res = ServiceGateway.get_vehicle_challan(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/pan/{pan_no}")
@app.get("/api/pan")
async def lookup_pan(pan_no: Optional[str] = None, pan: Optional[str] = Query(None), q: Optional[str] = Query(None), auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    target = pan_no or pan or q
    if not target:
        raise HTTPException(status_code=400, detail="Missing required parameter: provide 'pan_no', 'pan', or 'q'.")
    res = ServiceGateway.get_pan_details(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/gst/{gstin}")
@app.get("/api/gst")
async def lookup_gst(gstin: Optional[str] = None, gst: Optional[str] = Query(None), q: Optional[str] = Query(None), auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    target = gstin or gst or q
    if not target:
        raise HTTPException(status_code=400, detail="Missing required parameter: provide 'gstin', 'gst', or 'q'.")
    res = ServiceGateway.get_gst_details(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/pantogst/{pan_no}")
@app.get("/api/pantogst")
async def lookup_pantogst(pan_no: Optional[str] = None, pan: Optional[str] = Query(None), q: Optional[str] = Query(None), auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    target = pan_no or pan or q
    if not target:
        raise HTTPException(status_code=400, detail="Missing required parameter: provide 'pan_no', 'pan', or 'q'.")
    res = ServiceGateway.get_pan_to_gst(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/vehtonum/{vehicle_no}")
@app.get("/api/vehtonum")
@app.get("/api/veh-to-num")
async def lookup_veh_to_num(vehicle_no: Optional[str] = None, vehicle: Optional[str] = Query(None), q: Optional[str] = Query(None), auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    target = vehicle_no or vehicle or q
    if not target:
        raise HTTPException(status_code=400, detail="Missing required parameter: provide vehicle registration number.")
    res = ServiceGateway.get_veh_to_num(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/ration/{query}")
@app.get("/api/ration")
@app.get("/api/adharration/{query}")
@app.get("/api/adharration")
async def lookup_ration(query: Optional[str] = None, num: Optional[str] = Query(None), q: Optional[str] = Query(None), auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    target = query or num or q
    if not target:
        raise HTTPException(status_code=400, detail="Missing required parameter: provide 12-digit Aadhaar UID or Ration Card number.")
    res = ServiceGateway.get_ration(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/bgmi/{uid}")
@app.get("/api/bgmi")
async def lookup_bgmi(uid: Optional[str] = None, q: Optional[str] = Query(None), auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    target = uid or q
    if not target:
        raise HTTPException(status_code=400, detail="Missing required parameter: provide BGMI numeric UID.")
    res = ServiceGateway.get_bgmi(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/github/{query}")
@app.get("/api/github")
async def lookup_github(query: Optional[str] = None, q: Optional[str] = Query(None), repo: Optional[str] = Query(None), auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    target = query or repo or q
    if not target:
        raise HTTPException(status_code=400, detail="Missing required parameter: provide GitHub username or repo.")
    res = ServiceGateway.get_github(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/youtube/{query}")
@app.get("/api/youtube")
async def lookup_youtube(query: Optional[str] = None, q: Optional[str] = Query(None), video: Optional[str] = Query(None), auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    target = query or video or q
    if not target:
        raise HTTPException(status_code=400, detail="Missing required parameter: provide YouTube URL or Video ID.")
    res = ServiceGateway.get_youtube_info(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/gas/{query}")
@app.get("/api/gas")
@app.get("/api/lpg/{query}")
@app.get("/api/lpg")
@app.get("/api/indane/{query}")
@app.get("/api/indane")
@app.get("/api/bharatgas/{query}")
@app.get("/api/bharatgas")
@app.get("/api/hpgas/{query}")
@app.get("/api/hpgas")
async def lookup_lpg_gas(
    request: Request,
    query: Optional[str] = None,
    num: Optional[str] = Query(None, description="10-digit mobile number or 17-digit LPG ID"),
    phone: Optional[str] = Query(None),
    lpg_id: Optional[str] = Query(None),
    consumer_no: Optional[str] = Query(None),
    provider: Optional[str] = Query(None, description="Gas provider: 'indane', 'bharat', or 'hp'"),
    q: Optional[str] = Query(None),
    auth_meta: Dict[str, Any] = Depends(verify_api_key)
):
    """
    Direct LPG Gas Consumer Intelligence:
    Resolves 10-digit mobile number or 17-digit LPG ID across all 3 Indian OMCs:
    Indane Gas (IOCL), Bharat Gas (BPCL), and HP Gas (HPCL).
    """
    target = query or num or phone or lpg_id or consumer_no or q
    if not target:
        raise HTTPException(status_code=400, detail="Missing required parameter: provide 10-digit mobile number or 17-digit LPG ID.")

    auto_prov = provider if (provider and isinstance(provider, str)) else None
    if not auto_prov:
        path_lower = request.url.path.lower()
        if "bharat" in path_lower:
            auto_prov = "bharat"
        elif "hp" in path_lower:
            auto_prov = "hp"
        elif "indane" in path_lower:
            auto_prov = "indane"

    clean_digits = re.sub(r'\D', '', str(target)).strip()
    user_kyc = None
    if len(clean_digits) == 10:
        try:
            eng = get_engine()
            phone_res = eng.query_by_phone(clean_digits)
            if isinstance(phone_res, dict):
                records = phone_res.get("records", [])
                if records and isinstance(records, list) and len(records) > 0:
                    rec = records[0]
                    user_kyc = {
                        "name": rec.get("name") or rec.get("fullName"),
                        "fathersName": rec.get("fathersName") or rec.get("father_name"),
                        "phoneNumber": rec.get("phoneNumber") or clean_digits,
                        "aadharNumber": rec.get("aadharNumber") or rec.get("aadhaar"),
                        "address": rec.get("address"),
                        "pincode": rec.get("pincode") or rec.get("pinCode"),
                        "district": rec.get("district"),
                        "state": rec.get("state")
                    }

            if not user_kyc or not user_kyc.get("name"):
                comb_res = eng.query_combined(clean_digits)
                if isinstance(comb_res, dict):
                    summary = comb_res.get("consolidated_summary", {})
                    p_name = summary.get("primary_name")
                    if not p_name and summary.get("names_found"):
                        p_name = summary["names_found"][0]
                    locs = summary.get("locations_identified", [])
                    loc = locs[0] if locs else ""
                    if p_name:
                        user_kyc = {
                            "name": p_name,
                            "fathersName": None,
                            "phoneNumber": clean_digits,
                            "aadharNumber": None,
                            "address": loc,
                            "pincode": None
                        }
        except Exception as e:
            logger.error(f"Gas consumer KYC resolution: {e}")

    res = ServiceGateway.get_lpg(target, provider=auto_prov, user_kyc=user_kyc)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res



@app.get("/api/phone/{phone_number}")
async def lookup_by_phone(phone_number: str, auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    """Search Telecom 2.5B Database by phone number."""
    res = get_engine().query_by_phone(phone_number)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/aadhar/{aadhar_number}")
async def lookup_by_aadhar(aadhar_number: str, auth_meta: Dict[str, Any] = Depends(verify_api_key)):
    """Search Aadhaar Database by 12-digit number."""
    res = get_engine().query_by_aadhar(aadhar_number)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/family/{query}")
@app.get("/api/family")
@app.get("/api/aadhar-family/{query}")
@app.get("/api/aadhar_family/{query}")
@app.get("/api/aadhar/family/{query}")
async def lookup_aadhar_family(
    query: Optional[str] = None,
    q: Optional[str] = Query(None, description="Aadhaar UID or Phone Number"),
    aadhar: Optional[str] = Query(None, description="12-digit Aadhaar UID"),
    phone: Optional[str] = Query(None, description="10-digit Phone Number"),
    auth_meta: Dict[str, Any] = Depends(verify_api_key)
):
    """
    Search Aadhaar Family & Household Linkage Database by 12-digit Aadhaar UID or 10-digit Mobile.
    Returns structured family hierarchy, co-residents, and shared household connections.
    """
    target = query or q or aadhar or phone
    if not target:
        raise HTTPException(status_code=400, detail="Missing required parameter: provide 'query', 'q', 'aadhar', or 'phone'.")

    res = get_engine().query_by_aadhar_family(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


# ---------------------------------------------------------
# Unified Combined Search Endpoints (All Databases in Parallel)
# ---------------------------------------------------------
@app.get("/api/combined/{phone_number}")
@app.get("/api/lookup/all")
@app.get("/api/lookup/combined")
async def combined_lookup(
    phone_number: Optional[str] = None,
    phone: Optional[str] = Query(None, description="10-digit Indian Mobile"),
    q: Optional[str] = Query(None, description="Search query"),
    auth_meta: Dict[str, Any] = Depends(verify_api_key)
):
    """
    Unified Combined Search: Queries Truecaller, Airtel, HDFC, BSNL, 
    Paytm, Flipkart, Instagram, and Telecom DB in parallel and returns 
    both consolidated profile intelligence and individual database records.
    """
    target = phone_number or phone or q
    if not target:
        raise HTTPException(status_code=400, detail="Missing parameter: provide 'phone_number', 'phone', or 'q'.")

    res = get_engine().query_combined(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


# ---------------------------------------------------------
# 🔥 360° Deep Intelligence Dossier Endpoints
# ---------------------------------------------------------
@app.get("/api/deep/{query}")
@app.get("/api/deep")
@app.get("/api/dossier/{query}")
@app.get("/api/dossier")
async def lookup_deep_dossier(
    query: Optional[str] = None,
    q: Optional[str] = Query(None, description="Any Indian identifier: Phone, Aadhaar, PAN, UPI, Vehicle, Telegram ID, Email, Name"),
    target_param: Optional[str] = Query(None, alias="target"),
    auth_meta: Dict[str, Any] = Depends(verify_api_key)
):
    """
    🔥 360° Deep Master Intelligence Dossier:
    Cross-queries all 16 Indian Master Databases in parallel and synthesizes 
    a comprehensive, cross-correlated intelligence profile with KYC consensus score.
    """
    target = query or q or target_param
    if not target:
        raise HTTPException(status_code=400, detail="Missing parameter: provide 'query', 'q', or 'target'.")
    res = get_engine().query_deep_dossier(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/muthoot/{query}")
@app.get("/api/muthoot")
async def lookup_muthoot(
    query: Optional[str] = None,
    q: Optional[str] = Query(None),
    phone: Optional[str] = Query(None),
    auth_meta: Dict[str, Any] = Depends(verify_api_key)
):
    """Search Muthoot Finance Gold Loans & Banking KYC Master Database."""
    target = query or q or phone
    if not target:
        raise HTTPException(status_code=400, detail="Missing parameter: provide 'query', 'q', or 'phone'.")
    res = get_engine().query_muthoot(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/olx/{query}")
@app.get("/api/olx")
async def lookup_olx(
    query: Optional[str] = None,
    q: Optional[str] = Query(None),
    phone: Optional[str] = Query(None),
    auth_meta: Dict[str, Any] = Depends(verify_api_key)
):
    """Search OLX India Auto Dealers & Classifieds Merchants Master Database."""
    target = query or q or phone
    if not target:
        raise HTTPException(status_code=400, detail="Missing parameter: provide 'query', 'q', or 'phone'.")
    res = get_engine().query_olx(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/icici/{query}")
@app.get("/api/icici")
async def lookup_icici(
    query: Optional[str] = None,
    q: Optional[str] = Query(None),
    model: Optional[str] = Query(None),
    auth_meta: Dict[str, Any] = Depends(verify_api_key)
):
    """Search ICICI Lombard Private Car & Vehicle Insurance Master Database."""
    target = query or q or model
    if not target:
        raise HTTPException(status_code=400, detail="Missing parameter: provide 'query', 'q', or 'model'.")
    res = get_engine().query_icici(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/xyron/{query}")
@app.get("/api/xyron")
async def lookup_xyron_master(
    query: Optional[str] = None,
    q: Optional[str] = Query(None),
    phone: Optional[str] = Query(None),
    auth_meta: Dict[str, Any] = Depends(verify_api_key)
):
    """Search Unified Xyron Intel Master (Combined Banking, Insurance, Muthoot, OLX)."""
    target = query or q or phone
    if not target:
        raise HTTPException(status_code=400, detail="Missing parameter: provide 'query', 'q', or 'phone'.")
    res = get_engine().query_xyron_master(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/sbi/{query}")
@app.get("/api/sbi")
async def lookup_sbi(
    query: Optional[str] = None,
    q: Optional[str] = Query(None),
    phone: Optional[str] = Query(None),
    auth_meta: Dict[str, Any] = Depends(verify_api_key)
):
    """Search SBI POSP & Indian Banking KYC Master Database."""
    target = query or q or phone
    if not target:
        raise HTTPException(status_code=400, detail="Missing parameter: provide 'query', 'q', or 'phone'.")
    res = get_engine().query_sbi(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/ind/{query}")
@app.get("/api/ind")
async def lookup_ind(
    query: Optional[str] = None,
    q: Optional[str] = Query(None),
    phone: Optional[str] = Query(None),
    auth_meta: Dict[str, Any] = Depends(verify_api_key)
):
    """Search IND Education Admissions & Student KYC Master Database."""
    target = query or q or phone
    if not target:
        raise HTTPException(status_code=400, detail="Missing parameter: provide 'query', 'q', or 'phone'.")
    res = get_engine().query_ind(target)
    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/lookup")
async def unified_lookup(
    q: Optional[str] = Query(None, description="Search query (phone, aadhar, telegram user_id, username, deep)"),
    phone: Optional[str] = Query(None, description="Direct 10-digit phone number"),
    aadhar: Optional[str] = Query(None, description="Direct 12-digit aadhar number"),
    family: Optional[str] = Query(None, description="Direct Aadhaar family query"),
    user_id: Optional[str] = Query(None, description="Direct Telegram User ID"),
    sbi: Optional[str] = Query(None, description="Direct SBI POSP query"),
    ind: Optional[str] = Query(None, description="Direct IND Admissions & KYC query"),
    upi: Optional[str] = Query(None, description="Direct UPI ID or VPA query"),
    gas: Optional[str] = Query(None, description="Direct LPG Gas cylinder or consumer query"),
    deep: Optional[str] = Query(None, description="Direct 360° Deep Dossier query"),
    mode: Optional[str] = Query(None, description="Search mode: 'deep' or 'fast'"),
    auth_meta: Dict[str, Any] = Depends(verify_api_key)
):
    """
    Smart Unified Lookup: Automatically detects input type.
    - Deep mode: Runs complete 360° Deep Intelligence Dossier across all 16 Master DBs.
    - 10-digit phone: Runs full parallel combined search across ALL databases.
    - 12-digit: Searches Aadhaar database.
    - 16/17/18-digit: Searches LPG Gas database.
    - Telegram ID: Searches Telegram 65M Master DB.
    - UPI ID / VPA: Resolves phone, PSP app, bank, and 360° KYC dossier.
    - Text/Username: Searches Instagram/Twitter.
    - Family: Discovers family linkages across Aadhaar & Telecom.
    - Gas: Resolves complete LPG cylinder details across Indane, Bharat Gas, HP Gas.
    - SBI: Searches SBI POSP & Indian Banking database.
    - IND: Searches IND Education, Admissions & KYC database.
    """
    eng = get_engine()

    if deep or mode == "deep":
        target = deep or q or phone or aadhar or upi or user_id
        res = eng.query_deep_dossier(target)
    elif gas:
        res = ServiceGateway.get_lpg(gas)
    elif upi:
        res = eng.query_upi_full_info(upi)
    elif ind:
        res = eng.query_ind(ind)
    elif sbi:
        res = eng.query_sbi(sbi)
    elif family:
        res = eng.query_by_aadhar_family(family)
    elif user_id:
        res = eng.query_by_telegram(user_id)
    elif aadhar:
        res = eng.query_by_aadhar(aadhar)
    elif phone:
        res = eng.query_combined(phone)
    elif q:
        q_str = str(q).strip()
        if "@" in q_str:
            res = eng.query_upi_full_info(q_str)
        else:
            clean = "".join(c for c in q_str if c.isdigit())
            if len(clean) in (16, 17, 18):
                res = ServiceGateway.get_lpg(clean)
            elif len(clean) == 12:
                res = eng.query_by_aadhar(clean)
            elif len(clean) == 10:
                res = eng.query_combined(clean)
            elif re.match(r"^[A-Za-z]{5}[0-9]{4}[A-Za-z]$", q_str):
                res = ServiceGateway.get_pan_details(q_str.upper())
            elif 5 <= len(clean) <= 15:
                tg_res = eng.query_by_telegram(clean)
                if tg_res.get("status") == "success":
                    res = tg_res
                else:
                    res = eng.query_combined(clean)
            else:
                res = eng.query_instagram(q_str)
    else:
        raise HTTPException(status_code=400, detail="Missing parameter: provide 'q', 'phone', 'aadhar', 'upi', or 'user_id'.")

    if isinstance(res, dict):
        res["tier"] = auth_meta.get("tier", "free")
        res["remaining_credits"] = auth_meta.get("remaining", -1)
    return res


@app.get("/api/key/status")
async def check_key_status(api_key: str = Query(..., description="API Key to inspect")):
    status = KeyManager.get_key_status(api_key)
    if not status:
        raise HTTPException(status_code=404, detail="API Key not found.")
    return status


# ---------------------------------------------------------
# Admin Key Management Endpoints (Master Admin Only)
# ---------------------------------------------------------
@app.post("/api/admin/keys/create")
async def create_api_key(req: CreateKeyRequest, _admin: str = Depends(verify_admin_key)):
    """Generate a new client API key with custom expiry, credits, and rate limit."""
    new_key = KeyManager.create_key(
        name=req.name,
        days_valid=req.days_valid,
        rate_limit_per_min=req.rate_limit_per_min or 60,
        total_credits=req.total_credits if req.total_credits is not None else 1000
    )
    return {
        "success": True,
        "message": "API key generated successfully.",
        "key_data": new_key
    }


@app.get("/api/admin/keys")
async def list_all_keys(_admin: str = Depends(verify_admin_key)):
    """List all issued API keys with remaining credits and usage stats."""
    return {
        "success": True,
        "keys": KeyManager.list_keys()
    }


@app.delete("/api/admin/keys/{target_key}")
async def delete_api_key(target_key: str, _admin: str = Depends(verify_admin_key)):
    """Permanently delete or revoke an API key."""
    deleted = KeyManager.delete_key(target_key)
    if not deleted:
        raise HTTPException(status_code=404, detail="Key not found.")
    return {"success": True, "message": "API key deleted successfully."}


if __name__ == "__main__":
    import uvicorn
    uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)