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

app = Flask(__name__)
CORS(app)

HF_API_KEY = os.environ.get("HF_API_KEY", "")
HF_TEXT_MODEL_URL = "https://router.huggingface.co/hf-inference/models/Hello-SimpleAI/chatgpt-detector-roberta"
HF_TEXT_MODEL_URL_2 = "https://router.huggingface.co/hf-inference/models/openai-community/roberta-base-openai-detector"
HF_IMAGE_MODEL_URL = "https://router.huggingface.co/hf-inference/models/prithivMLmods/deepfake-detector-model-v1"
HF_AUDIO_MODEL_URL = "https://router.huggingface.co/hf-inference/models/Gustking/wav2vec2-large-xlsr-deepfake-audio-classification"
HF_CAPTION_MODEL_URL = "https://router.huggingface.co/hf-inference/models/Salesforce/blip-image-captioning-large"

SUPABASE_URL = os.environ.get("SUPABASE_URL", "")
SUPABASE_KEY = os.environ.get("SUPABASE_KEY", "")

TELEGRAM_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN", "")
TELEGRAM_API_URL = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}"


# ── AI TEXT DETECTION MODEL (desklib, loaded locally inside the Space) ──
# desklib/ai-text-detector-v1.01 β€” a deberta-v3-large detector that scores
# near the top of the RAID benchmark. Much better on modern AI than the old
# 2023 model. It uses a custom class (not the standard pipeline). Bigger model,
# so first startup downloads ~1.7GB and each analysis takes a few seconds.
TEXT_MODEL_NAME = "desklib/ai-text-detector-v1.01"


class DesklibAIDetectionModel(PreTrainedModel):
    config_class = AutoConfig

    def __init__(self, config):
        super().__init__(config)
        self.model = AutoModel.from_config(config)
        self.classifier = nn.Linear(config.hidden_size, 1)
        self.post_init()
        # NOTE: we deliberately do NOT call init_weights()/post_init() here.
        # from_pretrained() loads the trained weights (including the classifier)
        # anyway, and calling init on some transformers versions throws
        # AttributeError: 'all_tied_weights_keys'.

    def forward(self, input_ids, attention_mask=None, labels=None):
        outputs = self.model(input_ids, attention_mask=attention_mask)
        last_hidden_state = outputs[0]
        mask = attention_mask.unsqueeze(-1).expand(last_hidden_state.size()).float()
        summed = torch.sum(last_hidden_state * mask, dim=1)
        counts = torch.clamp(mask.sum(dim=1), min=1e-9)
        pooled = summed / counts
        logits = self.classifier(pooled)
        return {"logits": logits}


try:
    _tokenizer = AutoTokenizer.from_pretrained(TEXT_MODEL_NAME)
    _text_model = DesklibAIDetectionModel.from_pretrained(TEXT_MODEL_NAME)
    _text_model.eval()
    print(f"βœ… Text model loaded: {TEXT_MODEL_NAME}")
except Exception as e:
    _tokenizer = None
    _text_model = None
    print(f"❌ Text model failed to load: {type(e).__name__}: {e}")


# ── DEEPFAKE IMAGE MODEL (loaded locally inside the Space) ──
# dima806/deepfake_vs_real_image_detection β€” a Vision Transformer for Real-vs-Fake
# classification. Loaded locally (standard image-classification pipeline) instead
# of the HF Inference API, which is what caused the old 500 errors.
# NOTE: this model is trained on an older dataset and works best on human FACE
# photos; it is less reliable on brand-new AI-generated images.
IMAGE_MODEL_NAME = "dima806/deepfake_vs_real_image_detection"
try:
    image_classifier = pipeline("image-classification", model=IMAGE_MODEL_NAME)
    print(f"βœ… Image model loaded: {IMAGE_MODEL_NAME}")
except Exception as e:
    image_classifier = None
    print(f"❌ Image model failed to load: {type(e).__name__}: {e}")


def _ai_prob_for_chunk(chunk):
    enc = _tokenizer(chunk, truncation=True, max_length=512, return_tensors="pt")
    with torch.no_grad():
        out = _text_model(input_ids=enc["input_ids"], attention_mask=enc["attention_mask"])
        prob = torch.sigmoid(out["logits"]).item()  # probability that text is AI
    return prob


def classify_text_local(text):
    """Runs desklib on the WHOLE text in chunks (not just the intro).
    Returns (human_pct, ai_pct, ok)."""
    if _text_model is None or _tokenizer is None:
        return None, None, False
    try:
        words = text.split()
        chunk_size = 350
        chunks = [' '.join(words[i:i + chunk_size]) for i in range(0, len(words), chunk_size)] or [text]
        ai_scores = []
        for chunk in chunks[:5]:  # cap at 5 chunks to stay fast on CPU
            if not chunk.strip():
                continue
            ai_scores.append(_ai_prob_for_chunk(chunk))
        if not ai_scores:
            return None, None, False
        avg_ai = sum(ai_scores) / len(ai_scores)
        return round((1 - avg_ai) * 100), round(avg_ai * 100), True
    except Exception as e:
        print(f"Local text model error: {type(e).__name__}: {e}")
        return None, None, False


def supabase_insert(table, data):
    if not SUPABASE_URL or not SUPABASE_KEY:
        return False, "Supabase not configured on server"
    try:
        url = f"{SUPABASE_URL}/rest/v1/{table}"
        headers = {
            "apikey": SUPABASE_KEY,
            "Authorization": f"Bearer {SUPABASE_KEY}",
            "Content-Type": "application/json",
            "Prefer": "return=minimal"
        }
        response = requests.post(url, headers=headers, json=data, timeout=15)
        if response.status_code in (200, 201, 204):
            return True, None
        return False, f"Supabase error {response.status_code}: {response.text[:200]}"
    except Exception as e:
        return False, str(e)


