File size: 30,411 Bytes
ee37188
 
 
380fdf3
 
 
 
ee37188
 
4466506
 
 
 
 
d9f59d1
4466506
 
 
 
 
 
 
ee37188
 
4466506
d9f59d1
4466506
 
 
 
 
 
 
ee37188
 
4466506
d9f59d1
4466506
 
 
 
 
 
 
ee37188
 
 
d9f59d1
4466506
 
 
 
 
 
 
ee37188
 
4466506
d9f59d1
4466506
 
 
 
 
 
 
ee37188
 
4466506
d9f59d1
4466506
 
 
 
 
 
 
ee37188
 
4466506
d9f59d1
4466506
 
 
 
 
 
 
ee37188
 
 
d9f59d1
4466506
 
 
 
 
 
 
ee37188
 
4466506
d9f59d1
4466506
 
 
 
 
 
 
ee37188
 
4466506
d9f59d1
4466506
 
 
 
 
 
 
ee37188
 
 
d9f59d1
4466506
 
 
 
 
 
 
ee37188
 
4466506
d9f59d1
4466506
 
 
 
 
 
 
ee37188
 
4466506
d9f59d1
4466506
 
 
 
 
 
 
ee37188
 
4466506
d9f59d1
4466506
 
 
 
 
 
 
ee37188
 
 
d9f59d1
4466506
 
 
 
 
 
 
ee37188
 
4466506
d9f59d1
4466506
 
 
 
 
 
 
ee37188
 
 
 
4466506
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee37188
 
 
 
 
 
 
 
 
 
 
 
4466506
ee37188
 
 
 
 
 
 
 
 
 
 
 
 
4466506
ee37188
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4466506
 
 
 
ee37188
 
d9f59d1
 
 
 
380fdf3
d9f59d1
 
 
ee37188
 
 
 
 
 
 
 
 
d9f59d1
 
 
380fdf3
d9f59d1
 
ee37188
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# DEPENDENCIES
from typing import Dict
from typing import Tuple
from config.enums import Domain
from config.enums import ConfidenceLevel
from config.schemas import MetricThresholds
from config.schemas import DomainThresholds


# ================================ OPTIMIZED THRESHOLDS ===============================
# Philosophy: Multi-perturbation stability and perplexity are the most robust signals
# =====================================================================================

# GENERAL (Default fallback) - Balanced
DEFAULT_THRESHOLDS       = DomainThresholds(domain                       = Domain.GENERAL,
                                            structural                   = MetricThresholds(synthetic_threshold = 0.53, authentic_threshold = 0.38, weight = 0.15, confidence_multiplier = 1.0),
                                            perplexity                   = MetricThresholds(synthetic_threshold = 0.50, authentic_threshold = 0.40, weight = 0.23, confidence_multiplier = 1.0),
                                            entropy                      = MetricThresholds(synthetic_threshold = 0.46, authentic_threshold = 0.44, weight = 0.16, confidence_multiplier = 1.0), 
                                            semantic                     = MetricThresholds(synthetic_threshold = 0.53, authentic_threshold = 0.38, weight = 0.13, confidence_multiplier = 1.0), 
                                            linguistic                   = MetricThresholds(synthetic_threshold = 0.56, authentic_threshold = 0.35, weight = 0.10, confidence_multiplier = 1.0), 
                                            multi_perturbation_stability = MetricThresholds(synthetic_threshold = 0.58, authentic_threshold = 0.33, weight = 0.23, confidence_multiplier = 1.0), 
                                            ensemble_threshold           = 0.30,
                                           )

