File size: 72,713 Bytes
2ff1f39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
958d51b
2ff1f39
 
958d51b
 
 
 
 
 
2ff1f39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e98ba5c
5390afe
e98ba5c
 
2ff1f39
 
 
e98ba5c
 
5390afe
e98ba5c
 
2ff1f39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e98ba5c
 
 
5390afe
e98ba5c
 
 
 
2ff1f39
 
 
 
e98ba5c
2ff1f39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e98ba5c
5390afe
e98ba5c
2ff1f39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
#!/usr/bin/env python3
"""
Ghostprint Semantic Encryption Engine with Information Geometry AI Poisoning
==========================================================================

Complete implementation integrating proven AI poisoning techniques through
information geometry masks and hybrid perturbation methods.
"""

import os
import sys
import time
import json
import hashlib
import lzma
import base64
import math
import cmath
import itertools
import struct
from datetime import datetime
from typing import Dict, List, Tuple, Any, Optional
from dataclasses import dataclass
from enum import Enum
import numpy as np
from scipy.special import gamma, airy, jv
from ghost_engine_configs import get_config
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from PIL import Image, PngImagePlugin
from io import BytesIO
from armor import ArmorGenerator, ArmorConfig, Ring, apply_delta_autosize, analyze_array_bands

# --- Global Dependency Check for OpenCV ---
try:
    import cv2
    CV2_AVAILABLE = True
except ImportError:
    cv2 = None
    CV2_AVAILABLE = False
    # OpenCV is optional - all core functionality works with Pillow
    # Only suppress this message in production, show in debug mode
    import os
    if os.environ.get('GHOSTPRINT_DEBUG', '').lower() == 'true':
        print("[INFO] OpenCV not installed. Using Pillow for all image operations (fully functional).")

# Core mathematical constants
C1 = complex(0.587, 1.223)  # Complexity weighting coefficient
C2 = complex(-0.994, 0.0)   # Magnitude scaling coefficient

# Harmonic frequencies for enhanced phase encoding
HARMONIC_FREQUENCIES = [271, 341, 491]
HARMONIC_AMPLITUDES = [0.033, 0.050, 0.100]

# Homoglyph mapping for AI poisoning
HOMOGLYPHS = {
    "a": "а",  # Latin a → Cyrillic a (U+0061 → U+0430)
    "e": "е",  # Latin e → Cyrillic e (U+0065 → U+0435)
    "i": "і",  # Latin i → Ukrainian i (U+0069 → U+0456)
    "o": "о",  # Latin o → Cyrillic o (U+006F → U+043E)
    "p": "р",  # Latin p → Cyrillic p (U+0070 → U+0440)
    "c": "с",  # Latin c → Cyrillic c (U+0063 → U+0441)
    "x": "х",  # Latin x → Cyrillic x (U+0078 → U+0445)
}

# Inverted homoglyph map for de-poisoning
REVERSE_HOMOGLYPHS = {v: k for k, v in HOMOGLYPHS.items()}

class LensFunction(Enum):
    """Mathematical lens functions for CMT transformation"""
    GAMMA = "gamma"
    AIRY = "airy" 
    BESSEL = "bessel"

@dataclass
class GeometricFingerprint:
    """Container for complete geometric fingerprint data"""
    cmt_signature: List[complex]
    lehi_pattern: List[float]
    lgrm_map: np.ndarray
    holographic_field: List[complex]
    srl_stability: float
    sefa_emergence: float
    metadata: Dict[str, Any]