def supabase_select(table, filters=None):
    if not SUPABASE_URL or not SUPABASE_KEY:
        return None, "Supabase not configured on server"
    try:
        url = f"{SUPABASE_URL}/rest/v1/{table}"
        headers = {
            "apikey": SUPABASE_KEY,
            "Authorization": f"Bearer {SUPABASE_KEY}",
        }
        response = requests.get(url, headers=headers, params=filters or {}, timeout=15)
        if response.status_code == 200:
            return response.json(), None
        return None, f"Supabase error {response.status_code}: {response.text[:200]}"
    except Exception as e:
        return None, str(e)


def detect_with_hf(text):
    """Backed by the locally-loaded model now (name kept so other routes and
    the Telegram bot keep working without changes)."""
    return classify_text_local(text)


def detect_with_hf_v2(text):
    """Kept for reference. No longer used by the ensemble (we run a single
    local model now)."""
    return classify_text_local(text)


def ensemble_detect_text(text):
    """Runs the local AI-text model. Falls back to the heuristic ONLY if the
    model failed to load at startup."""
    human, ai, ok = classify_text_local(text)
    if ok:
        return human, ai, True, TEXT_MODEL_NAME
    trust, ai_prob = fallback_analyze(text)
    return trust, ai_prob, False, "heuristic-fallback"


def fallback_analyze(text):
    words = text.split()
    sentences = [s.strip() for s in re.split(r'[.!?]+', text) if len(s.strip()) > 4]
    transitions = ['furthermore','additionally','moreover','however','consequently','nevertheless','therefore','in conclusion','in summary','it is important','it should be noted','as a result','on the other hand','notably','significantly']
    lc = text.lower()
    trans_score = min(sum(1 for t in transitions if t in lc) * 8, 32)
    avg_len = len(words) / max(len(sentences), 1)
    sent_score = 30 if 14 < avg_len < 28 else 0
    human_punct = len(re.findall(r'[—–…\'\'""\(\)]', text))
    punct_score = 20 if human_punct < 2 else 0
    unique = set(w.lower().strip('.,!?') for w in words)
    rich_score = 10 if len(unique)/max(len(words),1) < 0.55 else 0
    starters = [' '.join(s.split()[:2]).lower() for s in sentences]
    rep_score = 15 if len(set(starters))/max(len(starters),1) < 0.7 else 0
    ai_score = min(trans_score + sent_score + punct_score + rich_score + rep_score, 97)
    return max(100 - ai_score, 3), ai_score


AI_BUZZWORDS = [
    'delve', 'tapestry', 'testament', 'boasts', 'meticulous', 'meticulously',
    'underscore', 'underscores', 'leverage', 'leveraging', 'robust', 'seamless',
    'seamlessly', 'realm', 'landscape', 'navigate', 'navigating', 'foster',
    'fostering', 'intricate', 'myriad', 'paramount', 'unprecedented', 'pivotal',
    'holistic', 'synergy', 'cutting-edge', 'game-changer', 'game changer',
    'in today\'s world', 'in the realm of', 'it is important to note',
    'it is worth noting', 'plays a crucial role', 'plays a significant role',
    'a testament to', 'stands as a', 'in conclusion', 'furthermore', 'moreover',
    'nevertheless', 'notably', 'boundless', 'unwavering', 'multifaceted',
    'ever-evolving', 'ever-changing', 'crucial role', 'vibrant tapestry'
]


def extract_buzzwords(text):
    lc = text.lower()
    found = []
    for word in AI_BUZZWORDS:
        pattern = r'\b' + re.escape(word) + r'\b'
        if re.search(pattern, lc):
            found.append(word)
    return found


def build_signals(trust_score, ai_probability, used_hf):
    signals = []
    if used_hf:
        if ai_probability > 70:
            signals.append({"type":"positive","icon":"⚑","title":"AI Detected β€” High Confidence","desc":f"Detection model found strong AI patterns. AI probability: {ai_probability}%."})
        elif ai_probability > 40:
            signals.append({"type":"positive","icon":"⚑","title":"Mixed Signals Detected","desc":f"Both AI and human patterns found. AI probability: {ai_probability}%."})
        else:
            signals.append({"type":"negative","icon":"βœ“","title":"Human Writing Detected","desc":f"Strong human writing patterns found. AI probability: {ai_probability}%."})
        signals.append({"type":"negative" if trust_score > 60 else "positive","icon":"πŸ€–","title":f"Powered by: {TEXT_MODEL_NAME}","desc":"Open-source ML model trained to detect AI-generated text."})
    else:
        signals.append({"type":"positive" if ai_probability > 50 else "negative","icon":"⚑","title":"Heuristic Analysis","desc":"Pattern-based detection. AI model warming up β€” try again in 20 seconds."})
    return signals