# ACADEMIC 
ACADEMIC_THRESHOLDS      = DomainThresholds(domain                       = Domain.ACADEMIC,
                                            structural                   = MetricThresholds(synthetic_threshold = 0.56, authentic_threshold = 0.35, weight = 0.13, confidence_multiplier = 1.0),  
                                            perplexity                   = MetricThresholds(synthetic_threshold = 0.48, authentic_threshold = 0.38, weight = 0.22, confidence_multiplier = 1.0),  
                                            entropy                      = MetricThresholds(synthetic_threshold = 0.44, authentic_threshold = 0.43, weight = 0.14, confidence_multiplier = 1.0),
                                            semantic                     = MetricThresholds(synthetic_threshold = 0.56, authentic_threshold = 0.35, weight = 0.14, confidence_multiplier = 1.0), 
                                            linguistic                   = MetricThresholds(synthetic_threshold = 0.60, authentic_threshold = 0.31, weight = 0.12, confidence_multiplier = 1.0),  
                                            multi_perturbation_stability = MetricThresholds(synthetic_threshold = 0.63, authentic_threshold = 0.28, weight = 0.25, confidence_multiplier = 1.0), 
                                            ensemble_threshold           = 0.32, 
                                           )

# CREATIVE WRITING (creative writing is naturally unstable)
CREATIVE_THRESHOLDS      = DomainThresholds(domain                       = Domain.CREATIVE,
                                            structural                   = MetricThresholds(synthetic_threshold = 0.50, authentic_threshold = 0.41, weight = 0.18, confidence_multiplier = 1.0), 
                                            perplexity                   = MetricThresholds(synthetic_threshold = 0.54, authentic_threshold = 0.43, weight = 0.20, confidence_multiplier = 1.0), 
                                            entropy                      = MetricThresholds(synthetic_threshold = 0.50, authentic_threshold = 0.48, weight = 0.20, confidence_multiplier = 1.0), 
                                            semantic                     = MetricThresholds(synthetic_threshold = 0.50, authentic_threshold = 0.41, weight = 0.15, confidence_multiplier = 1.0),  
                                            linguistic                   = MetricThresholds(synthetic_threshold = 0.54, authentic_threshold = 0.38, weight = 0.09, confidence_multiplier = 1.0),
                                            multi_perturbation_stability = MetricThresholds(synthetic_threshold = 0.57, authentic_threshold = 0.35, weight = 0.18, confidence_multiplier = 0.95), 
                                            ensemble_threshold           = 0.33, 
                                           )

# AI/ML/DATA SCIENCE
AI_ML_THRESHOLDS         = DomainThresholds(domain                       = Domain.AI_ML,
                                            structural                   = MetricThresholds(synthetic_threshold = 0.46, authentic_threshold = 0.43, weight = 0.13, confidence_multiplier = 0.95), 
                                            perplexity                   = MetricThresholds(synthetic_threshold = 0.40, authentic_threshold = 0.46, weight = 0.24, confidence_multiplier = 1.0),
                                            entropy                      = MetricThresholds(synthetic_threshold = 0.36, authentic_threshold = 0.50, weight = 0.14, confidence_multiplier = 0.95), 
                                            semantic                     = MetricThresholds(synthetic_threshold = 0.46, authentic_threshold = 0.43, weight = 0.14, confidence_multiplier = 1.0),  
                                            linguistic                   = MetricThresholds(synthetic_threshold = 0.50, authentic_threshold = 0.39, weight = 0.10, confidence_multiplier = 0.95),  
                                            multi_perturbation_stability = MetricThresholds(synthetic_threshold = 0.53, authentic_threshold = 0.36, weight = 0.25, confidence_multiplier = 1.0),  
                                            ensemble_threshold           = 0.28, 
                                           )

# SOFTWARE DEVELOPMENT 
SOFTWARE_DEV_THRESHOLDS  = DomainThresholds(domain                       = Domain.SOFTWARE_DEV,
                                            structural                   = MetricThresholds(synthetic_threshold = 0.56, authentic_threshold = 0.35, weight = 0.14, confidence_multiplier = 0.90),  
                                            perplexity                   = MetricThresholds(synthetic_threshold = 0.48, authentic_threshold = 0.38, weight = 0.22, confidence_multiplier = 1.0), 
                                            entropy                      = MetricThresholds(synthetic_threshold = 0.44, authentic_threshold = 0.43, weight = 0.15, confidence_multiplier = 0.90),  
                                            semantic                     = MetricThresholds(synthetic_threshold = 0.56, authentic_threshold = 0.35, weight = 0.13, confidence_multiplier = 0.90), 
                                            linguistic                   = MetricThresholds(synthetic_threshold = 0.58, authentic_threshold = 0.33, weight = 0.11, confidence_multiplier = 0.85),  
                                            multi_perturbation_stability = MetricThresholds(synthetic_threshold = 0.61, authentic_threshold = 0.30, weight = 0.25, confidence_multiplier = 0.95),  
                                            ensemble_threshold           = 0.31, 
                                           )