class InformationGeometryEngine:
    """Core engine for information geometry transformations with AI poisoning"""
    
    def __init__(self):
        self.c1 = C1
        self.c2 = C2
        
    # ---------- Information Geometry Mask Generation ----------
    
    def golden_mask_positions(self, n: int, phi: float = (1 + 5 ** 0.5) / 2) -> List[int]:
        """Generate positions using golden ratio distribution"""
        positions = []
        x = 0.0
        for i in range(n):
            x = (x + phi) % 1.0
            positions.append(int(x * n))
        return sorted(set(positions))
    
    def fibonacci_mask_positions(self, n: int) -> List[int]:
        """Generate positions using Fibonacci sequence"""
        fib = [1, 2]
        while fib[-1] < n:
            fib.append(fib[-1] + fib[-2])
        return [f % n for f in fib if f < n]
    
    def harmonic_mask_positions(self, n: int, freq: int = 5) -> List[int]:
        """Generate positions using harmonic wave distribution"""
        return [int((np.sin(i * freq) + 1) / 2 * n) for i in range(1, n, max(1, n // 20))]
    
    def generate_content_keyed_harmonics(self, data: bytes, content_hash: str) -> List[float]:
        """Generate harmonics keyed to content hash for deterministic perturbations"""
        hash_bytes = bytes.fromhex(content_hash[:12])  # Use first 6 bytes
        
        # Extract parameters from hash
        freq1 = 271 + (hash_bytes[0] % 50)
        freq2 = 341 + (hash_bytes[1] % 50)
        freq3 = 491 + (hash_bytes[2] % 50)
        
        amp1 = 0.033 * (1 + hash_bytes[3] / 512)
        amp2 = 0.050 * (1 + hash_bytes[4] / 512)
        amp3 = 0.100 * (1 + hash_bytes[5] / 512)
        
        harmonics = []
        N = len(data)
        for k in range(N):
            harmonic_value = (
                amp1 * math.sin(2 * math.pi * freq1 * k / N) +
                amp2 * math.sin(2 * math.pi * freq2 * k / N) +
                amp3 * math.sin(2 * math.pi * freq3 * k / N)
            )
            harmonics.append(harmonic_value)
        
        return harmonics
    
    # ---------- AI Poisoning Methods ----------
    
    def apply_homoglyph_substitution(self, text: str, ratio: float = 0.25, content_hash: str = None) -> str:
        """Apply homoglyph character substitution for AI poisoning"""
        if content_hash:
            # Use content hash for deterministic randomness
            np.random.seed(int(content_hash[:8], 16) % (2**32))
        
        chars = list(text)
        n = len(chars)
        swap_count = int(ratio * n)
        
        if swap_count == 0:
            return text
            
        swap_positions = np.random.choice(n, swap_count, replace=False)
        
        for pos in swap_positions:
            ch = chars[pos].lower()
            if ch in HOMOGLYPHS:
                chars[pos] = HOMOGLYPHS[ch]
        
        return "".join(chars)
    
    def apply_multi_mask_injection(self, text: str, content_hash: str = None) -> str:
        """Apply zero-width space injection at geometrically determined positions"""
        n = len(text)
        
        # Generate injection positions using multiple masks
        golden_positions = self.golden_mask_positions(n)
        fibonacci_positions = self.fibonacci_mask_positions(n)
        harmonic_positions = self.harmonic_mask_positions(n)
        
        # Combine all mask positions
        injection_positions = set(golden_positions + fibonacci_positions + harmonic_positions)
        
        # Apply zero-width space injection
        zwsp = "\u200b"  # Zero-width space
        result = []
        
        for i, char in enumerate(text):
            result.append(char)
            if i in injection_positions:
                result.append(zwsp)
        
        return "".join(result)
    
    def apply_hybrid_text_poisoning(self, text: str, strength: float = 1.0, content_hash: str = None) -> str:
        """Apply hybrid AI poisoning combining multiple techniques"""
        if not content_hash:
            content_hash = hashlib.sha256(text.encode()).hexdigest()
        
        # Step 1: Homoglyph substitution
        homoglyph_ratio = 0.15 + (strength * 0.15)  # 0.15-0.30 range
        step1 = self.apply_homoglyph_substitution(text, homoglyph_ratio, content_hash)
        
        # Step 2: Multi-mask injection
        step2 = self.apply_multi_mask_injection(step1, content_hash)
        
        return step2

    def apply_invisible_armor_image_poisoning(self, image_data: bytes, strength: float = 1.0,
                                              focus_parameter: float = 3.5,
                                              frequency_strategy: str = 'auto') -> bytes:
        """
        Applies the full suite of Invisible Armor protections using the advanced Chimera Engine.
        This function is the core of the image protection service.
        """
        try:
            import tempfile
            # The armor engine currently requires file paths, so we use temporary files.
            with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_input:
                temp_input.write(image_data)
                temp_input_path = temp_input.name
            
            temp_output_path = temp_input_path + ".armored.png"

            armor_gen = ArmorGenerator()
            config = ArmorConfig() # Base config, specific parameters are passed below

            armor_gen.apply_to_image(
                image_path=temp_input_path,
                out_path=temp_output_path,
                cfg=config,
                return_metrics=False,
                # Pass all advanced parameters to the armor engine
                strength=strength,
                focus_parameter=focus_parameter,
                frequency_strategy=frequency_strategy
            )

            with open(temp_output_path, "rb") as f:
                armored_data = f.read()

            # Clean up temporary files
            os.remove(temp_input_path)
            os.remove(temp_output_path)
            
            return armored_data

        except Exception as e:
            print(f"[ERROR] Invisible Armor application failed: {e}.")
            return image_data # Return original data on failure to prevent data loss
    
    def apply_ultra_subtle_armor(self, image_data: bytes) -> bytes:
        """
        Apply ultra-subtle protection that is completely invisible to human eyes.
        Only modifies 0.01% of pixels by ±1 value.
        """
        try:
            img = Image.open(BytesIO(image_data))
            original_mode = img.mode
            original_format = img.format if img.format else "PNG"
            img_array = np.array(img).astype(np.float32)
            
            if img_array.ndim == 2:  # Grayscale
                h, w = img_array.shape
                channels = 1
            elif img_array.ndim == 3:  # RGB, RGBA
                h, w, channels = img_array.shape
            else:
                return image_data
            
            # Generate deterministic seed
            data_hash_int = int(hashlib.sha256(image_data).hexdigest()[:16], 16)
            rng = np.random.default_rng(data_hash_int)
            
            # Modify only 0.01% of pixels
            total_pixels = h * w
            pixels_to_modify = max(1, int(total_pixels * 0.0001))  # 0.01%
            
            # Select random pixel positions
            flat_indices = rng.choice(total_pixels, size=pixels_to_modify, replace=False)
            
            # Convert to 2D coordinates
            y_coords = flat_indices // w
            x_coords = flat_indices % w
            
            # Apply minimal changes
            modified_img_array = img_array.copy()
            
            for i in range(pixels_to_modify):
                y, x = y_coords[i], x_coords[i]
                change = rng.choice([-1, 1])
                
                if channels == 1:
                    modified_img_array[y, x] = np.clip(modified_img_array[y, x] + change, 0, 255)
                else:
                    # Modify only one channel per pixel
                    channel = rng.integers(0, channels)
                    modified_img_array[y, x, channel] = np.clip(
                        modified_img_array[y, x, channel] + change, 0, 255
                    )
            
            # Convert back to image
            modified_img_array = modified_img_array.astype(np.uint8)
            result_img = Image.fromarray(modified_img_array, mode=original_mode)
            
            # Save in original format
            output = BytesIO()
            if original_format == 'JPEG':
                result_img.save(output, format='JPEG', quality=100, optimize=True)
            else:
                result_img.save(output, format=original_format)
            
            return output.getvalue()
            
        except Exception as e:
            print(f"[ERROR] Ultra-subtle armor failed: {e}")
            return image_data

    def apply_subtle_geometric_watermark(self, data: bytes, fingerprint: GeometricFingerprint, watermark_text: str) -> bytes:
        """Applies a very subtle, almost imperceptible geometric watermark to the data bytes.
        This is NOT AI poisoning; it's minimal modification for watermarking purposes.
        """
        if not watermark_text or len(data) == 0: # If no watermark or empty data, return original
            return data

        modified_data = bytearray(data)
        watermark_hash = hashlib.sha256(watermark_text.encode('utf-8')).hexdigest()
        hash_int = int(watermark_hash[:8], 16)

        # Use a deterministic pseudo-random sequence for positions and values
        import random
        random_gen = random.Random(hash_int) # Seed with watermark hash

        # Use some features from the fingerprint for geometric distribution
        # Example: magnitudes of cmt_signature can guide where to place subtle changes
        cmt_magnitudes = [abs(c) for c in fingerprint.cmt_signature]
        if not cmt_magnitudes: cmt_magnitudes = [1.0] # Fallback for empty fingerprint signature

        # Determine number of bytes to subtly alter (very small percentage)
        # Max 0.1% of data or a fixed small number, to ensure subtlety
        num_modifications = min(len(data) // 1000, 100) # Max 100 bytes, or 0.1% of data
        if num_modifications == 0 and len(data) > 0: num_modifications = 1 # At least one if data exists

        # Select positions based on a combination of watermark hash and fingerprint features
        # Use a simple rolling hash combined with scaled fingerprint features to pick positions
        potential_positions = []
        for i in range(num_modifications * 5): # Generate more candidates than needed
            # Combine current index, hash_int, and a scaled fingerprint magnitude for position
            fp_value = cmt_magnitudes[i % len(cmt_magnitudes)]
            pos = (hash_int + i + int(fp_value * 1000)) % len(data)
            potential_positions.append(pos)
        
        # Select unique positions ensuring they are within data bounds
        chosen_positions = sorted(list(set(potential_positions)))[:num_modifications]
        
        for pos in chosen_positions:
            if pos < len(modified_data):
                # Apply a minimal, content-keyed modification (e.g., +/- 1 or 0)
                # The change value is also determined deterministically
                change_value = (random_gen.randint(-1, 1)) # -1, 0, or 1
                modified_data[pos] = (modified_data[pos] + change_value) % 256
        
        return bytes(modified_data)

    def apply_geometric_byte_poisoning(self, data: bytes, strength: float = 1.0) -> bytes:
        """Apply aggressive geometric byte perturbations.
        Changes are significant (-20 to +20) to maximize AI confusion.
        Strength controls the intensity and density of changes.
        """
        poisoned_data = bytearray(data)
        if len(data) == 0: return bytes(poisoned_data)

        golden_positions = self.golden_mask_positions(len(data))
        
        # Use deterministic random generator for reproducible poisoning
        data_hash_int = int(hashlib.sha256(data).hexdigest()[:8], 16)
        import random
        rng = random.Random(data_hash_int)

        # Determine the number of bytes to aggressively modify based on strength
        # Strength of 1.0 means ~37.5% of golden positions are affected (25% reduction).
        target_mod_count = int(len(golden_positions) * (strength * 0.375)) # Strength scales density, reduced by 25%
        target_mod_count = min(target_mod_count, len(data) // 2) # Cap at 50% of total data
        if target_mod_count == 0 and len(data) > 0: target_mod_count = len(data) // 10 # Ensure substantial changes

        # Randomly select positions to modify
        actual_positions_to_modify = rng.sample(golden_positions, min(target_mod_count, len(golden_positions)))
        
        # Apply multiple layers of strong perturbations
        for layer in range(3):
            layer_strength = strength * (1.0 + layer * 0.4)
            
            # Apply geometric patterns to golden positions
            for i, pos in enumerate(actual_positions_to_modify):
                if pos < len(poisoned_data):
                    # Create pattern-based perturbation (much stronger)
                    pattern_val = math.sin(i / 10.0 + layer * math.pi/3) * math.cos(i / 15.0 + layer * math.pi/4)
                    
                    # Apply aggressive changes (-15 to +15) - 25% reduction
                    change_value = int(pattern_val * 15 * layer_strength)
                    new_val = (poisoned_data[pos] + change_value) % 256
                    
                    # Apply additional random component (25% reduction)
                    random_boost = rng.randint(-4, 4)
                    new_val = (new_val + random_boost) % 256
                    
                    poisoned_data[pos] = new_val
        
        # Additional high-frequency pattern layer (25% reduction)
        for pos in range(0, len(poisoned_data), 100):  # Every 100th byte gets extra treatment
            if pos < len(poisoned_data):
                pattern_val = math.sin(pos / 25.0) * math.cos(pos / 30.0)
                change_value = int(pattern_val * 11.25 * strength)  # 25% less
                poisoned_data[pos] = (poisoned_data[pos] + change_value) % 256
        
        return bytes(poisoned_data)

    # ---------- AI De-Poisoning Methods ----------

    def remove_homoglyph_substitution(self, text: str) -> str:
        """Revert homoglyph substitutions."""
        chars = list(text)
        for i, ch in enumerate(chars):
            if ch in REVERSE_HOMOGLYPHS:
                chars[i] = REVERSE_HOMOGLYPHS[ch]
        return "".join(chars)

    def remove_multi_mask_injection(self, text: str) -> str:
        """Remove injected zero-width spaces."""
        return text.replace("\u200b", "")

    def remove_hybrid_text_poisoning(self, text: str) -> str:
        """Revert hybrid text poisoning."""
        # The order of reversal is important: remove ZWSP first, then fix homoglyphs.
        step1 = self.remove_multi_mask_injection(text)
        step2 = self.remove_homoglyph_substitution(step1)
        return step2


    
    # ---------- Core CMT Engine (Preserved) ----------
    
    def analyze_entropy_landscape(self, data: bytes, block_size: int = 64) -> List[Tuple[int, float]]:
        """Analyze entropy landscape of data"""
        regions = []
        
        for i in range(0, len(data), block_size):
            block = data[i:i+block_size]
            if len(block) == 0:
                continue
                
            freq_count = {}
            for byte in block:
                freq_count[byte] = freq_count.get(byte, 0) + 1
            
            total = len(block)
            entropy = 0.0
            for count in freq_count.values():
                p = count / total
                if p > 0:
                    entropy -= p * math.log2(p)
            
            normalized_entropy = entropy / 8.0 if entropy > 0 else 0.0
            regions.append((i, normalized_entropy))
        
        return regions
    
    def complex_encode(self, data: bytes, enhanced_phase: bool = True) -> List[complex]:
        """Enhanced complex encoding with corrected harmonic calculations"""
        if len(data) == 0:
            return [complex(0, 0)]
        
        values = list(data)
        N = len(values)
        encoded = []
        
        for k, value in enumerate(values):
            theta_k = 2 * math.pi * k / N
            
            phi_k = 0.0
            if enhanced_phase:
                for freq, amp in zip(HARMONIC_FREQUENCIES, HARMONIC_AMPLITUDES):
                    harmonic_arg = 2 * math.pi * freq * k / N
                    phi_k += amp * math.sin(harmonic_arg)
            
            total_phase = theta_k + phi_k
            normalized_value = value / 255.0
            z_k = normalized_value * cmath.exp(1j * total_phase)
            encoded.append(z_k)
        
        return encoded
    
    def apply_lens_function(self, z: complex, lens_type: LensFunction) -> complex:
        """Apply specified lens function to complex number"""
        try:
            if lens_type == LensFunction.GAMMA:
                if abs(z) > 50:
                    return complex(1e-12, 1e-12)
                result = gamma(z)
                if not np.isfinite(result):
                    return complex(1e-12, 1e-12)
                return complex(result)
                
            elif lens_type == LensFunction.AIRY:
                result = airy(z)[0]
                if not np.isfinite(result):
                    return complex(1e-12, 1e-12)
                return complex(result)
                
            elif lens_type == LensFunction.BESSEL:
                result = jv(0, z)
                if not np.isfinite(result):
                    return complex(1e-12, 1e-12)
                return complex(result)
                
        except (ValueError, OverflowError, RuntimeWarning):
            return complex(1e-12, 1e-12)
    
    def cmt_transform(self, z_encoded: List[complex], lens_type: LensFunction) -> List[complex]:
        """Enhanced Complexity Magnitude Transform"""
        cmt_result = []
        
        for z in z_encoded:
            F_z = self.apply_lens_function(z, lens_type)
            complexity_component = self.c1 * F_z
            magnitude_component = self.c2 * abs(z)
            phi_z = complexity_component + magnitude_component
            cmt_result.append(phi_z)
        
        return cmt_result
    
    def generate_lehi_harmonics(self, data: bytes) -> List[float]:
        """Generate LEHI harmonic patterns from data"""
        if len(data) == 0:
            return [0.0]
        
        values = np.array(list(data), dtype=float)
        
        if np.max(values) > np.min(values):
            normalized = 2 * (values - np.min(values)) / (np.max(values) - np.min(values)) - 1
        else:
            normalized = np.zeros_like(values)
        
        N = len(normalized)
        harmonics = np.zeros(N)
        
        for i, (freq, amp) in enumerate(zip(HARMONIC_FREQUENCIES, HARMONIC_AMPLITUDES)):
            for k in range(N):
                phase = 2 * math.pi * freq * k / N
                harmonics[k] += amp * normalized[k] * math.sin(phase + i * math.pi / 3)
        
        return harmonics.tolist()
    
    def calculate_srl_stability(self, data: bytes) -> float:
        """Calculate SRL stability with corrected formula"""
        if len(data) < 2:
            return 1.0
        
        values = list(data)
        diffs = [abs(values[i+1] - values[i]) for i in range(len(values)-1)]
        
        if not diffs:
            return 1.0
        
        max_diff = max(diffs)
        mean_diff = sum(diffs) / len(diffs)
        
        if mean_diff > 0:
            srl = max_diff / mean_diff
        else:
            srl = 1.0
        
        stability = 1.0 / (1.0 + srl)
        return stability
    
    def calculate_sefa_emergence(self, cmt_field: List[complex]) -> float:
        """Calculate SEFA with corrected correlation"""
        if len(cmt_field) < 2:
            return 0.5
        
        real_parts = [z.real for z in cmt_field]
        imag_parts = [z.imag for z in cmt_field]
        
        try:
            correlation_matrix = np.corrcoef(real_parts, imag_parts)
            correlation = abs(correlation_matrix[0, 1])
            
            if np.isnan(correlation):
                correlation = 0.0
                
        except ValueError:
            correlation = 0.0
        
        emergence = 1.0 - correlation
        return emergence
    
    def generate_lgrm_map(self, cmt_field: List[complex]) -> np.ndarray:
        """Generate Logic-Geometry Resolution Map matrix"""
        if len(cmt_field) == 0:
            return np.zeros((4, 4), dtype=complex)
        
        magnitudes = [abs(z) for z in cmt_field]
        phases = [cmath.phase(z) for z in cmt_field]
        
        lgrm = np.zeros((4, 4), dtype=complex)
        
        for i in range(4):
            for j in range(4):
                idx = (i * 4 + j) % len(cmt_field)
                
                magnitude = magnitudes[idx] if idx < len(magnitudes) else 1.0
                phase = phases[idx] if idx < len(phases) else 0.0
                
                real_part = magnitude * math.cos(phase + i * math.pi / 4)
                imag_part = magnitude * math.sin(phase + j * math.pi / 4)
                
                lgrm[i, j] = complex(real_part, imag_part)
        
        return lgrm
    
    def reconstruct_holographic_field(self, cmt_views: List[List[complex]]) -> List[complex]:
        """Reconstruct holographic interference field from multiple CMT views"""
        if not cmt_views or len(cmt_views[0]) == 0:
            return [complex(0, 0)]
        
        field_length = len(cmt_views[0])
        holographic_field = []
        
        for i in range(field_length):
            interference = complex(0, 0)
            
            for view in cmt_views:
                if i < len(view):
                    interference += view[i]
            
            if len(cmt_views) > 0:
                interference /= len(cmt_views)
            
            holographic_field.append(interference)
        
        return holographic_field

def apply_integrated_ai_poisoning(data: bytes, protection_type: str, strength: float = 1.0,
                                 file_extension: str = "bin", fingerprint: Optional[GeometricFingerprint] = None,
                                 watermark_text: Optional[str] = None) -> bytes:
    """
    Applies a non-destructive, reversible "poisoning" effect based on file type.
    It now also handles the embedding of an optional watermark.
    """
    engine = InformationGeometryEngine()
    content_hash = hashlib.sha256(data).hexdigest()

    # --- Text-Based Formats ---
    text_formats = ['txt', 'md', 'json', 'csv', 'html', 'xml', 'htm', 'ipynb', 'eml', 'msg', 'py']
    if file_extension in text_formats and protection_type != "invisible_armor":
        try:
            text_data = data.decode('utf-8')
            poisoned_text = engine.apply_hybrid_text_poisoning(text_data, strength, content_hash)
            final_data = poisoned_text.encode('utf-8')
        except UnicodeDecodeError:
            # If not valid text, treat as binary.
            final_data = bytearray(data)
    
    # --- Image Formats with Invisible Armor ---
    elif file_extension in ["png", "jpg", "jpeg"] and protection_type == "invisible_armor":
        final_data = engine.apply_invisible_armor_image_poisoning(data, strength)
    
    # --- Binary/Other Formats ---
    else:
        # For binary formats or when protection_type doesn't match specific handling
        final_data = engine.apply_geometric_byte_poisoning(data, strength)

    # --- Watermark Embedding ---
    # The watermark is applied *after* any initial poisoning.
    if watermark_text and fingerprint:
        return engine.apply_subtle_geometric_watermark(bytes(final_data), fingerprint, watermark_text)
    elif watermark_text:
        return embed_watermark(bytes(final_data), watermark_text)
    
    return bytes(final_data)

def create_ghostprint_fingerprint(data: bytes, creator: str, protection_level: str, disclaimer: Optional[str] = None) -> GeometricFingerprint:
    """Create complete Ghostprint fingerprint, now with an optional AI disclaimer."""
    engine = InformationGeometryEngine()
    
    # Core fingerprint generation
    entropy_regions = engine.analyze_entropy_landscape(data)
    z_encoded = engine.complex_encode(data, enhanced_phase=True)
    cmt_signature = engine.cmt_transform(z_encoded, LensFunction.BESSEL)
    lehi_pattern = engine.generate_lehi_harmonics(data)
    srl_stability = engine.calculate_srl_stability(data)
    sefa_emergence = engine.calculate_sefa_emergence(cmt_signature)
    lgrm_map = engine.generate_lgrm_map(cmt_signature)
    
    cmt_views = [
        engine.cmt_transform(z_encoded, LensFunction.BESSEL),
        engine.cmt_transform(z_encoded, LensFunction.GAMMA),
        engine.cmt_transform(z_encoded, LensFunction.AIRY)
    ]
    holographic_field = engine.reconstruct_holographic_field(cmt_views)
    
    # Enhanced metadata with AI poisoning capabilities
    metadata = {
        "creator": creator,
        "timestamp": datetime.now().isoformat(),
        "protection_level": protection_level,
        "data_hash": hashlib.sha256(data).hexdigest(),
        "entropy_regions": len(entropy_regions),
        "cmt_views": len(cmt_views),
        "srl_stability": float(srl_stability),
        "sefa_emergence": float(sefa_emergence),
        "algorithm_version": "Ghostprint-1.0-2025",
        "ai_poisoning_enabled": True,
        "geometry_mask_version": "G-F-H-v1.0",
    }
    if disclaimer:
        metadata["ai_disclaimer"] = disclaimer
    
    fingerprint = GeometricFingerprint(
        cmt_signature=cmt_signature,
        lehi_pattern=lehi_pattern,
        lgrm_map=lgrm_map,
        holographic_field=holographic_field,
        srl_stability=srl_stability,
        sefa_emergence=sefa_emergence,
        metadata=metadata
    )
    
    return fingerprint

def embed_metadata_stamp(data: bytes, metadata: Dict, file_extension: str) -> bytes:
    """
    Embeds metadata into a file as a 'digital rubber stamp' in a format-aware and non-destructive way.
    """
    metadata_str = json.dumps(metadata) # Use compact JSON for metadata fields

    # --- Image Formats (Safe Metadata Embedding) ---
    if file_extension == 'png':
        try:
            img = Image.open(BytesIO(data))
            # Ensure we're working with a valid image mode
            if img.mode not in ['RGB', 'RGBA', 'L', 'P']:
                img = img.convert('RGBA')
            
            info = PngImagePlugin.PngInfo()
            info.add_text("GhostprintFingerprint", metadata_str)
            output = BytesIO()
            img.save(output, format='PNG', pnginfo=info)
            return output.getvalue()
        except Exception as e:
            print(f"Error embedding PNG metadata: {e}")
            # For PNG files that can't be processed, append metadata as comment at end
            # PNG files end with IEND chunk, we can add a comment after
            return data + b'\n<!-- GHOSTPRINT_METADATA:' + metadata_str.encode('utf-8') + b' -->\n'

    if file_extension in ['jpg', 'jpeg']:
        try:
            img = Image.open(BytesIO(data))
            exif_data = img.getexif()
            exif_data[0x9286] = metadata_str.encode('utf-8') # UserComment EXIF tag
            output = BytesIO()
            img.save(output, format='JPEG', exif=exif_data)
            return output.getvalue()
        except Exception as e:
            print(f"Error embedding JPEG metadata: {e}")
            return data # Return original data on failure

    # --- Structured Text Formats ---
    if file_extension == 'json':
        try:
            json_data = json.loads(data.decode('utf-8'))
            json_data['_ghostprint_fingerprint'] = metadata
            return json.dumps(json_data, indent=2).encode('utf-8')
        except (json.JSONDecodeError, UnicodeDecodeError):
            pass # Fallback to text append

    # --- All Other Text-Like Formats (and Fallback) ---
    stamp = f"\n\n--- GHOSTPRINT FINGERPRINT ---\n{json.dumps(metadata, indent=2)}\n--- END GHOSTPRINT ---\n"
    return data + stamp.encode('utf-8')

def create_lite_fingerprinted_file(data: bytes, disclaimer: str, watermark_text: str, creator: str, filename: str) -> bytes:
    """
    Creates a 'lite' fingerprinted file by embedding a metadata stamp (with AI disclaimer)
    and a separate hidden watermark for tamper-proofing.
    """
    engine = InformationGeometryEngine()
    
    # 1. Create the full fingerprint, including the AI disclaimer.
    file_ext = filename.split('.')[-1] if '.' in filename else 'bin'
    fingerprint = create_ghostprint_fingerprint(data, creator, "Lite", disclaimer=disclaimer)
    
    # 2. Apply new robust watermarking for all file types if watermark_text is provided
    if watermark_text:
        # Use the subtle geometric watermark for all file types. It's more robust than XOR.
        modified_data_bytes = engine.apply_subtle_geometric_watermark(data, fingerprint, watermark_text)
        watermark_method = "cmt_geometric" # New method name
    else:
        modified_data_bytes = data
        watermark_method = "none"
    
    # 3. Add watermark info to fingerprint metadata
    fingerprint.metadata["watermark_method"] = watermark_method
    fingerprint.metadata["watermark_applied"] = watermark_text is not None
    
    # 4. Embed the metadata as a visible "stamp".
    stamped_data = embed_metadata_stamp(modified_data_bytes, fingerprint.metadata, file_ext)
    
    # 5. Calculate final file hash for tamper detection
    # The content_hash should be of the data *with* the watermark but *without* the metadata stamp.
    content_to_hash = modified_data_bytes
    
    # Add tamper detection hash to metadata
    fingerprint.metadata["content_hash"] = hashlib.sha256(content_to_hash).hexdigest()
    
    # Re-embed metadata with the content hash
    stamped_data = embed_metadata_stamp(modified_data_bytes, fingerprint.metadata, file_ext)
    
    return stamped_data



def secure_encrypt_with_passphrase(data: bytes, user_passphrase: str, random_salt: bytes) -> bytes:
    """
    Encrypts data using AES-256-GCM with a user-provided passphrase.
    Uses a random salt to ensure unique encryption keys for identical files.
    SECURITY: The passphrase is NEVER stored anywhere.
    """
    # Derive AES key from user passphrase using PBKDF2-HMAC-SHA256
    iterations = get_config("pbkdf2_iterations")  # Should be at least 100,000
    key_length = 32  # AES-256 requires a 32-byte key
    aes_key = hashlib.pbkdf2_hmac('sha256', user_passphrase.encode('utf-8'), random_salt, iterations, key_length)
    
    # Encrypt with AES-256-GCM
    aesgcm = AESGCM(aes_key)
    nonce = os.urandom(12)  # GCM standard nonce size is 12 bytes
    encrypted_data = aesgcm.encrypt(nonce, data, None)
    
    # Return: nonce + encrypted_data
    return nonce + encrypted_data

def create_ghost_file_with_ai_poisoning(data: bytes, fingerprint: GeometricFingerprint, filename: str,
                                       user_passphrase: str, watermark_text: Optional[str] = None,
                                       create_zip: bool = False) -> bytes:
    """
    Create .ghost file with AES-256-GCM encryption using a user-provided passphrase.
    The original data is NOT poisoned - only the encrypted container is.
    SECURITY: The passphrase is never stored in the file.
    """
    # If requested, create a ZIP file first
    if create_zip:
        import zipfile
        from io import BytesIO
        
        zip_buffer = BytesIO()
        with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zf:
            # Add the file to the ZIP
            zf.writestr(filename, data)
        
        # Use the ZIP data as the data to encrypt
        data_to_encrypt = zip_buffer.getvalue()
        original_filename = filename
        filename = filename + ".zip"  # Update filename to reflect ZIP
    else:
        data_to_encrypt = data
        original_filename = filename

    # Store watermark info in the fingerprint
    fingerprint.metadata["has_watermark"] = bool(watermark_text)
    if watermark_text:
        fingerprint.metadata["watermark_text"] = watermark_text

    # Compress the data (NOT poisoned)
    compressed_data = lzma.compress(data_to_encrypt)
    
    # SECURITY FIX: Generate a truly random salt for this encryption
    random_salt = os.urandom(32)  # 256-bit random salt
    
    # Encrypt using the secure passphrase-based encryption
    encrypted_data = secure_encrypt_with_passphrase(compressed_data, user_passphrase, random_salt)
    
    # Create metadata (treated as public information)
    file_ext = original_filename.split('.')[-1] if '.' in original_filename else 'bin'
    metadata = {
        "fingerprint_metadata": fingerprint.metadata,
        "lgrm_map_hash": hashlib.sha256(str(fingerprint.lgrm_map.tolist()).encode()).hexdigest(),
        "holographic_field_hash": hashlib.sha256(str(fingerprint.holographic_field).encode()).hexdigest(),
        "original_filename": original_filename,
        "stored_filename": filename,
        "original_extension": file_ext,
        "was_zipped": create_zip,
        "encryption_salt": base64.b64encode(random_salt).decode('utf-8'),  # Salt is safe to store
        "kdf_iterations": get_config("pbkdf2_iterations"),  # For transparency
        "container_poisoning": {
            "enabled": True,
            "type": "encrypted_container",
            "note": "Original data is NOT poisoned, only the encrypted container"
        }
    }
    
    # Assemble the ghost file
    header = b"GHOSTPRT0200"  # New version for security update
    metadata_json = json.dumps(metadata).encode('utf-8')
    metadata_length = len(metadata_json).to_bytes(4, 'big')
    
    ghost_file = header + metadata_length + metadata_json + encrypted_data
    
    return ghost_file

def secure_decrypt_with_passphrase(encrypted_data: bytes, user_passphrase: str, random_salt: bytes) -> bytes:
    """
    Decrypts data using AES-256-GCM with a user-provided passphrase.
    The same passphrase and salt used for encryption must be provided.
    """
    # Derive the same AES key using the same parameters
    iterations = get_config("pbkdf2_iterations")
    key_length = 32
    aes_key = hashlib.pbkdf2_hmac('sha256', user_passphrase.encode('utf-8'), random_salt, iterations, key_length)
    
    # Extract nonce and ciphertext
    nonce = encrypted_data[:12]
    ciphertext = encrypted_data[12:]
    
    # Decrypt with AES-256-GCM
    aesgcm = AESGCM(aes_key)
    try:
        decrypted_data = aesgcm.decrypt(nonce, ciphertext, None)
        return decrypted_data
    except Exception:
        raise ValueError("Decryption failed - invalid passphrase or corrupted data")

def read_ghost_file_with_poisoning(ghost_data: bytes, user_passphrase: str, auto_unzip: bool = True) -> Tuple[bytes, Dict]:
    """Read and decrypt .ghost file using a user-provided passphrase."""
    # Check for both old and new file formats
    if ghost_data.startswith(b"GHOSTPRT0200"):
        # New secure format
        header_length = 12
    elif ghost_data.startswith(b"GHOSTPRT0100"):
        # Legacy format - continue to support
        header_length = 12
    else:
        raise ValueError("Invalid ghost file format")

    metadata_length = int.from_bytes(ghost_data[header_length:header_length+4], 'big')
    metadata_start = header_length + 4
    metadata_end = metadata_start + metadata_length
    metadata = json.loads(ghost_data[metadata_start:metadata_end].decode('utf-8'))
    
    encrypted_data = ghost_data[metadata_end:]
    
    # Extract the salt from metadata
    encryption_salt = base64.b64decode(metadata["encryption_salt"])
    
    # Decrypt the compressed data using the user's passphrase
    compressed_data = secure_decrypt_with_passphrase(encrypted_data, user_passphrase, encryption_salt)
    
    try:
        decrypted_data = lzma.decompress(compressed_data)
    except lzma.LZMAError:
        raise ValueError("Decryption failed - data is corrupted")

    # The original data was NOT poisoned, so no need to remove poisoning
    # Check if we need to unzip
    if auto_unzip and metadata.get("was_zipped", False):
        import zipfile
        from io import BytesIO
        
        try:
            with zipfile.ZipFile(BytesIO(decrypted_data), 'r') as zf:
                # Get the first file in the ZIP
                file_list = zf.namelist()
                if file_list:
                    # Extract the original file
                    original_filename = metadata.get("original_filename", file_list[0])
                    # Try to find the exact file, or use the first one
                    if original_filename in file_list:
                        decrypted_data = zf.read(original_filename)
                    else:
                        decrypted_data = zf.read(file_list[0])
        except Exception as e:
            # If unzipping fails, return the ZIP file itself
            print(f"[DEBUG] Auto-unzip failed: {e}. Returning ZIP file.")

    return decrypted_data, metadata

def remove_integrated_ai_poisoning(data: bytes, poison_type: str, strength: float,
                                   file_extension: str, fingerprint_meta: Dict) -> bytes:
    """
    Reverses the non-destructive poisoning to restore the original data with 100% fidelity.
    """
    engine = InformationGeometryEngine()
    
    # The watermark is the outermost layer, so it must be removed first.
    watermark_to_remove = fingerprint_meta.get("watermark_text")
    if watermark_to_remove:
        data_without_watermark = embed_watermark(data, watermark_to_remove) # XORs it out
    else:
        data_without_watermark = data

    # --- Text-Based Formats ---
    text_formats = ['txt', 'md', 'json', 'csv', 'html', 'xml', 'htm', 'ipynb', 'eml', 'msg']
    if file_extension in text_formats:
        try:
            text_data = data_without_watermark.decode('utf-8')
            clean_text = engine.remove_hybrid_text_poisoning(text_data)
            return clean_text.encode('utf-8')
        except UnicodeDecodeError:
            return data_without_watermark
    
    return data_without_watermark


# Analysis and verification functions
def analyze_ai_poisoning_effectiveness(original_data: bytes, poisoned_data: bytes) -> Dict[str, float]:
    """Analyze effectiveness of AI poisoning"""
    def file_entropy(data: bytes) -> float:
        if not data:
            return 0.0
        freq = np.bincount(np.frombuffer(data, dtype=np.uint8), minlength=256)
        probs = freq / len(data)
        probs = probs[probs > 0]
        return -np.sum(probs * np.log2(probs))
    
    def compressibility(data: bytes) -> float:
        if not data:
            return 1.0
        compressed = lzma.compress(data)
        return len(compressed) / len(data)
    
    def jensen_shannon_divergence(p, q):
        p = np.array(p, dtype=float)
        q = np.array(q, dtype=float)
        p_sum = p.sum()
        q_sum = q.sum()
        
        if p_sum == 0 or q_sum == 0:
            return 0.0

        p /= p_sum
        q /= q_sum
        
        m = (p + q) / 2
        
        # Add epsilon to avoid division by zero
        epsilon = 1e-12
        
        kl_pm = np.sum(np.where(p != 0, p * np.log2(p / (m + epsilon) + epsilon), 0))
        kl_qm = np.sum(np.where(q != 0, q * np.log2(q / (m + epsilon) + epsilon), 0))
        
        jsd = (kl_pm + kl_qm) / 2
        return jsd if np.isfinite(jsd) else 0.0
    
    # Calculate metrics
    orig_entropy = file_entropy(original_data)
    pois_entropy = file_entropy(poisoned_data)
    orig_compression = compressibility(original_data)
    pois_compression = compressibility(poisoned_data)
    
    orig_histogram = np.bincount(np.frombuffer(original_data, dtype=np.uint8), minlength=256)
    pois_histogram = np.bincount(np.frombuffer(poisoned_data, dtype=np.uint8), minlength=256)
    jsd = jensen_shannon_divergence(orig_histogram, pois_histogram)
    
    min_length = min(len(original_data), len(poisoned_data))
    byte_changes = sum(1 for i in range(min_length) if original_data[i] != poisoned_data[i])
    change_percentage = (byte_changes / min_length) * 100 if min_length > 0 else 0
    
    return {
        "jensen_shannon_divergence": float(jsd),
        "entropy_original": float(orig_entropy),
        "entropy_poisoned": float(pois_entropy),
        "entropy_delta": float(abs(orig_entropy - pois_entropy)),
        "compression_original": float(orig_compression),
        "compression_poisoned": float(pois_compression),
        "compression_delta": float(abs(orig_compression - pois_compression)),
        "byte_changes": int(byte_changes),
        "change_percentage": float(change_percentage),
        "ai_confusion_score": float(jsd * abs(orig_entropy - pois_entropy) * 10)
    }



def embed_watermark(data: bytes, watermark_text: str) -> bytes:
    """
    Embeds a custom watermark text into the data using a reversible XOR process
    at geometrically determined positions. A sentinel is added to make it detectable.
    """
    if not watermark_text:
        return data

    sentinel = b"GPW::"
    watermark_bytes = sentinel + watermark_text.encode('utf-8')
    data_array = bytearray(data)
    engine = InformationGeometryEngine()
    positions = engine.golden_mask_positions(len(data_array))

    for i, byte_to_embed in enumerate(watermark_bytes):
        pos_index = i % len(positions)
        target_pos = positions[pos_index]
        data_array[target_pos] ^= byte_to_embed

    return bytes(data_array)

def verify_watermark(data: bytes, expected_watermark: str, file_extension: str) -> bool:
    """
    Verifies watermark and detects tampering by checking content hash.
    """
    if not expected_watermark:
        return False

    # 1. Extract metadata
    metadata_stamp = extract_metadata_stamp(data, file_extension)
    if not metadata_stamp:
        return False

    # 2. Check watermark method
    watermark_method = metadata_stamp.get("watermark_method", "none")
    
    if watermark_method == "cmt_geometric":
        try:
            # To verify, we must reconstruct the original data, then re-apply the expected watermark,
            # and see if the hash matches the file's content hash.

            # a. Reconstruct the original data by removing the metadata stamp from the current file data.
            #    The result is the watermarked content.
            stamp_marker = f"\n\n--- GHOSTPRINT FINGERPRINT ---".encode('utf-8')
            stamp_pos = data.rfind(stamp_marker)
            if stamp_pos != -1:
                watermarked_content = data[:stamp_pos]
            else:
                # If no stamp, we can't proceed with this method.
                return False

            # b. We can't easily reverse the geometric watermark. Instead, we re-create it.
            #    To do this, we need the *original* unwatermarked data, which we don't have.
            #    However, we have its hash in the metadata (`data_hash`).
            #    The `content_hash` in the metadata is the hash of the watermarked content.
            
            # c. We can verify the integrity of the watermarked content directly.
            current_content_hash = hashlib.sha256(watermarked_content).hexdigest()
            expected_content_hash = metadata_stamp.get("content_hash")

            if current_content_hash != expected_content_hash:
                 # This means the file content (excluding metadata) has been tampered with.
                return False

            # d. Since we can't reverse the watermark, we can't prove the user provided the *correct* one.
            #    But we can prove the file is authentic to the metadata. For now, this is a sufficient check.
            #    A more advanced verification would involve storing enough info to reconstruct the watermark.
            #    For now, if the content hash is valid, we assume the watermark is correct.
            #    This is a design decision for "lite" fingerprinting.
            return True

        except Exception as e:
            print(f"[DEBUG] CMT Geometric watermark verification error: {e}")
            return False

    elif watermark_method == "geometric": # This is for the old image method
        return verify_content_integrity(data, metadata_stamp, file_extension)
    
    else: # Handles "none" and legacy "xor"
        return metadata_stamp.get("watermark_applied", False)


def verify_content_integrity(data: bytes, metadata: Dict, file_extension: str) -> bool:
    """
    Verifies that the content hasn't been tampered with by checking hashes.
    """
    if not metadata.get("content_hash"):
        return False
    
    # Extract the content (excluding metadata)
    stamp_marker = f"\n\n--- GHOSTPRINT FINGERPRINT ---".encode('utf-8')
    stamp_pos = data.rfind(stamp_marker)
    
    if stamp_pos != -1:
        content_to_hash = data[:stamp_pos]
    else:
        # If no stamp, the data is the content
        content_to_hash = data

    # For images, the metadata is embedded differently and this method won't work.
    # We rely on PIL to handle image integrity.
    if file_extension.lower() in ['png', 'jpg', 'jpeg']:
        try:
            # The act of opening it and extracting metadata is a form of integrity check
            img_metadata = extract_metadata_stamp(data, file_extension)
            return img_metadata is not None
        except Exception:
            return False

    # Calculate hash of current content
    current_hash = hashlib.sha256(content_to_hash).hexdigest()
    stored_hash = metadata.get("content_hash")
    
    if current_hash != stored_hash:
        print(f"[DEBUG] Content integrity check failed!")
        print(f"[DEBUG] Expected hash: {stored_hash}")
        print(f"[DEBUG] Current hash: {current_hash}")
        return False
    
    return True


def verify_ghost_file(ghost_data: bytes) -> Optional[Dict]:
    """
    Comprehensively verifies and extracts metadata from .ghost files.
    Supports both legacy (0100) and secure (0200) formats.
    """
    try:
        # Check file format version
        if ghost_data.startswith(b"GHOSTPRT0200"):
            version = "2.0-SECURE"
            header_length = 12
        elif ghost_data.startswith(b"GHOSTPRT0100"):
            version = "1.0-LEGACY"
            header_length = 12
        else:
            return None
        
        # Extract metadata
        metadata_length = int.from_bytes(ghost_data[header_length:header_length+4], 'big')
        metadata_start = header_length + 4
        metadata_end = metadata_start + metadata_length
        metadata = json.loads(ghost_data[metadata_start:metadata_end].decode('utf-8'))
        
        # Enhance metadata with verification info
        verification_info = {
            "ghost_file_version": version,
            "file_size_bytes": len(ghost_data),
            "metadata_size_bytes": metadata_length,
            "encrypted_data_size_bytes": len(ghost_data) - metadata_end,
            "verification_timestamp": datetime.now().isoformat()
        }
        
        # Add verification info to the metadata
        metadata["_verification_info"] = verification_info
        
        return metadata
        
    except (IndexError, json.JSONDecodeError, KeyError) as e:
        return None

def extract_metadata_stamp(data: bytes, file_extension: str) -> Optional[Dict]:
    """
    Extracts a metadata stamp from a file using format-aware and non-destructive methods.
    """
    # --- Image Formats ---
    if file_extension == 'png':
        try:
            img = Image.open(BytesIO(data))
            # Check PNG text chunks
            metadata_str = img.info.get("GhostprintFingerprint")
            if metadata_str:
                return json.loads(metadata_str)
            
            # Also check for our custom protection metadata
            protection_str = img.info.get("GhostprintProtection")
            if protection_str:
                metadata = json.loads(protection_str)
                # Add a clear type indicator for the frontend
                metadata['_ghostprint_type'] = 'invisible_armor'
                return metadata
        except Exception as e:
            print(f"[DEBUG] PNG metadata extraction error: {e}")
            pass
        
        # Try to extract metadata from our custom gpAI chunk (used by Invisible Armor)
        try:
            # Look for the custom gpAI chunk in the PNG file
            import struct
            
            # PNG files start with signature
            if data[:8] != b'\x89PNG\r\n\x1a\n':
                return None
                
            # Parse PNG chunks
            pos = 8  # Skip PNG signature
            
            while pos < len(data) - 12:  # Need at least 12 bytes for chunk
                # Read chunk length (4 bytes)
                chunk_length = struct.unpack('>I', data[pos:pos+4])[0]
                pos += 4
                
                # Read chunk type (4 bytes)
                chunk_type = data[pos:pos+4]
                pos += 4
                
                # Check if this is our custom gpAI chunk
                if chunk_type == b'gpAI':
                    # This chunk contains compressed armored data
                    # The metadata might be stored separately, so check for text chunks
                    print("[DEBUG] Found gpAI chunk in PNG - this is an Invisible Armor protected image")
                    
                    # Return a special metadata indicating this is an armored PNG
                    return {
                        "protection_type": "invisible_armor_png",
                        "has_gpAI_chunk": True,
                        "chunk_size": chunk_length,
                        "message": "This PNG contains Invisible Armor protection with embedded AI-confusing data"
                    }
                
                # Skip chunk data and CRC
                pos += chunk_length + 4
                
                # Check for IEND chunk
                if chunk_type == b'IEND':
                    break
                    
        except Exception as e:
            print(f"[DEBUG] PNG chunk parsing error: {e}")
            pass

    if file_extension in ['jpg', 'jpeg']:
        try:
            img = Image.open(BytesIO(data))
            exif_data = img.getexif()
            metadata_str = exif_data.get(0x9286) # UserComment EXIF tag
            if metadata_str:
                return json.loads(metadata_str.decode('utf-8'))
        except Exception:
            return None

    # --- Structured Text Formats ---
    if file_extension == 'json':
        try:
            json_data = json.loads(data.decode('utf-8'))
            return json_data.get('_ghostprint_fingerprint')
        except (json.JSONDecodeError, UnicodeDecodeError):
            pass # Fallback to text append

    # Check for PNG comment fallback
    if file_extension == 'png':
        # Check if metadata was appended as comment
        try:
            comment_marker = b'<!-- GHOSTPRINT_METADATA:'
            comment_end = b' -->'
            if comment_marker in data:
                start = data.rfind(comment_marker) + len(comment_marker)
                end = data.rfind(comment_end)
                if start < end:
                    metadata_str = data[start:end].decode('utf-8')
                    return json.loads(metadata_str)
        except Exception as e:
            print(f"[DEBUG] PNG comment metadata extraction error: {e}")
            pass
    
    # --- All Other Text-Like Formats (and Fallback) ---
    # Try to decode as text (including PDFs which might have text appended)
    try:
        # For PDFs and binary files, try to find the metadata at the end
        if file_extension in ['pdf', 'png', 'jpg', 'jpeg']:
            # Try to find the metadata in the last part of the file
            tail_size = min(10000, len(data))  # Check last 10KB
            tail_data = data[-tail_size:]
            try:
                text_data = tail_data.decode('utf-8', errors='ignore')
            except:
                text_data = data.decode('utf-8', errors='ignore')
        else:
            text_data = data.decode('utf-8')
            
        start_sentinel = "--- GHOSTPRINT FINGERPRINT ---"
        end_sentinel = "--- END GHOSTPRINT ---"
        start_index = text_data.rfind(start_sentinel)
        end_index = text_data.rfind(end_sentinel)
        
        if start_index != -1 and end_index > start_index:
            metadata_str = text_data[start_index + len(start_sentinel):end_index].strip()
            return json.loads(metadata_str)
    except Exception as e:
        print(f"[DEBUG] Error extracting metadata stamp: {e}")
        pass
        
    return None

def create_preview_safe_armor(image_data: bytes, creator: str,
                            custom_watermark_text: Optional[str] = None,
                            ai_disclaimer_text: Optional[str] = None,
                            strength: float = 1.0) -> Tuple[bytes, Dict[str, Any]]:
    """
    Creates a special dual-purpose image file:
    1. The main image data is completely untouched (perfect for OS previews)
    2. Embeds heavily armored data in metadata/alternate streams
    3. When AI tries to process the file, it encounters the armored version
    
    This is achieved by:
    - For PNG: Using custom chunks to store armored data
    - For JPEG: Using APP segments to store armored data
    - The OS preview shows the clean image, but AI processing hits the armor
    """
    engine = InformationGeometryEngine()
    
    # 1. Create fingerprint
    fingerprint = create_ghostprint_fingerprint(
        data=image_data,
        creator=creator,
        protection_level="preview_safe_armor",
        disclaimer=ai_disclaimer_text
    )
    
    # 2. Create heavily armored version for AI confusion
    is_jpeg = image_data.startswith(b'\xff\xd8\xff')
    is_png = image_data.startswith(b'\x89PNG\r\n\x1a\n')
    
    # Use ultra-subtle armoring for human invisibility
    if is_jpeg:
        armored_data = engine.apply_ultra_subtle_armor(image_data)
    elif is_png:
        armored_data = engine.apply_ultra_subtle_armor(image_data)
    else:
        armored_data = engine.apply_ultra_subtle_armor(image_data)
    
    # 3. Create the special format
    if is_png:
        try:
            # For PNG, we can use custom chunks
            output = BytesIO()
            
            # First, save the image with metadata using PIL
            img = Image.open(BytesIO(image_data))
            
            # Create metadata for embedding
            metadata = {
                "creator": creator,
                "timestamp": datetime.now().isoformat(),
                "protection_level": "preview_safe_armor",
                "protection_type": "dual_data_streams",
                "original_hash": hashlib.sha256(image_data).hexdigest(),
                "armored_hash": hashlib.sha256(armored_data).hexdigest(),
                "watermark_embedded": False,  # Watermarking disabled for images
                "ai_disclaimer": ai_disclaimer_text,
                "format_preserved": False if is_jpeg else True,
                "format_conversion": "JPEG_to_PNG" if is_jpeg else "none",
                "human_visibility": "perfect_original_preview",
                "ai_protection": "hidden_armor_stream",
                "armor_location": "custom_chunks",
                "ai_confusion_level": "extreme",
                "crc_trap": True
            }
            
            # Add metadata as PNG text chunk
            info = PngImagePlugin.PngInfo()
            info.add_text("GhostprintProtection", json.dumps(metadata))
            
            # Save with metadata but using the ARMORED IMAGE as pixels
            # First create an armored image for the pixel data
            armored_img = Image.open(BytesIO(armored_data))
            
            temp_output = BytesIO()
            armored_img.save(temp_output, format='PNG', pnginfo=info)
            
            # Now rebuild the PNG with our custom chunk
            temp_output.seek(0)
            png_data = temp_output.read()
            
            # Write PNG signature
            output.write(b'\x89PNG\r\n\x1a\n')
            
            # Parse and copy chunks from the saved PNG
            pos = 8  # Skip PNG signature
            chunks_written = []
            
            while pos < len(png_data) - 12:
                # Read chunk length and type
                chunk_length = int.from_bytes(png_data[pos:pos+4], 'big')
                chunk_type = png_data[pos+4:pos+8]
                
                # Don't write IEND yet
                if chunk_type == b'IEND':
                    break
                    
                # Write this chunk
                chunk_data = png_data[pos:pos+12+chunk_length]
                output.write(chunk_data)
                chunks_written.append(chunk_type)
                
                pos += 12 + chunk_length
            
            # Add custom chunk with armored data (before IEND)
            # PNG custom chunk: gpAI (Ghostprint AI Protection)
            chunk_type = b'gpAI'
            
            # Compress armored data
            import zlib
            compressed_armor = zlib.compress(armored_data, 9)
            
            # Create chunk
            chunk_data = compressed_armor
            chunk_length = len(chunk_data).to_bytes(4, 'big')
            
            # Calculate CRC
            import struct
            crc = zlib.crc32(chunk_type + chunk_data) & 0xffffffff
            chunk_crc = struct.pack('>I', crc)
            
            # Write custom chunk
            output.write(chunk_length)
            output.write(chunk_type)
            output.write(chunk_data)
            output.write(chunk_crc)
            
            # Write IEND chunk
            output.write(b'\x00\x00\x00\x00IEND\xaeB`\x82')
            
            protected_data = output.getvalue()
            
        except Exception as e:
            print(f"[ERROR] Preview-safe PNG armor failed: {e}")
            # Fallback to clean image
            protected_data = image_data
            
    elif is_jpeg:
        try:
            # Convert JPEG to PNG first, then apply PNG armor
            print("[INFO] Converting JPEG to PNG for better armor compatibility...")
            
            # Open JPEG image
            img = Image.open(BytesIO(image_data))
            
            # Convert to RGB if needed (remove alpha channel if present)
            if img.mode not in ('RGB', 'L'):
                img = img.convert('RGB')
            
            # Save as PNG
            png_output = BytesIO()
            img.save(png_output, format='PNG', optimize=True)
            png_data = png_output.getvalue()
            
            # Apply armor to the converted PNG with adjustable strength
            armored_data = engine.apply_invisible_armor_image_poisoning(png_data, strength=strength)
            
            # Use the armored image directly with metadata
            img = Image.open(BytesIO(armored_data))
            original_mode = img.mode
            info = PngImagePlugin.PngInfo()
            
            # Add standard metadata
            info.add_text("Creator", creator)
            if custom_watermark_text:
                info.add_text("GhostprintWatermark", custom_watermark_text)
            if ai_disclaimer_text:
                info.add_text("AIDisclaimer", ai_disclaimer_text)
            
            info.add_text("GhostprintProtected", "true")
            info.add_text("ProtectionTimestamp", datetime.now().isoformat())
            info.add_text("ProtectionStrength", str(strength))
            info.add_text("OriginalFormat", "JPEG")
            
            # Save with metadata
            temp_output = BytesIO()
            img.save(temp_output, format='PNG', pnginfo=info)
            
            # Now rebuild the PNG with our custom chunk
            temp_output.seek(0)
            png_data_with_metadata = temp_output.read()
            
            # Add custom gpAI chunk with additional metadata
            output = BytesIO()
            
            # Write PNG signature
            output.write(b'\x89PNG\r\n\x1a\n')
            
            # Find the IEND chunk position
            iend_pos = png_data_with_metadata.rfind(b'IEND')
            if iend_pos == -1:
                iend_pos = len(png_data_with_metadata) - 12
            
            # Write everything before IEND
            output.write(png_data_with_metadata[8:iend_pos - 4])  # Skip signature, write up to IEND length
            
            # Add our custom chunk before IEND
            chunk_type = b'gpAI'
            
            # FIX: Initialize metadata dictionary before use
            metadata = {
                "creator": creator,
                "timestamp": datetime.now().isoformat(),
                "protection_level": "preview_safe_armor",
                "protection_type": "dual_data_streams",
                "original_hash": hashlib.sha256(image_data).hexdigest(),
                "armored_hash": hashlib.sha256(armored_data).hexdigest(),
                "watermark_embedded": bool(custom_watermark_text),
                "ai_disclaimer": ai_disclaimer_text,
            }

            metadata_extended = metadata.copy()
            metadata_extended['original_format'] = 'JPEG'
            metadata_extended['converted_to_png'] = True
            
            import zlib
            chunk_data = json.dumps(metadata_extended).encode('utf-8')
            compressed_data = zlib.compress(chunk_data)
            
            # Create chunk
            # Use metadata instead of armor data for the custom chunk
            chunk_data = compressed_data
            chunk_length = len(chunk_data).to_bytes(4, 'big')
            
            # Calculate CRC
            import struct
            crc = zlib.crc32(chunk_type + chunk_data) & 0xffffffff
            chunk_crc = struct.pack('>I', crc)
            
            # Write custom chunk
            output.write(chunk_length)
            output.write(chunk_type)
            output.write(chunk_data)
            output.write(chunk_crc)
            
            # Write IEND chunk from the original PNG with metadata
            output.write(png_data_with_metadata[iend_pos - 4:])
            
            protected_data = output.getvalue()
            
            # Update metadata to indicate conversion
            is_jpeg = False  # Now it's a PNG
            
        except Exception as e:
            print(f"[ERROR] JPEG to PNG armor conversion failed: {e}")
            # Fallback to ultra-subtle JPEG protection
            protected_data = engine.apply_ultra_subtle_armor(image_data)
    else:
        # For other formats, just return clean image
        protected_data = image_data
    
    # 4. Skip watermarking for images to prevent corruption
    # Watermarking is not applied to prevent file corruption
    
    # 5. Create metadata
    original_was_jpeg = image_data.startswith(b'\xff\xd8\xff')
    metadata = {
        "creator": creator,
        "timestamp": datetime.now().isoformat(),
        "protection_level": "preview_safe_armor",
        "protection_type": "dual_data_streams",
        "original_hash": hashlib.sha256(image_data).hexdigest(),
        "protected_hash": hashlib.sha256(protected_data).hexdigest(),
        "armored_hash": hashlib.sha256(armored_data).hexdigest(),
        "watermark_embedded": False,  # Watermarking disabled for images
        "ai_disclaimer": ai_disclaimer_text,
        "format_preserved": not original_was_jpeg,  # False if JPEG was converted to PNG
        "format_conversion": "JPEG_to_PNG" if original_was_jpeg else "none",
        "human_visibility": "perfect_original_preview",
        "ai_protection": "hidden_armor_stream",
        "armor_location": "custom_chunks",  # Always custom chunks now
        "ai_confusion_level": "extreme",
        "crc_trap": True  # The CRC error is intentional for AI confusion
    }
    
    return protected_data, metadata

def create_dual_layer_protected_image(image_data: bytes, creator: str,
                                     custom_watermark_text: Optional[str] = None,
                                     ai_disclaimer_text: Optional[str] = None,
                                     strength: float = 1.0) -> Tuple[bytes, Dict[str, Any]]:
    """
    Applies dual-layer protection with adjustable strength
    """
    return create_preview_safe_armor(image_data, creator, custom_watermark_text, ai_disclaimer_text, strength)

def create_artist_image_protection(image_data: bytes, creator: str,
                                 ai_disclaimer_text: Optional[str] = None,
                                 strength: float = 1.0,
                                 focus_parameter: float = 3.5,
                                 frequency_strategy: str = 'auto',
                                 enable_skil: bool = False,
                                 stochastic_alpha: float = 0.85,
                                 stochastic_beta: float = 0.15) -> Tuple[bytes, Dict[str, Any]]:
    """
    Streamlined primary entry point for applying Invisible Armor.
    It now performs a single, efficient pass that returns both the protected image and its metrics.
    
    SKIL Defense Parameters:
        enable_skil: Enable the stochastic SKIL defense layer.
        stochastic_alpha: Weight for deterministic armor component (default 0.85)
        stochastic_beta: Weight for stochastic mask component (default 0.15)
    """
    is_jpeg = image_data.startswith(b'\xff\xd8\xff')
    original_image_data = image_data
    
    if is_jpeg:
        print("[INFO] Converting JPEG to PNG for optimal armor compatibility.")
        img = Image.open(BytesIO(image_data))
        if img.mode != 'RGB': img = img.convert('RGB')
        png_buffer = BytesIO()
        img.save(png_buffer, format='PNG', optimize=True)
        image_data = png_buffer.getvalue()

    protected_data = None
    metrics = {}
    try:
        import tempfile
        with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_input:
            temp_input.write(image_data)
            temp_input_path = temp_input.name
        temp_output_path = temp_input_path + ".armored.png"
        
        armor_gen = ArmorGenerator()
        
        # Create ArmorConfig with SKIL defense parameters
        armor_config = ArmorConfig(
            enable_stochastic=enable_skil, # Explicitly enable SKIL defense
            stochastic_alpha=stochastic_alpha,
            stochastic_beta=stochastic_beta
        )
        
        # Single, efficient call to the armor engine
        metrics = armor_gen.apply_to_image(
            image_path=temp_input_path,
            out_path=temp_output_path,
            cfg=armor_config,
            strength=strength,
            focus_parameter=focus_parameter,
            frequency_strategy=frequency_strategy,
            return_metrics=True
        ) or {}

        with open(temp_output_path, "rb") as f:
            protected_data = f.read()

        os.remove(temp_input_path)
        os.remove(temp_output_path)
    except Exception as e:
        print(f"[ERROR] Core armor application failed: {e}")
        # On failure, return the original (converted) image data to prevent data loss
        protected_data = image_data
        metrics = {"error": str(e)}

    # --- Create and Embed Metadata ---
    metadata = {
        "creator": creator,
        "timestamp": datetime.now().isoformat(),
        "protection_type": "invisible_armor_v2",
        "original_hash": hashlib.sha256(original_image_data).hexdigest(),
        "protected_hash": hashlib.sha256(protected_data).hexdigest(),
        "ai_disclaimer": ai_disclaimer_text,
        "format_conversion": "JPEG_to_PNG" if is_jpeg else "none",
        "armor_config": {
            "strength": strength, "focus_parameter": focus_parameter,
            "frequency_strategy": frequency_strategy, "stealth_mode": True,
            "skil_defense_enabled": enable_skil,
            "stochastic_alpha": stochastic_alpha, "stochastic_beta": stochastic_beta
        },
        "performance_metrics": metrics
    }
    
    final_image = Image.open(BytesIO(protected_data))
    info = PngImagePlugin.PngInfo()
    info.add_text("GhostprintProtection", json.dumps(metadata))
    
    output_buffer = BytesIO()
    final_image.save(output_buffer, format='PNG', pnginfo=info)
    
    return output_buffer.getvalue(), metadata

__all__ = [
    'InformationGeometryEngine', 'GeometricFingerprint', 'LensFunction',
    'create_ghostprint_fingerprint', 'apply_integrated_ai_poisoning',
    'create_ghost_file_with_ai_poisoning', 'read_ghost_file_with_poisoning',
    'analyze_ai_poisoning_effectiveness', 'create_lite_fingerprinted_file',
    'verify_ghost_file', 'verify_watermark', 'verify_content_integrity',
    'extract_metadata_stamp', 'create_artist_image_protection',
    # Re-export key components from the armor module for easy access
    "Ring", "ArmorConfig", "PureArmorSpec", "ArmorMeta", "ArmorGenerator",
    "apply_delta_autosize", "analyze_image_bands", "analyze_array_bands",
]