def detect_image_deepfake(image_data):
    """Runs the locally-loaded ViT deepfake detector. No more HF Inference API,
    so no more 500s from a dead endpoint."""
    if image_classifier is None:
        return {"success":False,"error":"Image model not loaded on server. Try again shortly.","deepfake_probability":0,"real_probability":0,"verdict":"UNKNOWN","confidence":0,"model":"error"}
    try:
        img = Image.open(io.BytesIO(image_data)).convert("RGB")
        results = image_classifier(img)  # list of {label, score}
        fake_score = 0.0
        real_score = 0.0
        for item in results:
            label = str(item.get('label', '')).lower()
            score = item.get('score', 0)
            if label in ['fake', 'deepfake', 'label_1', 'artificial']:
                fake_score = score
            elif label in ['real', 'genuine', 'label_0', 'authentic']:
                real_score = score
        if fake_score == 0 and real_score == 0 and len(results) >= 2:
            real_score = results[0]['score']
            fake_score = results[1]['score']
        deepfake_prob = round(fake_score * 100)
        real_prob = round(real_score * 100)
        return {"success":True,"deepfake_probability":deepfake_prob,"real_probability":real_prob,"verdict":"DEEPFAKE" if deepfake_prob > 50 else "AUTHENTIC","confidence":max(deepfake_prob, real_prob),"model":IMAGE_MODEL_NAME}
    except Exception as e:
        print(f"Image detection error: {type(e).__name__}: {e}")
        return {"success":False,"error":"Could not analyze this image. Try a clear JPG/PNG.","deepfake_probability":0,"real_probability":0,"verdict":"UNKNOWN","confidence":0,"model":"error"}


def detect_voice_deepfake(audio_data, content_type):
    try:
        headers = {"Authorization": f"Bearer {HF_API_KEY}", "Content-Type": content_type}
        response = requests.post(HF_AUDIO_MODEL_URL, headers=headers, data=audio_data, timeout=60)
        print(f"Audio HF Status: {response.status_code}, Response: {response.text[:300]}")
        result = response.json()
        if isinstance(result, list) and len(result) > 0:
            scores = result
            fake_score = 0
            real_score = 0
            for item in scores:
                label = item.get('label', '').lower()
                score = item.get('score', 0)
                if label in ['fake', 'spoof', 'synthetic', 'ai', 'label_1']:
                    fake_score = score
                elif label in ['real', 'bonafide', 'genuine', 'human', 'label_0']:
                    real_score = score
            if fake_score == 0 and real_score == 0 and len(scores) >= 2:
                real_score = scores[0]['score']
                fake_score = scores[1]['score']
            fake_prob = round(fake_score * 100)
            real_prob = round(real_score * 100)
            return {"success":True,"fake_probability":fake_prob,"real_probability":real_prob,"verdict":"AI VOICE / CLONED" if fake_prob > 50 else "LIKELY REAL VOICE","confidence":max(fake_prob, real_prob),"model":"wav2vec2-deepfake-audio-classification"}
    except Exception as e:
        print(f"Audio detection error: {type(e).__name__}: {e}")
    return {"success":False,"error":"Model loading. Please try again in 30 seconds.","fake_probability":0,"real_probability":0,"verdict":"UNKNOWN","confidence":0,"model":"error"}


def generate_prompt_from_image(image_data, content_type):
    try:
        headers = {"Authorization": f"Bearer {HF_API_KEY}", "Content-Type": content_type}
        response = requests.post(HF_CAPTION_MODEL_URL, headers=headers, data=image_data, timeout=60)
        print(f"Caption HF Status: {response.status_code}, Response: {response.text[:300]}")
        result = response.json()
        if isinstance(result, list) and len(result) > 0:
            caption = result[0].get('generated_text', '').strip()
            if caption:
                # Turn the plain caption into a more "prompt-style" phrase
                prompt_style = f"A highly detailed, realistic image of {caption}, cinematic lighting, high resolution, professional photography"
                return {"success": True, "caption": caption, "suggested_prompt": prompt_style}
    except Exception as e:
        print(f"Caption error: {type(e).__name__}: {e}")
    return {"success": False, "error": "Model loading. Please try again in 30 seconds."}


import subprocess
import tempfile
from argon2 import PasswordHasher

ph = PasswordHasher()


def create_blockchain_timestamp(content_hash_hex):
    """Creates a real OpenTimestamps proof by calling the official `ots`
    CLI tool (installed via the opentimestamps-client pip package). This
    submits the hash to public OpenTimestamps calendar servers, which will
    anchor it into the Bitcoin blockchain within a few hours. Returns the
    base64-encoded .ots proof file, or None if it fails (fails gracefully β€”
    the certificate is still valid without it, just without blockchain proof)."""
    try:
        with tempfile.TemporaryDirectory() as tmpdir:
            digest_path = os.path.join(tmpdir, "digest.bin")
            with open(digest_path, "wb") as f:
                f.write(bytes.fromhex(content_hash_hex))

            result = subprocess.run(
                ["ots", "stamp", digest_path],
                capture_output=True, timeout=30
            )
            ots_path = digest_path + ".ots"
            if os.path.exists(ots_path):
                with open(ots_path, "rb") as f:
                    proof_bytes = f.read()
                return base64.b64encode(proof_bytes).decode('utf-8')
            print(f"OTS stamp stderr: {result.stderr.decode()[:300]}")
    except Exception as e:
        print(f"Blockchain timestamp error: {type(e).__name__}: {e}")
    return None


def check_blockchain_timestamp(digest_hex, proof_b64):
    """Checks whether an OTS proof has been confirmed on the Bitcoin
    blockchain yet (takes a few hours after creation). Returns a status
    string: 'confirmed', 'pending', or 'unknown'."""
    try:
        with tempfile.TemporaryDirectory() as tmpdir:
            digest_path = os.path.join(tmpdir, "digest.bin")
            ots_path = digest_path + ".ots"
            with open(digest_path, "wb") as f:
                f.write(bytes.fromhex(digest_hex))
            with open(ots_path, "wb") as f:
                f.write(base64.b64decode(proof_b64))

            result = subprocess.run(
                ["ots", "verify", ots_path], capture_output=True, timeout=30
            )
            output = (result.stdout.decode() + result.stderr.decode()).lower()
            if "bitcoin block" in output or "attests" in output:
                return "confirmed"
            elif "pending" in output:
                return "pending"
    except Exception as e:
        print(f"OTS verify error: {type(e).__name__}: {e}")
    return "unknown"