# TECHNICAL DOCUMENTATION (docs can be template-based)
TECHNICAL_DOC_THRESHOLDS = DomainThresholds(domain                       = Domain.TECHNICAL_DOC,
                                            structural                   = MetricThresholds(synthetic_threshold = 0.52, authentic_threshold = 0.38, weight = 0.15, confidence_multiplier = 0.85),  
                                            perplexity                   = MetricThresholds(synthetic_threshold = 0.46, authentic_threshold = 0.40, weight = 0.24, confidence_multiplier = 1.0), 
                                            entropy                      = MetricThresholds(synthetic_threshold = 0.42, authentic_threshold = 0.45, weight = 0.16, confidence_multiplier = 0.90),  
                                            semantic                     = MetricThresholds(synthetic_threshold = 0.52, authentic_threshold = 0.38, weight = 0.14, confidence_multiplier = 0.90),  
                                            linguistic                   = MetricThresholds(synthetic_threshold = 0.55, authentic_threshold = 0.35, weight = 0.11, confidence_multiplier = 0.85),  
                                            multi_perturbation_stability = MetricThresholds(synthetic_threshold = 0.58, authentic_threshold = 0.32, weight = 0.20, confidence_multiplier = 0.90),  
                                            ensemble_threshold           = 0.30, 
                                           )

# ENGINEERING 
ENGINEERING_THRESHOLDS   = DomainThresholds(domain                       = Domain.ENGINEERING,
                                            structural                   = MetricThresholds(synthetic_threshold = 0.56, authentic_threshold = 0.35, weight = 0.14, confidence_multiplier = 0.85), 
                                            perplexity                   = MetricThresholds(synthetic_threshold = 0.48, authentic_threshold = 0.38, weight = 0.22, confidence_multiplier = 1.0),  
                                            entropy                      = MetricThresholds(synthetic_threshold = 0.44, authentic_threshold = 0.43, weight = 0.15, confidence_multiplier = 0.90),  
                                            semantic                     = MetricThresholds(synthetic_threshold = 0.56, authentic_threshold = 0.35, weight = 0.13, confidence_multiplier = 0.90),  
                                            linguistic                   = MetricThresholds(synthetic_threshold = 0.59, authentic_threshold = 0.32, weight = 0.11, confidence_multiplier = 0.85), 
                                            multi_perturbation_stability = MetricThresholds(synthetic_threshold = 0.62, authentic_threshold = 0.29, weight = 0.25, confidence_multiplier = 0.95), 
                                            ensemble_threshold           = 0.30,
                                           )

# SCIENCE (Physics, Chemistry, Biology)
SCIENCE_THRESHOLDS       = DomainThresholds(domain                       = Domain.SCIENCE,
                                            structural                   = MetricThresholds(synthetic_threshold = 0.56, authentic_threshold = 0.35, weight = 0.13, confidence_multiplier = 0.90),
                                            perplexity                   = MetricThresholds(synthetic_threshold = 0.49, authentic_threshold = 0.39, weight = 0.23, confidence_multiplier = 1.0), 
                                            entropy                      = MetricThresholds(synthetic_threshold = 0.44, authentic_threshold = 0.43, weight = 0.15, confidence_multiplier = 0.90), 
                                            semantic                     = MetricThresholds(synthetic_threshold = 0.56, authentic_threshold = 0.35, weight = 0.14, confidence_multiplier = 0.95), 
                                            linguistic                   = MetricThresholds(synthetic_threshold = 0.60, authentic_threshold = 0.31, weight = 0.11, confidence_multiplier = 0.90),  
                                            multi_perturbation_stability = MetricThresholds(synthetic_threshold = 0.62, authentic_threshold = 0.29, weight = 0.24, confidence_multiplier = 0.95),  
                                            ensemble_threshold           = 0.32,
                                           )

# BUSINESS 
BUSINESS_THRESHOLDS      = DomainThresholds(domain                       = Domain.BUSINESS,
                                            structural                   = MetricThresholds(synthetic_threshold = 0.50, authentic_threshold = 0.37, weight = 0.15, confidence_multiplier = 1.0),  
                                            perplexity                   = MetricThresholds(synthetic_threshold = 0.46, authentic_threshold = 0.41, weight = 0.22, confidence_multiplier = 1.0), 
                                            entropy                      = MetricThresholds(synthetic_threshold = 0.42, authentic_threshold = 0.45, weight = 0.16, confidence_multiplier = 1.0), 
                                            semantic                     = MetricThresholds(synthetic_threshold = 0.50, authentic_threshold = 0.37, weight = 0.14, confidence_multiplier = 1.0), 
                                            linguistic                   = MetricThresholds(synthetic_threshold = 0.54, authentic_threshold = 0.33, weight = 0.10, confidence_multiplier = 0.95), 
                                            multi_perturbation_stability = MetricThresholds(synthetic_threshold = 0.56, authentic_threshold = 0.31, weight = 0.23, confidence_multiplier = 0.95), 
                                            ensemble_threshold           = 0.28,
                                           )

# LEGAL (NO CHANGES - already working well at 70.3% F1)
LEGAL_THRESHOLDS         = DomainThresholds(domain                       = Domain.LEGAL,
                                            structural                   = MetricThresholds(synthetic_threshold = 0.63, authentic_threshold = 0.33, weight = 0.13, confidence_multiplier = 1.0), 
                                            perplexity                   = MetricThresholds(synthetic_threshold = 0.53, authentic_threshold = 0.37, weight = 0.24, confidence_multiplier = 1.0), 
                                            entropy                      = MetricThresholds(synthetic_threshold = 0.47, authentic_threshold = 0.41, weight = 0.15, confidence_multiplier = 1.0), 
                                            semantic                     = MetricThresholds(synthetic_threshold = 0.63, authentic_threshold = 0.33, weight = 0.14, confidence_multiplier = 1.0), 
                                            linguistic                   = MetricThresholds(synthetic_threshold = 0.66, authentic_threshold = 0.30, weight = 0.10, confidence_multiplier = 0.95), 
                                            multi_perturbation_stability = MetricThresholds(synthetic_threshold = 0.69, authentic_threshold = 0.27, weight = 0.24, confidence_multiplier = 1.0),  
                                            ensemble_threshold           = 0.35,
                                           )

# MEDICAL
MEDICAL_THRESHOLDS       = DomainThresholds(domain                       = Domain.MEDICAL,
                                            structural                   = MetricThresholds(synthetic_threshold = 0.57, authentic_threshold = 0.34, weight = 0.13, confidence_multiplier = 0.90), 
                                            perplexity                   = MetricThresholds(synthetic_threshold = 0.48, authentic_threshold = 0.38, weight = 0.23, confidence_multiplier = 1.0),  
                                            entropy                      = MetricThresholds(synthetic_threshold = 0.43, authentic_threshold = 0.42, weight = 0.14, confidence_multiplier = 0.90),  
                                            semantic                     = MetricThresholds(synthetic_threshold = 0.57, authentic_threshold = 0.34, weight = 0.14, confidence_multiplier = 0.95),  
                                            linguistic                   = MetricThresholds(synthetic_threshold = 0.60, authentic_threshold = 0.31, weight = 0.11, confidence_multiplier = 0.90), 
                                            multi_perturbation_stability = MetricThresholds(synthetic_threshold = 0.63, authentic_threshold = 0.28, weight = 0.25, confidence_multiplier = 0.95), 
                                            ensemble_threshold           = 0.29, 
                                           )