def generate_cert_id(text, trust_score):
    raw = f"{text[:100]}{trust_score}{datetime.datetime.utcnow().isoformat()}"
    h = hashlib.sha256(raw.encode()).hexdigest()[:16].upper()
    return f"AXT-{h[:4]}-{h[4:8]}-{h[8:12]}"


def extract_writing_style(text):
    words = text.split()
    sentences = [s.strip() for s in re.split(r'[.!?]+', text) if len(s.strip()) > 4]
    unique_words = set(w.lower().strip('.,!?;"\'') for w in words)
    return {
        "vocab_richness": round((len(unique_words)/max(len(words),1))*100),
        "avg_sentence_length": round(len(words)/max(len(sentences),1)),
        "punctuation_variety": len(re.findall(r'[—–…\'\'""\(\)!?;:]', text)),
        "avg_word_length": round(sum(len(w) for w in words)/max(len(words),1), 1),
        "word_count": len(words),
        "sentence_count": len(sentences)
    }


# ── ORIGINAL ROUTES (kept for backward compatibility) ──

@app.route('/analyze', methods=['POST'])
def analyze():
    data = request.get_json()
    if not data or 'text' not in data:
        return jsonify({"error": "No text provided"}), 400
    text = data['text'].strip()
    if len(text) < 30:
        return jsonify({"error": "Text too short. Minimum 30 characters."}), 400
    trust_score, ai_probability, used_hf = detect_with_hf(text)
    if trust_score is None:
        trust_score, ai_probability = fallback_analyze(text)
        used_hf = False
    return jsonify({"trust":trust_score,"ai_probability":ai_probability,"signals":build_signals(trust_score,ai_probability,used_hf),"model":TEXT_MODEL_NAME if used_hf else "heuristic-fallback"})


@app.route('/analyze-image', methods=['POST'])
def analyze_image():
    try:
        if 'image' in request.files:
            image_data = request.files['image'].read()
        elif request.is_json:
            data = request.get_json()
            if 'image_base64' not in data:
                return jsonify({"error": "No image provided"}), 400
            b64 = data['image_base64']
            if ',' in b64:
                b64 = b64.split(',')[1]
            image_data = base64.b64decode(b64)
        else:
            return jsonify({"error": "No image provided"}), 400
        if len(image_data) > 5 * 1024 * 1024:
            return jsonify({"error": "Image too large. Maximum 5MB."}), 400
        return jsonify(detect_image_deepfake(image_data))
    except Exception as e:
        print(f"Image route error: {e}")
        return jsonify({"error": "Image processing failed."}), 500


@app.route('/certificate', methods=['POST'])
def generate_certificate():
    data = request.get_json()
    if not data or 'text' not in data:
        return jsonify({"error": "No text provided"}), 400
    text = data.get('text','').strip()
    author_name = data.get('author_name','Anonymous').strip()
    document_title = data.get('document_title','Untitled Document').strip()
    if len(text) < 30:
        return jsonify({"error": "Text too short."}), 400
    trust_score, ai_probability, used_hf = detect_with_hf(text)
    if trust_score is None:
        trust_score, ai_probability = fallback_analyze(text)
        used_hf = False
    if trust_score < 60:
        return jsonify({"error":"Certificate cannot be issued","reason":f"Trust score too low ({trust_score}/100).","trust_score":trust_score,"ai_probability":ai_probability}), 422
    cert_id = generate_cert_id(text, trust_score)
    timestamp = datetime.datetime.utcnow()
    style = extract_writing_style(text)
    content_hash = hashlib.sha256(text.encode()).hexdigest()[:32].upper()
    return jsonify({"certificate_id":cert_id,"status":"VERIFIED","issued_at":timestamp.strftime("%Y-%m-%d %H:%M:%S UTC"),"issued_date":timestamp.strftime("%B %d, %Y"),"author_name":author_name,"document_title":document_title,"trust_score":trust_score,"ai_probability":ai_probability,"human_probability":100-ai_probability,"content_hash":content_hash,"word_count":style["word_count"],"writing_style":style,"verdict":"HUMAN AUTHORED","model_used":TEXT_MODEL_NAME if used_hf else "heuristic-fallback","verify_url":f"https://aethelx.com/verify.html?id={cert_id}","issuer":"AETHELX AI Trust Infrastructure","version":"1.0"})


# ── ROUTES matching the frontend tool pages ──

@app.route('/detect-text', methods=['POST'])
def detect_text():
    data = request.get_json()
    if not data or 'text' not in data:
        return jsonify({"error": "No text provided"}), 400
    text = data['text'].strip()
    if len(text) < 30:
        return jsonify({"error": "Text too short. Minimum 30 characters."}), 400
    trust_score, ai_probability, used_hf, model_note = ensemble_detect_text(text)
    buzzwords = extract_buzzwords(text)

    # Short text is unreliable for ANY detector β€” warn the user clearly so a
    # false positive can't be used to wrongly accuse someone.
    word_count = len(text.split())
    reliable = word_count >= 50
    if not reliable:
        details = (f"⚠️ Short text ({word_count} words) β€” detection is unreliable below "
                   f"~50 words. Paste a longer passage for a meaningful result. "
                   f"(Model: {model_note})")
    else:
        details = f"Analyzed with {model_note}" if used_hf else "Heuristic analysis (models warming up)"

    return jsonify({
        "ai_probability": ai_probability / 100,
        "trust_score": trust_score,
        "reliable": reliable,
        "word_count": word_count,
        "details": details,
        "buzzwords_found": buzzwords
    })