# JOURNALISM 
JOURNALISM_THRESHOLDS    = DomainThresholds(domain                       = Domain.JOURNALISM,
                                            structural                   = MetricThresholds(synthetic_threshold = 0.59, authentic_threshold = 0.33, weight = 0.15, confidence_multiplier = 1.0), 
                                            perplexity                   = MetricThresholds(synthetic_threshold = 0.55, authentic_threshold = 0.37, weight = 0.22, confidence_multiplier = 1.0),  
                                            entropy                      = MetricThresholds(synthetic_threshold = 0.51, authentic_threshold = 0.41, weight = 0.15, confidence_multiplier = 1.0),  
                                            semantic                     = MetricThresholds(synthetic_threshold = 0.59, authentic_threshold = 0.33, weight = 0.13, confidence_multiplier = 1.0), 
                                            linguistic                   = MetricThresholds(synthetic_threshold = 0.61, authentic_threshold = 0.31, weight = 0.10, confidence_multiplier = 0.95), 
                                            multi_perturbation_stability = MetricThresholds(synthetic_threshold = 0.65, authentic_threshold = 0.27, weight = 0.25, confidence_multiplier = 0.95),  
                                            ensemble_threshold           = 0.32,
                                           )

# MARKETING (marketing copy can be naturally formulaic)
MARKETING_THRESHOLDS     = DomainThresholds(domain                       = Domain.MARKETING,
                                            structural                   = MetricThresholds(synthetic_threshold = 0.50, authentic_threshold = 0.35, weight = 0.16, confidence_multiplier = 1.0),
                                            perplexity                   = MetricThresholds(synthetic_threshold = 0.49, authentic_threshold = 0.38, weight = 0.22, confidence_multiplier = 1.0),  
                                            entropy                      = MetricThresholds(synthetic_threshold = 0.40, authentic_threshold = 0.42, weight = 0.17, confidence_multiplier = 1.0),  
                                            semantic                     = MetricThresholds(synthetic_threshold = 0.40, authentic_threshold = 0.35, weight = 0.14, confidence_multiplier = 0.95),  
                                            linguistic                   = MetricThresholds(synthetic_threshold = 0.50, authentic_threshold = 0.32, weight = 0.10, confidence_multiplier = 0.90), 
                                            multi_perturbation_stability = MetricThresholds(synthetic_threshold = 0.55, authentic_threshold = 0.28, weight = 0.21, confidence_multiplier = 0.95),
                                            ensemble_threshold           = 0.30, 
                                           )

# SOCIAL MEDIA (social media is naturally chaotic and unstable)
SOCIAL_MEDIA_THRESHOLDS  = DomainThresholds(domain                       = Domain.SOCIAL_MEDIA,
                                            structural                   = MetricThresholds(synthetic_threshold = 0.50, authentic_threshold = 0.37, weight = 0.16, confidence_multiplier = 0.85),
                                            perplexity                   = MetricThresholds(synthetic_threshold = 0.48, authentic_threshold = 0.39, weight = 0.23, confidence_multiplier = 0.90),
                                            entropy                      = MetricThresholds(synthetic_threshold = 0.48, authentic_threshold = 0.43, weight = 0.20, confidence_multiplier = 0.90),
                                            semantic                     = MetricThresholds(synthetic_threshold = 0.50, authentic_threshold = 0.37, weight = 0.14, confidence_multiplier = 0.85), 
                                            linguistic                   = MetricThresholds(synthetic_threshold = 0.52, authentic_threshold = 0.34, weight = 0.09, confidence_multiplier = 0.80), 
                                            multi_perturbation_stability = MetricThresholds(synthetic_threshold = 0.54, authentic_threshold = 0.29, weight = 0.18, confidence_multiplier = 0.85), 
                                            ensemble_threshold           = 0.30, 
                                           )

# PERSONAL BLOG
BLOG_PERSONAL_THRESHOLDS = DomainThresholds(domain                       = Domain.BLOG_PERSONAL,
                                            structural                   = MetricThresholds(synthetic_threshold = 0.51, authentic_threshold = 0.40, weight = 0.16, confidence_multiplier = 1.0),
                                            perplexity                   = MetricThresholds(synthetic_threshold = 0.52, authentic_threshold = 0.43, weight = 0.21, confidence_multiplier = 1.0), 
                                            entropy                      = MetricThresholds(synthetic_threshold = 0.48, authentic_threshold = 0.47, weight = 0.17, confidence_multiplier = 1.0),  
                                            semantic                     = MetricThresholds(synthetic_threshold = 0.51, authentic_threshold = 0.40, weight = 0.14, confidence_multiplier = 1.0),  
                                            linguistic                   = MetricThresholds(synthetic_threshold = 0.54, authentic_threshold = 0.37, weight = 0.10, confidence_multiplier = 0.95),  
                                            multi_perturbation_stability = MetricThresholds(synthetic_threshold = 0.57, authentic_threshold = 0.34, weight = 0.22, confidence_multiplier = 0.95),  
                                            ensemble_threshold           = 0.32, 
                                           )

# TUTORIAL/HOW-TO (Tutorials follow patterns)
TUTORIAL_THRESHOLDS      = DomainThresholds(domain                       = Domain.TUTORIAL,
                                            structural                   = MetricThresholds(synthetic_threshold = 0.55, authentic_threshold = 0.37, weight = 0.14, confidence_multiplier = 0.95), 
                                            perplexity                   = MetricThresholds(synthetic_threshold = 0.47, authentic_threshold = 0.40, weight = 0.23, confidence_multiplier = 1.0),  
                                            entropy                      = MetricThresholds(synthetic_threshold = 0.45, authentic_threshold = 0.44, weight = 0.15, confidence_multiplier = 0.95), 
                                            semantic                     = MetricThresholds(synthetic_threshold = 0.53, authentic_threshold = 0.37, weight = 0.14, confidence_multiplier = 1.0),  
                                            linguistic                   = MetricThresholds(synthetic_threshold = 0.56, authentic_threshold = 0.34, weight = 0.10, confidence_multiplier = 0.90),  
                                            multi_perturbation_stability = MetricThresholds(synthetic_threshold = 0.60, authentic_threshold = 0.31, weight = 0.24, confidence_multiplier = 0.95), 
                                            ensemble_threshold           = 0.31, 
                                           )


# THRESHOLD REGISTRY
THRESHOLD_REGISTRY  : Dict[Domain, DomainThresholds]              = {Domain.GENERAL       : DEFAULT_THRESHOLDS,
                                                                     Domain.ACADEMIC      : ACADEMIC_THRESHOLDS,
                                                                     Domain.CREATIVE      : CREATIVE_THRESHOLDS,
                                                                     Domain.AI_ML         : AI_ML_THRESHOLDS,
                                                                     Domain.SOFTWARE_DEV  : SOFTWARE_DEV_THRESHOLDS,
                                                                     Domain.TECHNICAL_DOC : TECHNICAL_DOC_THRESHOLDS,
                                                                     Domain.ENGINEERING   : ENGINEERING_THRESHOLDS,
                                                                     Domain.SCIENCE       : SCIENCE_THRESHOLDS,
                                                                     Domain.BUSINESS      : BUSINESS_THRESHOLDS,
                                                                     Domain.LEGAL         : LEGAL_THRESHOLDS,
                                                                     Domain.MEDICAL       : MEDICAL_THRESHOLDS,
                                                                     Domain.JOURNALISM    : JOURNALISM_THRESHOLDS,
                                                                     Domain.MARKETING     : MARKETING_THRESHOLDS,
                                                                     Domain.SOCIAL_MEDIA  : SOCIAL_MEDIA_THRESHOLDS,
                                                                     Domain.BLOG_PERSONAL : BLOG_PERSONAL_THRESHOLDS,
                                                                     Domain.TUTORIAL      : TUTORIAL_THRESHOLDS,
                                                                    }


# CONFIDENCE LEVEL MAPPING
CONFIDENCE_RANGES   : Dict[ConfidenceLevel, Tuple[float, float]] = {ConfidenceLevel.VERY_LOW  : (0.0, 0.4),
                                                                    ConfidenceLevel.LOW       : (0.4, 0.6),
                                                                    ConfidenceLevel.MEDIUM    : (0.6, 0.75),
                                                                    ConfidenceLevel.HIGH      : (0.75, 0.9),
                                                                    ConfidenceLevel.VERY_HIGH : (0.9, 1.0),
                                                                   }