@app.route('/detect-image', methods=['POST'])
def detect_image():
    try:
        if 'image' in request.files:
            image_data = request.files['image'].read()
        elif request.is_json:
            data = request.get_json()
            if 'image_base64' not in data:
                return jsonify({"error": "No image provided"}), 400
            b64 = data['image_base64']
            if ',' in b64:
                b64 = b64.split(',')[1]
            image_data = base64.b64decode(b64)
        else:
            return jsonify({"error": "No image provided"}), 400
        if len(image_data) > 5 * 1024 * 1024:
            return jsonify({"error": "Image too large. Maximum 5MB."}), 400
        result = detect_image_deepfake(image_data)
        if not result.get("success"):
            return jsonify({"error": result.get("error", "Detection failed")}), 500
        return jsonify({
            "label": result["verdict"],
            "confidence": result["confidence"] / 100,
            "deepfake_probability": result["deepfake_probability"],
            "real_probability": result["real_probability"],
            "model": result["model"]
        })
    except Exception as e:
        print(f"Image route error: {e}")
        return jsonify({"error": "Image processing failed."}), 500


# ── NEW: VOICE CLONING DETECTOR ──

@app.route('/detect-audio', methods=['POST'])
def detect_audio():
    try:
        if 'audio' not in request.files:
            return jsonify({"error": "No audio file provided"}), 400
        audio_file = request.files['audio']
        audio_data = audio_file.read()

        if len(audio_data) > 10 * 1024 * 1024:
            return jsonify({"error": "Audio file too large. Maximum 10MB."}), 400
        if len(audio_data) < 100:
            return jsonify({"error": "Audio file seems empty or corrupted."}), 400

        filename = (audio_file.filename or '').lower()
        if filename.endswith('.mp3'):
            content_type = 'audio/mpeg'
        elif filename.endswith('.ogg'):
            content_type = 'audio/ogg'
        elif filename.endswith('.m4a'):
            content_type = 'audio/mp4'
        else:
            content_type = 'audio/wav'

        result = detect_voice_deepfake(audio_data, content_type)
        if not result.get("success"):
            return jsonify({"error": result.get("error", "Detection failed")}), 500
        return jsonify({
            "label": result["verdict"],
            "confidence": result["confidence"] / 100,
            "fake_probability": result["fake_probability"]
        })
    except Exception as e:
        print(f"Audio route error: {e}")
        return jsonify({"error": "Audio processing failed."}), 500


# ── NEW: AI PROMPT REVERSE ENGINEERING ──

@app.route('/reverse-prompt', methods=['POST'])
def reverse_prompt():
    try:
        if 'image' not in request.files:
            return jsonify({"error": "No image provided"}), 400
        image_file = request.files['image']
        image_data = image_file.read()

        if len(image_data) > 5 * 1024 * 1024:
            return jsonify({"error": "Image too large. Maximum 5MB."}), 400

        filename = (image_file.filename or '').lower()
        content_type = 'image/png' if filename.endswith('.png') else 'image/jpeg'

        result = generate_prompt_from_image(image_data, content_type)
        if not result.get("success"):
            return jsonify({"error": result.get("error", "Could not generate prompt")}), 500
        return jsonify({
            "caption": result["caption"],
            "suggested_prompt": result["suggested_prompt"]
        })
    except Exception as e:
        print(f"Reverse prompt route error: {e}")
        return jsonify({"error": "Image processing failed."}), 500


@app.route('/generate-certificate', methods=['POST'])
def generate_certificate_v2():
    data = request.get_json()
    if not data:
        return jsonify({"error": "No data provided"}), 400
    text = data.get('text', '').strip()
    author_name = data.get('name', 'Anonymous').strip()
    document_title = data.get('title', 'Untitled Document').strip()
    if len(text) < 30:
        return jsonify({"error": "Text too short."}), 400
    trust_score, ai_probability, used_hf, model_note = ensemble_detect_text(text)
    cert_id = generate_cert_id(text, trust_score)
    content_hash = hashlib.sha256(text.encode()).hexdigest()[:12].upper()
    fingerprint = f"FP-{content_hash}"
    full_hash = hashlib.sha256(text.encode()).hexdigest()

    ots_proof = create_blockchain_timestamp(full_hash)
    ots_status = "pending" if ots_proof else "unavailable"

    ok, err = supabase_insert("certificates", {
        "cert_id": cert_id,
        "author_name": author_name,
        "document_title": document_title,
        "trust_score": trust_score,
        "ai_probability": ai_probability,
        "fingerprint": fingerprint,
        "full_hash": full_hash,
        "ots_proof": ots_proof,
        "ots_status": ots_status
    })
    if not ok:
        print(f"Certificate save error: {err}")

    return jsonify({
        "cert_id": cert_id,
        "fingerprint": fingerprint,
        "trust_score": trust_score,
        "ai_probability": ai_probability,
        "author_name": author_name,
        "document_title": document_title,
        "verify_url": f"https://aethelx.com/verify.html?id={cert_id}",
        "blockchain_status": ots_status
    })


@app.route('/check-blockchain-status', methods=['GET'])
def check_blockchain_status():
    cert_id = request.args.get('id', '').strip()
    if not cert_id:
        return jsonify({"error": "No certificate ID provided"}), 400

    rows, err = supabase_select("certificates", {"cert_id": f"eq.{cert_id}"})
    if err or not rows:
        return jsonify({"error": "Certificate not found"}), 404

    cert = rows[0]
    if not cert.get("full_hash") or not cert.get("ots_proof"):
        return jsonify({"status": "unavailable", "message": "No blockchain proof was created for this certificate."})

    status = check_blockchain_timestamp(cert["full_hash"], cert["ots_proof"])
    return jsonify({"status": status})


@app.route('/verify-certificate', methods=['GET'])
def verify_certificate():
    cert_id = request.args.get('id', '').strip()
    if not cert_id:
        return jsonify({"error": "No certificate ID provided"}), 400

    rows, err = supabase_select("certificates", {"cert_id": f"eq.{cert_id}"})
    if err:
        print(f"Verify lookup error: {err}")
        return jsonify({"error": "Could not look up certificate"}), 500
    if not rows:
        return jsonify({"error": "Certificate not found"}), 404

    return jsonify(rows[0])


# ── WAITLIST + SIGNUP (saved to Supabase) ──

@app.route('/waitlist', methods=['POST'])
def join_waitlist():
    data = request.get_json()
    if not data or not data.get('name') or not data.get('email'):
        return jsonify({"error": "Name and email are required"}), 400
    name = data['name'].strip()
    email = data['email'].strip().lower()
    plan = data.get('plan', 'Free').strip()

    ok, err = supabase_insert("waitlist", {"name": name, "email": email, "plan": plan})
    if not ok:
        print(f"Waitlist insert error: {err}")
        return jsonify({"error": "Could not save to waitlist. Try again later."}), 500
    return jsonify({"success": True, "message": "Added to waitlist"})


@app.route('/signup', methods=['POST'])
def signup():
    data = request.get_json()
    if not data or not data.get('name') or not data.get('email') or not data.get('password'):
        return jsonify({"error": "Name, email and password are required"}), 400
    name = data['name'].strip()
    email = data['email'].strip().lower()
    password_hash = ph.hash(data['password'])

    ok, err = supabase_insert("users", {"name": name, "email": email, "password_hash": password_hash})
    if not ok:
        print(f"Signup insert error: {err}")
        return jsonify({"error": "Could not create account. Email may already be registered."}), 500
    return jsonify({"success": True, "message": "Account created", "name": name, "email": email})

# β€” CONTACT (saved to Supabase) β€”

@app.route('/submit-contact', methods=['POST'])
def submit_contact():
    data = request.get_json()
    if not data or not data.get('name') or not data.get('email') or not data.get('message'):
        return jsonify({"error": "Name, email and message are required"}), 400
    name = data['name'].strip()
    email = data['email'].strip().lower()
    subject = data.get('subject', '').strip()
    message = data['message'].strip()

    ok, err = supabase_insert("contacts", {"name": name, "email": email, "subject": subject, "message": message})
    if not ok:
        print(f"Contact insert error: {err}")
        return jsonify({"error": "Could not send message. Try again later."}), 500

    return jsonify({"success": True, "message": "Message sent"})

# β€” ONBOARDING (attribution) β€”

@app.route('/save-onboarding', methods=['POST'])
def save_onboarding():
    data = request.get_json() or {}
    email = (data.get('email') or '').strip().lower()
    heard_from = (data.get('heard_from') or '').strip()
    role = (data.get('role') or '').strip()
    note = (data.get('note') or '').strip()

    ok, err = supabase_insert("onboarding", {"email": email, "heard_from": heard_from, "role": role, "note": note})
    if not ok:
        print(f"Onboarding insert error: {err}")
        return jsonify({"error": "Could not save"}), 500

    return jsonify({"success": True})

# ── TELEGRAM BOT ──

def telegram_send(chat_id, text):
    try:
        requests.post(f"{TELEGRAM_API_URL}/sendMessage", json={
            "chat_id": chat_id,
            "text": text,
            "parse_mode": "Markdown"
        }, timeout=10)
    except Exception as e:
        print(f"Telegram send error: {e}")


@app.route('/telegram-webhook', methods=['POST'])
def telegram_webhook():
    update = request.get_json(silent=True) or {}
    message = update.get('message', {})
    chat_id = message.get('chat', {}).get('id')
    text = message.get('text', '').strip()

    if not chat_id:
        return jsonify({"ok": True})

    if text == '/start':
        telegram_send(chat_id,
            "πŸ‘‹ *Welcome to AETHELX Verify Bot!*\n\n"
            "Send me any text (30+ characters) and I'll check if it's likely "
            "AI-generated or human-written.\n\n"
            "⚠️ Beta model β€” treat results as one signal, not a final verdict.\n\n"
            "More tools: https://aethelx.com"
        )
        return jsonify({"ok": True})

    if not text:
        telegram_send(chat_id, "Please send me some text to analyze (30+ characters).")
        return jsonify({"ok": True})

    if len(text) < 30:
        telegram_send(chat_id, "⚠️ That's too short β€” send at least 30 characters of text.")
        return jsonify({"ok": True})

    trust_score, ai_probability, used_hf = detect_with_hf(text)
    if trust_score is None:
        trust_score, ai_probability = fallback_analyze(text)
        used_hf = False

    verdict = "⚠️ *Likely AI-generated*" if ai_probability > 50 else "βœ… *Likely human-written*"
    model_note = TEXT_MODEL_NAME if used_hf else "heuristic fallback (model warming up)"

    reply = (
        f"{verdict}\n\n"
        f"AI Probability: *{ai_probability}%*\n"
        f"Model: {model_note}\n\n"
        f"⚠️ Beta accuracy β€” treat as one signal, not a final verdict.\n"
        f"More tools: https://aethelx.com"
    )
    telegram_send(chat_id, reply)
    return jsonify({"ok": True})


@app.route('/telegram-set-webhook', methods=['GET'])
def telegram_set_webhook():
    webhook_url = "https://aggnes99-lumora-backend.hf.space/telegram-webhook"
    try:
        res = requests.get(f"{TELEGRAM_API_URL}/setWebhook", params={"url": webhook_url}, timeout=10)
        return jsonify(res.json())
    except Exception as e:
        return jsonify({"error": str(e)}), 500