# HELPER FUNCTIONS 
def get_threshold_for_domain(domain: Domain) -> DomainThresholds:
    """
    Get thresholds for a specific domain
    """
    return THRESHOLD_REGISTRY.get(domain, DEFAULT_THRESHOLDS)


def get_confidence_level(score: float) -> ConfidenceLevel:
    """
    Determine confidence level for authenticity estimation: score represents synthetic-likeness probability
    """
    for level, (min_val, max_val) in CONFIDENCE_RANGES.items():
        if (min_val <= score < max_val):
            return level

    return ConfidenceLevel.VERY_HIGH


def adjust_threshold_by_confidence(threshold: float, confidence: float, conservative: bool = True) -> float:
    """
    Adjust threshold based on confidence level
    """
    if conservative:
        adjustment         = min(((1 - confidence) * 0.1), 0.05)
        adjusted_threshold = threshold + adjustment
        
        return adjusted_threshold

    else:
        adjustment         = confidence * 0.05
        adjusted_threshold = threshold - adjustment
        
        return adjusted_threshold


def interpolate_thresholds(domain1: Domain, domain2: Domain, weight1: float = 0.5) -> DomainThresholds:
    """
    Interpolate between two domain thresholds
    """
    thresh1 = get_threshold_for_domain(domain = domain1)
    thresh2 = get_threshold_for_domain(domain = domain2)
    weight2 = 1 - weight1
    
    def interpolate_metric(m1: MetricThresholds, m2: MetricThresholds) -> MetricThresholds:
        return MetricThresholds(synthetic_threshold   = m1.synthetic_threshold * weight1 + m2.synthetic_threshold * weight2,
                                authentic_threshold   = m1.authentic_threshold * weight1 + m2.authentic_threshold * weight2,
                                weight                = m1.weight * weight1 + m2.weight * weight2,
                                confidence_multiplier = m1.confidence_multiplier * weight1 + m2.confidence_multiplier * weight2,
                               )
    
    return DomainThresholds(domain                       = domain1,
                            structural                   = interpolate_metric(thresh1.structural, thresh2.structural),
                            perplexity                   = interpolate_metric(thresh1.perplexity, thresh2.perplexity),
                            entropy                      = interpolate_metric(thresh1.entropy, thresh2.entropy),
                            semantic                     = interpolate_metric(thresh1.semantic, thresh2.semantic),
                            linguistic                   = interpolate_metric(thresh1.linguistic, thresh2.linguistic),
                            multi_perturbation_stability = interpolate_metric(thresh1.multi_perturbation_stability, thresh2.multi_perturbation_stability),
                            ensemble_threshold           = thresh1.ensemble_threshold * weight1 + thresh2.ensemble_threshold * weight2,
                           )


def get_active_metric_weights(domain: Domain, enabled_metrics: Dict[str, bool]) -> Dict[str, float]:
    """
    Get weights for enabled metrics, normalized to sum to 1.0
    """
    thresholds     = get_threshold_for_domain(domain = domain)
    
    metric_mapping = {"structural"                   : thresholds.structural,
                      "perplexity"                   : thresholds.perplexity,
                      "entropy"                      : thresholds.entropy,
                      "semantic"                     : thresholds.semantic,
                      "linguistic"                   : thresholds.linguistic,
                      "multi_perturbation_stability" : thresholds.multi_perturbation_stability,
                     }
    
    active_weights = dict()

    for metric_name, threshold_obj in metric_mapping.items():
        if enabled_metrics.get(metric_name, False):
            active_weights[metric_name] = threshold_obj.weight
    
    # Normalize
    total_weight = sum(active_weights.values())

    if (total_weight > 0):
        active_weights = {name: weight / total_weight for name, weight in active_weights.items()}
    
    return active_weights



# Export
__all__ = ["Domain",
           "ConfidenceLevel",
           "MetricThresholds",
           "DomainThresholds",
           "CONFIDENCE_RANGES",
           "DEFAULT_THRESHOLDS",
           "THRESHOLD_REGISTRY",
           "get_confidence_level",
           "interpolate_thresholds",  
           "get_threshold_for_domain",           
           "get_active_metric_weights",
           "adjust_threshold_by_confidence",
          ]