# ── NETWORK DIAGNOSTIC ──

@app.route('/network-test', methods=['GET'])
def network_test():
    targets = {
        "google (general internet)": "https://www.google.com",
        "huggingface_router (text model domain)": "https://router.huggingface.co",
        "huggingface_api_old (deprecated domain)": "https://api-inference.huggingface.co",
        "supabase": SUPABASE_URL if SUPABASE_URL else "https://supabase.com",
        "telegram_api": "https://api.telegram.org",
    }
    results = {}
    for name, url in targets.items():
        start = time.time()
        try:
            res = requests.get(url, timeout=8)
            elapsed = round(time.time() - start, 2)
            results[name] = {"status": "OK", "http_status": res.status_code, "seconds": elapsed}
        except requests.exceptions.Timeout:
            elapsed = round(time.time() - start, 2)
            results[name] = {"status": "TIMEOUT", "seconds": elapsed}
        except Exception as e:
            elapsed = round(time.time() - start, 2)
            results[name] = {"status": "ERROR", "error": f"{type(e).__name__}: {str(e)[:150]}", "seconds": elapsed}
    return jsonify(results)


# ── MODEL STATUS (quick check that the models loaded) ──

@app.route('/model-status', methods=['GET'])
def model_status():
    return jsonify({
        "text_model": TEXT_MODEL_NAME,
        "text_loaded": _text_model is not None,
        "image_model": IMAGE_MODEL_NAME,
        "image_loaded": image_classifier is not None
    })

# ── EMAIL BREACH CHECK (via XposedOrNot, free, no API key) ──

@app.route('/check-email-breach', methods=['GET'])
def check_email_breach():
    email = request.args.get('email', '').strip().lower()
    if not email or '@' not in email:
        return jsonify({"error": "Please provide a valid email address"}), 400
    try:
        r = requests.get(
            "https://api.xposedornot.com/v1/breach-analytics",
            params={"email": email},
            timeout=15
        )
        if r.status_code == 404:
            return jsonify({"found": False, "breaches": [], "email": email})
        data = r.json()
        exposed = data.get("ExposedBreaches")
        if not exposed or not exposed.get("breaches_details"):
            return jsonify({"found": False, "breaches": [], "email": email})
        breaches = []
        for b in exposed["breaches_details"]:
            breaches.append({
                "name": b.get("breach", "Unknown"),
                "year": b.get("xposed_date", ""),
                "data": b.get("xposed_data", ""),
                "records": b.get("xposed_records", 0)
            })
        risk = {}
        try:
            risk = (data.get("BreachMetrics") or {}).get("risk", [{}])[0] or {}
        except Exception:
            risk = {}
        return jsonify({
            "found": True,
            "count": len(breaches),
            "breaches": breaches,
            "risk_label": risk.get("risk_label", ""),
            "risk_score": risk.get("risk_score", ""),
            "email": email
        })
    except Exception as e:
        print(f"Email breach check error: {type(e).__name__}: {e}")
        return jsonify({"error": "Could not reach breach database. Try again shortly."}), 502


# ── HOME / STATUS ──

@app.route('/', methods=['GET'])
def home():
    return jsonify({"status": "AETHELX API running", "version": "1.0"})
    

# ── BREACH MONITORING: add an email to the watch list ──

@app.route('/add-monitor', methods=['POST'])
def add_monitor():
    data = request.get_json()
    if not data or not data.get('email'):
        return jsonify({"error": "Email is required"}), 400
    email = data['email'].strip().lower()
    if '@' not in email:
        return jsonify({"error": "Enter a valid email"}), 400

    # Record current breaches as the baseline, so later we only alert on NEW ones.
    known = []
    try:
        r = requests.get("https://api.xposedornot.com/v1/breach-analytics",
                         params={"email": email}, timeout=15)
        if r.status_code == 200:
            dx = r.json()
            exposed = dx.get("ExposedBreaches")
            if exposed and exposed.get("breaches_details"):
                known = [b.get("breach", "") for b in exposed["breaches_details"] if b.get("breach")]
    except Exception as e:
        print(f"Monitor baseline error: {type(e).__name__}: {e}")

    ok, err = supabase_insert("monitors", {"email": email, "known_breaches": known})
    if not ok:
        print(f"Add monitor error: {err}")
        return jsonify({"error": "This email may already be monitored, or we couldn't save it. Try again."}), 500

    return jsonify({"success": True, "email": email, "baseline_count": len(known)})


# ── BREACH MONITORING: scan + email alerts (Phase B) ──

RESEND_API_KEY = os.environ.get("RESEND_API_KEY", "")
SCAN_SECRET = os.environ.get("SCAN_SECRET", "")
ALERT_FROM = os.environ.get("ALERT_FROM", "AETHELX Alerts <onboarding@resend.dev>")


def send_email(to_email, subject, html):
    if not RESEND_API_KEY:
        print("send_email: RESEND_API_KEY not set")
        return False
    try:
        r = requests.post(
            "https://api.resend.com/emails",
            headers={"Authorization": f"Bearer {RESEND_API_KEY}", "Content-Type": "application/json"},
            json={"from": ALERT_FROM, "to": [to_email], "subject": subject, "html": html},
            timeout=15
        )
        if r.status_code in (200, 201):
            return True
        print(f"Resend error {r.status_code}: {r.text[:200]}")
        return False
    except Exception as e:
        print(f"send_email error: {type(e).__name__}: {e}")
        return False


def _xon_breach_names(email):
    try:
        r = requests.get("https://api.xposedornot.com/v1/breach-analytics",
                         params={"email": email}, timeout=15)
        if r.status_code == 200:
            dx = r.json()
            exposed = dx.get("ExposedBreaches")
            if exposed and exposed.get("breaches_details"):
                return [b.get("breach", "") for b in exposed["breaches_details"] if b.get("breach")]
    except Exception as e:
        print(f"xon lookup error: {type(e).__name__}: {e}")
    return []


def supabase_update_monitor(email, known_breaches):
    if not SUPABASE_URL or not SUPABASE_KEY:
        return
    try:
        url = f"{SUPABASE_URL}/rest/v1/monitors"
        headers = {
            "apikey": SUPABASE_KEY,
            "Authorization": f"Bearer {SUPABASE_KEY}",
            "Content-Type": "application/json",
            "Prefer": "return=minimal"
        }
        payload = {"known_breaches": known_breaches,
                   "last_checked": datetime.datetime.utcnow().isoformat()}
        requests.patch(url, headers=headers, params={"email": f"eq.{email}"},
                       json=payload, timeout=15)
    except Exception as e:
        print(f"supabase update error: {type(e).__name__}: {e}")


def build_alert_html(email, new_breaches):
    items = "".join(f"<li><b>{b}</b></li>" for b in new_breaches)
    return f"""
    <div style="font-family:Arial,sans-serif;max-width:520px;margin:0 auto;color:#111">
      <h2 style="color:#c93b40">&#9888;&#65039; Your email appeared in a new data breach</h2>
      <p>We're monitoring <b>{email}</b>, and it just showed up in {len(new_breaches)} new breach(es):</p>
      <ul>{items}</ul>
      <p><b>What to do now:</b></p>
      <ol>
        <li>Change your password on the affected site(s), and anywhere you reused it.</li>
        <li>Turn on 2-step verification.</li>
        <li>Watch for phishing emails pretending to be these companies.</li>
      </ol>
      <p style="font-size:12px;color:#666">You're receiving this because you signed up for breach monitoring at aethelx.com.
      This is an automated security alert, not financial or legal advice.</p>
    </div>
    """


@app.route('/run-breach-scan', methods=['GET'])
def run_breach_scan():
    if not SCAN_SECRET or request.args.get('token') != SCAN_SECRET:
        return jsonify({"error": "Unauthorized"}), 401

    rows, err = supabase_select("monitors")
    if err or rows is None:
        return jsonify({"error": f"Could not load monitors: {err}"}), 500

    checked = 0
    alerts_sent = 0
    for row in rows[:50]:  # safety cap per run
        email = (row.get("email") or "").strip().lower()
        if not email:
            continue
        old = row.get("known_breaches") or []
        if isinstance(old, str):
            old = []
        current = _xon_breach_names(email)
        checked += 1
        new_ones = [b for b in current if b not in old]
        if new_ones:
            html = build_alert_html(email, new_ones)
            if send_email(email, "Security alert: your email appeared in a new data breach", html):
                alerts_sent += 1
        supabase_update_monitor(email, current)
        time.sleep(1)  # stay under XposedOrNot rate limits

    return jsonify({"status": "done", "checked": checked, "alerts_sent": alerts_sent})
    

# ── LINK SAFETY CHECK (via URLhaus, free, no key) ──

@app.route('/check-link', methods=['GET'])
def check_link():
    from urllib.parse import urlparse
    url = request.args.get('url', '').strip()
    if not url:
        return jsonify({"error": "Please provide a URL"}), 400
    if not url.startswith(('http://', 'https://')):
        url = 'http://' + url
    try:
        r = requests.post("https://urlhaus-api.abuse.ch/v1/url/", data={"url": url}, timeout=15)
        d = r.json()
        if d.get("query_status") == "ok":
            return jsonify({"verdict": "threat", "threat": d.get("threat", "malicious"),
                            "tags": d.get("tags") or [], "url": url})
        host = urlparse(url).hostname or ""
        if host:
            rh = requests.post("https://urlhaus-api.abuse.ch/v1/host/", data={"host": host}, timeout=15)
            dh = rh.json()
            if dh.get("query_status") == "ok" and int(dh.get("url_count", 0) or 0) > 0:
                return jsonify({"verdict": "suspicious", "url_count": dh.get("url_count"),
                                "host": host, "url": url})
        return jsonify({"verdict": "clean", "url": url})
    except Exception as e:
        print(f"Link check error: {type(e).__name__}: {e}")
        return jsonify({"error": "Could not check the link right now. Try again shortly."}), 502
        

@app.route('/check-waitlist', methods=['GET'])
def check_waitlist():
    if request.args.get('token') != SCAN_SECRET:
        return jsonify({"error": "Unauthorized"}), 401
    ok, err = supabase_insert("waitlist", {"name": "Test", "email": "wltest@test.com", "plan": "Free"})
    rows, rerr = supabase_select("waitlist")
    return jsonify({"insert_ok": ok, "insert_error": err, "row_count": len(rows) if rows else 0, "read_error": rerr})
    
    
@app.route('/test-email', methods=['GET'])
def test_email():
    if request.args.get('token') != SCAN_SECRET:
        return jsonify({"error": "Unauthorized"}), 401
    to = request.args.get('to', '').strip()
    if not to:
        return jsonify({"error": "Add ?to=your@email.com"}), 400
    ok = send_email(to, "AETHELX test email",
                    "<p>If you can read this, your alerts are working! \u2705</p>")
    return jsonify({"sent": ok, "to": to, "from": ALERT_FROM})

    
if __name__ == '__main__':
    app.run(host="0.0.0.0", port=7860)