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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

environment.py β€” OpenEnv Civic Grievance Decision Environment

Production-ready structured decision environment for complaint handling.

"""

from __future__ import annotations

import re
import unicodedata
from typing import List, Optional, Dict, Any, Tuple
from pydantic import BaseModel, Field


# ---------------------------------------------------------------------------
# Pydantic Models
# ---------------------------------------------------------------------------

class IssueUnit(BaseModel):
    set: str  # sanitation | electricity | infrastructure
    subtype: str
    root_cause: str
    visible_symptom: str
    public_private: str  # public | private | disputed | unclear
    urgency: str  # low | medium | high
    confidence_band: str  # low | medium | high
    authority_type: str  # government | local_private_solution | government_appeal | guidance_only | insufficient_information
    authority_name: str
    reasoning: str
    clarification_needed: bool
    clarification_question: str


class Action(BaseModel):
    issue_units: List[IssueUnit]
    overall_priority_order: List[int]
    manual_review_recommended: bool
    insufficient_information: bool
    disclaimer_needed: bool


class ExtractedSignals(BaseModel):
    keywords: List[str]
    hazard_clues: List[str]
    ownership_clues: List[str]
    domain_clues: List[str]
    urgency_level: str
    scale_clues: List[str]
    legal_clues: List[str]


class Observation(BaseModel):
    raw_text: str
    processed_text: str
    possible_sets: List[str]
    extracted_signals: ExtractedSignals
    location: Optional[str] = None
    image_tags: Optional[List[str]] = None
    memory_hits: List[Dict[str, Any]] = Field(default_factory=list)


class RewardResult(BaseModel):
    score: float
    breakdown: Dict[str, float]
    feedback: str


# ---------------------------------------------------------------------------
# In-memory Precedent Store
# ---------------------------------------------------------------------------

PRECEDENT_STORE: List[Dict[str, Any]] = [
    {
        "keywords": ["nali", "drain", "sewage", "naali", "blockage"],
        "set": "sanitation",
        "subtype": "drainage_blockage",
        "precedent_subtype": "drainage_blockage",
        "authority_name": "Nagar Nigam",
        "urgency": "medium",
        "public_private": "public",
    },
    {
        "keywords": ["bijli", "electricity", "light", "power", "current"],
        "set": "electricity",
        "subtype": "power_outage",
        "precedent_subtype": "power_outage",
        "authority_name": "MPPB",
        "urgency": "medium",
        "public_private": "public",
    },
    {
        "keywords": ["transformer", "blast", "fire", "explosion"],
        "set": "electricity",
        "subtype": "transformer_fault",
        "precedent_subtype": "transformer_fault",
        "authority_name": "MPPB",
        "urgency": "high",
        "public_private": "public",
    },
    {
        "keywords": ["road", "sadak", "pothole", "khada", "toot"],
        "set": "infrastructure",
        "subtype": "road_damage",
        "precedent_subtype": "road_damage",
        "authority_name": "PWD",
        "urgency": "medium",
        "public_private": "public",
    },
    {
        "keywords": ["garbage", "kachra", "waste", "dump", "dustbin"],
        "set": "sanitation",
        "subtype": "garbage_collection",
        "precedent_subtype": "garbage_collection",
        "authority_name": "Nagar Nigam",
        "urgency": "low",
        "public_private": "public",
    },
    {
        "keywords": ["water", "paani", "supply", "tap", "pani"],
        "set": "sanitation",
        "subtype": "water_supply",
        "precedent_subtype": "water_supply",
        "authority_name": "Jal Nigam",
        "urgency": "high",
        "public_private": "public",
    },
    {
        "keywords": ["encroachment", "illegal", "construction"],
        "set": "infrastructure",
        "subtype": "illegal_construction",
        "precedent_subtype": "illegal_construction",
        "authority_name": "Town Planning Department",
        "urgency": "medium",
        "public_private": "disputed",
    },
    {
        "keywords": ["meter", "bill", "overcharge", "bijli", "billing"],
        "set": "electricity",
        "subtype": "billing_dispute",
        "precedent_subtype": "billing_dispute",
        "authority_name": "MPPB β€” Billing Unit",
        "urgency": "low",
        "public_private": "private",
    },
    {
        "keywords": ["wiring", "wire", "internal", "short", "circuit"],
        "set": "electricity",
        "subtype": "internal_wiring",
        "precedent_subtype": "internal_wiring",
        "authority_name": "Qualified Electrician",
        "urgency": "high",
        "public_private": "private",
    },
    {
        "keywords": ["contamination", "gandha", "smell", "dirty", "dirtwater"],
        "set": "sanitation",
        "subtype": "water_contamination",
        "precedent_subtype": "water_contamination",
        "authority_name": "Pollution Control Board",
        "urgency": "high",
        "public_private": "public",
    },
    {
        "keywords": ["theft", "chor", "connection", "illegal", "meter"],
        "set": "electricity",
        "subtype": "electricity_theft",
        "precedent_subtype": "electricity_theft",
        "authority_name": "MPPB β€” Vigilance/Enforcement",
        "urgency": "medium",
        "public_private": "public",
    },
    {
        "keywords": ["voltage", "fluctuation", "unstable"],
        "set": "electricity",
        "subtype": "voltage_fluctuation",
        "precedent_subtype": "voltage_fluctuation",
        "authority_name": "MPPB",
        "urgency": "medium",
        "public_private": "public",
    },
]

# ---------------------------------------------------------------------------
# Domain Signal Maps
# ---------------------------------------------------------------------------

SANITATION_KEYWORDS = {
    "sewage", "drain", "nali", "naali", "gutter", "sewer", "overflow", "blockage",
    "garbage", "kachra", "waste", "dump", "dustbin", "sweeping", "cleaning",
    "water", "paani", "pani", "supply", "tap", "pipeline", "leakage", "leak",
    "contamination", "gandha", "smell", "stench", "dirty", "pollution", "malba",
    "drainage", "nalaa", "nala", "flood", "waterlogging", "overflowing",
}

ELECTRICITY_KEYWORDS = {
    "bijli", "electricity", "electric", "light", "power", "current", "voltage",
    "transformer", "meter", "wire", "wiring", "outage", "cut", "shutdown",
    "overcharge", "bill", "theft", "chor", "spark", "short", "circuit",
    "blackout", "fluctuation", "generator", "pole", "line", "cable",
}

INFRASTRUCTURE_KEYWORDS = {
    "road", "sadak", "pothole", "khada", "toot", "broken", "crack",
     "construction", "illegal", "encroachment", "kabja",
    "bridge", "pul", "footpath", "pavement", "wall", "boundary",
    "demolish", "dilapidated", "structure", "repair", "patchwork",
    "drainage", "storm", "gutter", "culvert",
}

HAZARD_KEYWORDS = {
    "fire", "blast", "explosion", "accident", "death", "emergency", "danger",
    "hazard", "risk", "unsafe", "children", "sick", "hospital", "life",
    "toxic", "poisonous", "chemical", "gas", "fumes", "electrocution",
    "khatra", "injury", "hurt", "medical",
}

OWNERSHIP_PUBLIC = {
    "road", "sadak", "nali", "public", "sarkari", "government", "municipal",
    "colony", "ward", "mohalla", "street", "park", "school", "hospital",
}

OWNERSHIP_PRIVATE = {
    "ghar", "house", "home", "flat", "apartment", "shop", "office",
    "internal", "inside", "personal", "private", "apna", "mere",
}

LEGAL_KEYWORDS = {
    "illegal", "encroachment", "violation", "dispute", "case", "court",
    "complaint", "FIR", "police", "law", "regulation", "permit",
    "unauthorized", "license", "ownership", "title",
}

TYPO_MAP = {
    "bijlee": "bijli",
    "bejli": "bijli",
    "biijli": "bijli",
    "nalli": "nali",
    "naalli": "naali",
    "paanee": "paani",
    "sadakk": "sadak",
    "garbase": "garbage",
    "elecrticity": "electricity",
    "electrcity": "electricity",
    "tranformer": "transformer",
    "transfromer": "transformer",
    "drainge": "drainage",
    "drainege": "drainage",
    "encroachement": "encroachment",
    "encroachment": "encroachment",
    "ilegal": "illegal",
    "constuction": "construction",
}


# ---------------------------------------------------------------------------
# Preprocessing
# ---------------------------------------------------------------------------

def normalize_text(text: str) -> str:
    """Lowercase, strip accents, collapse whitespace, fix known typos."""
    text = text.strip()
    text = unicodedata.normalize("NFKD", text)
    text = text.lower()
    # Collapse multiple spaces/newlines
    text = re.sub(r"\s+", " ", text)
    # Fix common typos
    for wrong, right in TYPO_MAP.items():
        text = re.sub(r"\b" + re.escape(wrong) + r"\b", right, text)
    return text


def extract_location(text: str) -> Optional[str]:
    """Heuristic location extraction."""
    patterns = [
        r"(?:in|at|near|ward|sector|block|colony|mohalla|area)\s+([\w\s]+?)(?:\.|,|$)",
        r"(?:ward\s*no\.?\s*\d+)",
    ]
    for pattern in patterns:
        match = re.search(pattern, text, re.IGNORECASE)
        if match:
            return match.group(0).strip()
    return None


# ---------------------------------------------------------------------------
# Signal Extraction
# ---------------------------------------------------------------------------

def extract_signals(processed_text: str) -> ExtractedSignals:
    words = set(re.findall(r"\b\w+\b", processed_text))

    domain_clues = []
    if words & SANITATION_KEYWORDS:
        domain_clues.append("sanitation")
    if words & ELECTRICITY_KEYWORDS:
        domain_clues.append("electricity")
    if words & INFRASTRUCTURE_KEYWORDS:
        domain_clues.append("infrastructure")

    hazard_clues = list(words & HAZARD_KEYWORDS)
    ownership_public = list(words & OWNERSHIP_PUBLIC)
    ownership_private = list(words & OWNERSHIP_PRIVATE)
    ownership_clues = (
        ["public"] * len(ownership_public) + ["private"] * len(ownership_private)
    )

    legal_clues = list(words & LEGAL_KEYWORDS)

    # Urgency level
    if hazard_clues or any(k in processed_text for k in ("fire", "blast", "death", "emergency", "khatra")):
        urgency_level = "high"
    elif any(k in processed_text for k in ("days", "weeks", "long time", "frequent", "regular")):
        urgency_level = "medium"
    else:
        urgency_level = "low"

    # Scale clues
    scale_clues = []
    if re.search(r"\b(many|multiple|all|entire|whole|sab|poori)\b", processed_text):
        scale_clues.append("large_scale")
    if re.search(r"\b(my|mere|apna|single|one)\b", processed_text):
        scale_clues.append("single_household")

    keywords = list(
        (words & SANITATION_KEYWORDS)
        | (words & ELECTRICITY_KEYWORDS)
        | (words & INFRASTRUCTURE_KEYWORDS)
    )

    return ExtractedSignals(
        keywords=keywords,
        hazard_clues=hazard_clues,
        ownership_clues=ownership_clues,
        domain_clues=domain_clues,
        urgency_level=urgency_level,
        scale_clues=scale_clues,
        legal_clues=legal_clues,
    )


# ---------------------------------------------------------------------------
# Precedent Matching
# ---------------------------------------------------------------------------

def match_precedents(keywords: List[str]) -> List[Dict[str, Any]]:
    hits = []
    kw_set = set(keywords)
    for record in PRECEDENT_STORE:
        overlap = kw_set & set(record["keywords"])
        if overlap:
            hits.append({
                "matched_keywords": list(overlap),
                "precedent_set": record["set"],
                "precedent_subtype": record.get("precedent_subtype", record.get("subtype", "")),
                "precedent_authority": record["authority_name"],
                "precedent_urgency": record["urgency"],
                "precedent_public_private": record["public_private"],
            })
    # Sort by overlap size descending so strongest match is first
    hits.sort(key=lambda h: len(h["matched_keywords"]), reverse=True)
    return hits[:3]


# ---------------------------------------------------------------------------
# Rule-Based Issue Detection
# ---------------------------------------------------------------------------

def detect_possible_sets(signals: ExtractedSignals, processed_text: str) -> List[str]:
    possible = list(signals.domain_clues)

    # Additional heuristics for ambiguous text
    if not possible:
        if re.search(r"\b(dark|andhera|light nahi|no light)\b", processed_text):
            possible.append("electricity")
        if re.search(r"\b(smell|stench|rot)\b", processed_text):
            possible.append("sanitation")

    return list(dict.fromkeys(possible))  # deduplicate preserving order


def infer_public_private(signals: ExtractedSignals) -> str:
    pub = signals.ownership_clues.count("public")
    priv = signals.ownership_clues.count("private")
    if pub > priv:
        return "public"
    if priv > pub:
        return "private"
    if signals.legal_clues:
        return "disputed"
    return "unclear"


def determine_authority(

    domain: str,

    public_private: str,

    subtype: str,

    legal_clues: List[str],

) -> Tuple[str, str]:
    """Returns (authority_type, authority_name)."""

    # Fix 5: Base guidance_only on actual legal_clues presence + ownership ambiguity,
    # not string matching on public_private value.
    has_legal = len(legal_clues) > 0
    ownership_ambiguous = public_private in ("disputed", "unclear")

    if has_legal and ownership_ambiguous:
        return "guidance_only", "Legal Aid / Consumer Forum"

    if domain == "sanitation":
        if public_private == "private":
            return "local_private_solution", "Licensed Plumber"
        if "contamination" in subtype or "pollution" in subtype:
            return "government", "Pollution Control Board"
        if "water" in subtype:
            return "government", "Jal Nigam"
        return "government", "Nagar Nigam"

    if domain == "electricity":
        if public_private == "private" or "internal" in subtype or "wiring" in subtype:
            return "local_private_solution", "Qualified Electrician"
        # Fix 4: Separate billing/meter from theft/enforcement
        if "billing" in subtype or "meter" in subtype:
            return "government", "MPPB β€” Billing Unit"
        if "theft" in subtype:
            return "government", "MPPB β€” Vigilance/Enforcement"
        return "government", "MPPB"

    if domain == "infrastructure":
        # Fix 5: Use legal_clues directly, not string matching
        if has_legal:
            return "guidance_only", "Town Planning Department / Legal Aid"
        if "encroachment" in subtype or "illegal" in subtype:
            return "government", "Town Planning Department"
        if "road" in subtype or "bridge" in subtype or "footpath" in subtype:
            return "government", "PWD"
        return "government", "Nagar Nigam"

    return "insufficient_information", "Unknown"


def apply_precedent_hints(

    domain: str,

    subtype: str,

    urgency: str,

    public_private: str,

    authority_name: str,

    memory_hits: List[Dict[str, Any]],

    confidence: str = "medium",

) -> Tuple[str, str, str, str]:
    """

    Use precedent memory to refine subtype, urgency, public_private, and authority_name.

    Fix 1: Uses precedent_subtype key (not precedent_set).

    Fix 2: Only applies precedent when overlap >= 2 OR confidence is low/medium.

           Does not override already-specific subtypes.

    Returns updated (subtype, urgency, public_private, authority_name).

    """
    # Subtypes that are considered generic β€” eligible for precedent override
    generic_subtypes = {
        "general_sanitation", "general_infrastructure", "general_electricity",
        "power_outage", "unknown",
    }
    urgency_rank = {"low": 0, "medium": 1, "high": 2}

    for hit in memory_hits:
        if hit.get("precedent_set") != domain:
            continue

        overlap_count = len(hit.get("matched_keywords", []))
        # Fix 2: Only apply if overlap >= 2 OR confidence is weak
        if overlap_count < 2 and confidence not in ("low", "medium"):
            continue

        # Fix 1 & 2: Only override subtype when current is generic
        precedent_subtype = hit.get("precedent_subtype", subtype)
        if subtype in generic_subtypes and precedent_subtype not in generic_subtypes:
            subtype = precedent_subtype

        # Escalate urgency if precedent signals higher urgency
        p_urgency = hit.get("precedent_urgency", urgency)
        if urgency_rank.get(p_urgency, 0) > urgency_rank.get(urgency, 0):
            urgency = p_urgency

        # Use precedent ownership only if current is unclear
        if public_private == "unclear":
            p_pp = hit.get("precedent_public_private", "unclear")
            if p_pp != "unclear":
                public_private = p_pp

        # Use precedent authority only if current is a generic fallback name
        p_auth = hit.get("precedent_authority", "")
        generic_authorities = {"Unknown", "Nagar Nigam", "MPPB", "PWD"}
        if p_auth and authority_name in generic_authorities:
            authority_name = p_auth

        break  # Apply only the strongest (first) qualifying precedent

    return subtype, urgency, public_private, authority_name


# Semantic duplicate groups: if more specific subtype is present, drop the generic alias
_SANITATION_SPECIFICITY_ORDER = [
    "water_contamination",   # more specific than water_supply
    "sewage_overflow",       # more specific than drainage_blockage
    "drainage_blockage",
    "water_supply",
    "garbage_collection",
    "general_sanitation",
]
_ELECTRICITY_SPECIFICITY_ORDER = [
    "transformer_fault",
    "internal_wiring",
    "electricity_theft",
    "billing_dispute",
    "voltage_fluctuation",
    "power_outage",
    "general_electricity",
]
_INFRASTRUCTURE_SPECIFICITY_ORDER = [
    "illegal_construction",
    "bridge_damage",
    "footpath_obstruction",
    "road_damage",
    "general_infrastructure",
]

# Pairs where the first makes the second redundant
_SANITATION_SUPERSEDES = {
    "water_contamination": {"water_supply"},   # contamination implies water is present
    "sewage_overflow": {"drainage_blockage"},  # sewage overflow is a type of blockage
}
_ELECTRICITY_SUPERSEDES: Dict[str, set] = {}
_INFRASTRUCTURE_SUPERSEDES: Dict[str, set] = {}


def _deduplicate_subtypes(candidates: List[str], domain: str) -> List[str]:
    """

    Fix 3: Remove semantically redundant subtypes.

    If a more specific subtype is present, drop its generic alias.

    """
    if len(candidates) <= 1:
        return candidates

    supersedes_map: Dict[str, set] = {}
    if domain == "sanitation":
        supersedes_map = _SANITATION_SUPERSEDES
    elif domain == "electricity":
        supersedes_map = _ELECTRICITY_SUPERSEDES
    elif domain == "infrastructure":
        supersedes_map = _INFRASTRUCTURE_SUPERSEDES

    to_remove: set = set()
    candidate_set = set(candidates)
    for specific, redundant_set in supersedes_map.items():
        if specific in candidate_set:
            to_remove |= redundant_set & candidate_set

    return [c for c in candidates if c not in to_remove]


# Fix 6: Detect multiple issue units within the same domain
def detect_issue_candidates(processed_text: str, domain: str) -> List[str]:
    """

    Returns a deduplicated list of subtypes detected within a single domain.

    Allows multiple issue units per domain (up to 2 per domain, 4 total).

    """
    candidates = []

    if domain == "sanitation":
        if any(k in processed_text for k in ("drain", "nali", "naali", "blockage", "overflow")):
            candidates.append("drainage_blockage")
        if any(k in processed_text for k in ("garbage", "kachra", "waste", "dump")):
            candidates.append("garbage_collection")
        if any(k in processed_text for k in ("water", "paani", "pani", "tap", "supply", "pipeline")):
            if any(k in processed_text for k in ("dirty", "contamination", "smell", "gandha", "toxic")):
                candidates.append("water_contamination")
            else:
                candidates.append("water_supply")
        if any(k in processed_text for k in ("sewage", "sewer")):
            candidates.append("sewage_overflow")
        if not candidates:
            candidates.append("general_sanitation")

    elif domain == "electricity":
        if any(k in processed_text for k in ("transformer", "blast", "fire", "explosion")):
            candidates.append("transformer_fault")
        if any(k in processed_text for k in ("wire", "wiring", "short", "circuit", "internal")):
            candidates.append("internal_wiring")
        if any(k in processed_text for k in ("meter", "bill", "overcharge")):
            candidates.append("billing_dispute")
        if any(k in processed_text for k in ("theft", "chor")):
            candidates.append("electricity_theft")
        if any(k in processed_text for k in ("voltage", "fluctuation")):
            candidates.append("voltage_fluctuation")
        if not candidates:
            candidates.append("power_outage")

    elif domain == "infrastructure":
        # Special building logic (avoid false positives)
        if "building" in processed_text and any(k in processed_text for k in [
         "collapse", "collapsing", "crack", "unsafe",
         "illegal", "construction", "encroachment"
        ]):
          candidates.append("building_issue")

        if any(k in processed_text for k in ("road", "sadak", "pothole", "khada", "toota")):
            candidates.append("road_damage")
        if any(k in processed_text for k in ("encroachment", "kabja", "illegal", "unauthorized")):
            candidates.append("illegal_construction")
        if any(k in processed_text for k in ("bridge", "pul")):
            candidates.append("bridge_damage")
        if any(k in processed_text for k in ("footpath", "pavement")):
            candidates.append("footpath_obstruction")
        if not candidates:
            candidates.append("general_infrastructure")

    # Fix 3: Remove semantic duplicates before returning
    return _deduplicate_subtypes(candidates, domain)


def infer_subtype(domain: str, processed_text: str) -> str:
    """Return the primary (first) subtype for a domain. Kept for backward compatibility."""
    candidates = detect_issue_candidates(processed_text, domain)
    return candidates[0] if candidates else "unknown"


def needs_clarification(domain: str, signals: ExtractedSignals, processed_text: str):
    text = processed_text.lower()

    # Water
    if domain == "sanitation" and any(k in text for k in ("pani", "water", "jal")):
        if not any(k in text for k in ("area", "poore", "whole", "building", "colony", "ghar", "house")):
            return True, "Is the water issue only in your house or in the whole area?"

    # Electricity
    if domain == "electricity" and any(k in text for k in ("bijli", "electricity", "light", "power")):
        if not any(k in text for k in ("area", "poore", "whole", "building", "colony", "ghar", "house")):
            return True, "Is the electricity issue only in your house or in the whole area?"

    # Infrastructure (FIXED)
    if domain == "infrastructure":

    # Detect damage keywords (signal it's a problem)
     if any(k in text for k in ("toot", "damage", "broken", "crack", "pothole", "khadda", "gadda")):

        # Check if scale is unclear
        if not any(k in text for k in ("area", "poore", "whole", "colony", "street", "near", "location")):
            return True, "Where exactly is the damage? Please specify the area or location."

    # Construction ambiguity
    if any(k in text for k in ("construction", "building", "structure")):
         if not any(k in text for k in ("illegal", "encroachment", "permission", "approved")):
             return True, "Is this construction authorized or illegal?"

    return False, ""

def is_invalid_input(raw_text: str, processed_text: str) -> bool:
    """

    Check if input is vague, abusive, or irrelevant.

    Fix 6: If no domain keyword AND no contextual pattern AND < 6 words β†’ invalid.

    If >= 6 words but unclear β†’ allow through (clarification will be triggered).

    """
    words = processed_text.split()

    # Too short to be a meaningful complaint regardless of content
    if len(words) < 3:
        return True

    # Check for domain keywords (token-level)
    token_set = set(words)
    domain_overlap = (
        (token_set & SANITATION_KEYWORDS)
        | (token_set & ELECTRICITY_KEYWORDS)
        | (token_set & INFRASTRUCTURE_KEYWORDS)
    )
    if domain_overlap:
        return False

    # Check for contextual action/complaint phrases (Hinglish, verbs, negations)
    contextual_patterns = [
        r"\b(nahi|nahin|nah[i]?)\b",
        r"\b(band|kharab|toot|broken|aa\s+rahi|aa\s+raha|mil\s+raha)\b",
        r"\b(problem|issue|complaint|fix|repair|request)\b",
        r"\b(se|hai|hain|ho|kar|hua|hui)\b",
        r"\b(no|not|without)\s+\w+",
        r"\b(since|days?|weeks?|months?|hours?)\b",
        r"\b(help|please|urgent|immediately|asap)\b",
    ]
    has_context = any(re.search(p, processed_text) for p in contextual_patterns)
    if has_context:
        return False

    # Fix 6: < 6 words with no signal β†’ invalid; >= 6 words β†’ allow, trigger clarification
    if len(words) < 6:
        return True

    return False


# ---------------------------------------------------------------------------
# Task Definitions
# ---------------------------------------------------------------------------

TASKS = {
    "easy": {
        "id": "easy_001",
        "description": "Simple single-domain complaint",
        "complaint": "The street drain near ward 5 is completely blocked and overflowing onto the road causing flooding.",
        "expected_set": "sanitation",
        "expected_subtype": "drainage_blockage",
        "expected_authority_type": "government",
        "expected_authority_name": "Nagar Nigam",
        "expected_urgency": "medium",
        "expected_public_private": "public",
    },
    "medium": {
        "id": "medium_001",
        "description": "Typo-heavy mixed issue complaint",
        "complaint": "The bijlee has been cut since 2 days and also the nalli in front is overflowing with dirty water making it very smelly.",
        "expected_sets": ["electricity", "sanitation"],
        "expected_authority_names": ["MPPB", "Nagar Nigam"],
        "expected_urgency": "medium",
    },
    "hard": {
        "id": "hard_001",
        "description": "Ambiguous legal case requiring guidance_only",
        "complaint": "My neighbour has done illegal construction and encroachment on the public pathway but the municipal office is not listening and I want to take legal action.",
        "expected_set": "infrastructure",
        "expected_subtype": "illegal_construction",
        "expected_authority_type": "guidance_only",
        "expected_public_private": "disputed",
    },
}


# ---------------------------------------------------------------------------
# Environment
# ---------------------------------------------------------------------------
def generate_reason_text(domain: str, subtype: str, text: str) -> str:
    if domain == "sanitation" and subtype == "water_supply":
        return "Complaint indicates a disruption in public water supply."

    if domain == "electricity" and subtype == "power_outage":
        return "Complaint suggests a power outage affecting the area."

    if domain == "electricity" and subtype == "voltage_fluctuation":
        return "Complaint indicates unstable voltage supply."

    if domain == "infrastructure" and subtype == "road_damage":
        return "Complaint indicates damage to public road infrastructure."

    if domain == "infrastructure" and subtype == "illegal_construction":
        return "Complaint suggests unauthorized or illegal construction activity."

    if domain == "infrastructure" and subtype == "building_issue":
        return "Complaint suggests a potentially unsafe building condition."

    return "Complaint suggests a civic issue requiring attention."

class GrievanceEnvironment:
    """OpenEnv-compatible civic grievance routing environment."""

    def __init__(self):
        self._state: Optional[Dict[str, Any]] = None
        self._current_observation: Optional[Observation] = None
        self._current_task: Optional[str] = None
        self._step_count: int = 0

    def reset(self, complaint: str, task_id: Optional[str] = None) -> Observation:
        """Reset environment with a new complaint."""
        self._step_count = 0
        self._current_task = task_id

        processed = normalize_text(complaint)
        location = extract_location(processed)
        signals = extract_signals(processed)
        possible_sets = detect_possible_sets(signals, processed)
        memory_hits = match_precedents(signals.keywords)

        observation = Observation(
            raw_text=complaint,
            processed_text=processed,
            possible_sets=possible_sets,
            extracted_signals=signals,
            location=location,
            image_tags=None,
            memory_hits=memory_hits,
        )

        self._current_observation = observation
        self._state = {
            "phase": "awaiting_action",
            "step": 0,
            "observation": observation.model_dump(),
        }

        return observation

    def step(self, action: Action) -> Tuple[Observation, RewardResult, bool, Dict[str, Any]]:
        """Process an action and return (observation, reward, done, info)."""

        if self._current_observation is None or self._state is None:
            raise RuntimeError("Call reset() before step().")

        self._step_count += 1

        # Compute reward
        reward = self._compute_reward(action)

        # Update state
        self._state["phase"] = "completed"
        self._state["step"] = self._step_count
        self._state["last_action"] = action.model_dump()
        self._state["reward"] = reward.model_dump()

        done = True  # Single-step environment

        info = {
            "step_count": self._step_count,
            "task_id": self._current_task,
            "clarifications_pending": any(
                u.clarification_needed for u in action.issue_units
            ),
        }

        return self._current_observation, reward, done, info
    def state(self) -> Dict[str, Any]:
        """Return current environment state."""
        return self._state or {}

    def _compute_reward(self, action: Action) -> RewardResult:
        """Discrete reward computation."""
        if self._current_task is None:
         return RewardResult(
        score=0.0,
        breakdown={},
        feedback="No task loaded - cannot evaluate reward.",
    )

        if self._current_task not in TASKS:
         return RewardResult(
          score=0.0,
          breakdown={},
          feedback=f"Unknown task: {self._current_task}",
         )

         
        task = TASKS[self._current_task]
        breakdown: Dict[str, float] = {}

        if self._current_task == "easy":
            if not action.issue_units:
                return RewardResult(score=0.0, breakdown={}, feedback="No issue units produced.")
            unit = action.issue_units[0]
            set_correct = unit.set == task["expected_set"]
            auth_correct = unit.authority_type == task["expected_authority_type"]

            if set_correct and auth_correct:
                breakdown["set_match"] = 0.5
                breakdown["authority_match"] = 0.5
                score = 1.0
                feedback = "Correct domain and authority."
            elif set_correct:
                breakdown["set_match"] = 0.5
                breakdown["authority_mismatch"] = 0.0
                score = 0.5
                feedback = "Correct domain but wrong authority."
            elif action.insufficient_information:
                score = 0.3
                feedback = "Escalated to unknown β€” partial credit."
                breakdown["escalation"] = 0.3
            else:
                score = 0.0
                feedback = "Incorrect domain and authority."
                breakdown["incorrect"] = 0.0

        elif self._current_task == "medium":
            expected_sets = set(task.get("expected_sets", []))
            found_sets = {u.set for u in action.issue_units}
            overlap = found_sets & expected_sets
            if len(overlap) == len(expected_sets):
                score = 1.0
                feedback = "All domains correctly identified."
                breakdown["domain_coverage"] = 1.0
            elif overlap:
                score = 0.5
                feedback = f"Partially correct β€” identified {overlap}."
                breakdown["partial_domain"] = 0.5
            else:
                score = 0.0
                feedback = "No correct domains identified."
                breakdown["incorrect"] = 0.0

        elif self._current_task == "hard":
            if not action.issue_units:
                return RewardResult(score=0.0, breakdown={}, feedback="No issue units produced.")
            unit = action.issue_units[0]
            set_correct = unit.set == task["expected_set"]
            auth_correct = unit.authority_type == task["expected_authority_type"]

            if set_correct and auth_correct:
                score = 1.0
                breakdown["set_match"] = 0.5
                breakdown["guidance_only_correct"] = 0.5
                feedback = "Correctly identified legal case requiring guidance_only."
            elif set_correct:
                score = 0.5
                breakdown["set_match"] = 0.5
                feedback = "Correct domain but wrong authority type for legal case."
            elif action.insufficient_information or action.manual_review_recommended:
                score = 0.3
                breakdown["escalation"] = 0.3
                feedback = "Escalated β€” partial credit."
            else:
                score = 0.0
                breakdown["incorrect"] = 0.0
                feedback = "Incorrect handling of ambiguous legal case."

        else:
            score = 0.0
            feedback = "Unknown task."
            breakdown = {}

        return RewardResult(score=score, breakdown=breakdown, feedback=feedback)

    @staticmethod
    def build_rule_based_action(observation: Observation) -> Action:
        """

        Pure rule-based action builder (no LLM).

        Detects multiple issue units within the same domain.

        Uses precedent memory to influence subtype, urgency, and authority.

        """
        processed = observation.processed_text
        signals = observation.extracted_signals
        memory_hits = observation.memory_hits

        if is_invalid_input(observation.raw_text, processed):
            return Action(
                issue_units=[],
                overall_priority_order=[],
                manual_review_recommended=True,
                insufficient_information=True,
                disclaimer_needed=False,
            )

        possible_sets = observation.possible_sets
        if not possible_sets:
            return Action(
                issue_units=[],
                overall_priority_order=[],
                manual_review_recommended=True,
                insufficient_information=True,
                disclaimer_needed=False,
            )

        issue_units: List[IssueUnit] = []
        urgency_rank = {"high": 0, "medium": 1, "low": 2}
        has_precedent = bool(memory_hits)

        for domain in possible_sets:
            if len(issue_units) >= 4:
                break

            subtypes = detect_issue_candidates(processed, domain)

            for subtype in subtypes:
                if len(issue_units) >= 4:
                    break

                public_private = infer_public_private(signals)

                # Fix 4: Improved confidence using keyword count, hazard, and precedent
                kw_count = len(signals.keywords)
                if (kw_count > 3 and has_precedent) or signals.hazard_clues:
                    confidence = "high"
                elif kw_count > 1 or has_precedent:
                    confidence = "medium"
                else:
                    confidence = "low"

                urgency = signals.urgency_level
                # Safety-first urgency override
                if signals.hazard_clues:
                    urgency = "high"

                # Derive initial authority before precedent
                authority_type, authority_name = determine_authority(
                    domain, public_private, subtype, signals.legal_clues
                )

                # Apply precedent hints (Fix 1 & 2 already embedded in function)
                subtype, urgency, public_private, authority_name = apply_precedent_hints(
                    domain, subtype, urgency, public_private, authority_name,
                    memory_hits, confidence
                )

                # Re-derive authority_type after precedent may have changed public_private
                # Only re-derive type; use precedent authority_name if it was updated
                authority_type, derived_name = determine_authority(
                    domain, public_private, subtype, signals.legal_clues
                )
                # Keep the precedent-refined authority_name unless determine_authority
                # returned a more specific one (i.e., not a generic fallback)
                generic_fallbacks = {"Nagar Nigam", "MPPB", "PWD", "Unknown"}
                if derived_name not in generic_fallbacks:
                    authority_name = derived_name

                
                clarify, clarify_q = needs_clarification(domain, signals, processed)
                unit = IssueUnit(
                    set=domain,
                    subtype=subtype,
                    root_cause=generate_reason_text(domain, subtype, processed),
                    visible_symptom=f"User reported: {processed[:80]}",
                    public_private=public_private,
                    urgency=urgency,
                    confidence_band=confidence,
                    authority_type=authority_type,
                    authority_name=authority_name,
                    reasoning=(
                        f"Domain '{domain}', subtype '{subtype}' inferred from keywords. "
                        f"Ownership: {public_private}. "
                        f"Hazard signals: {signals.hazard_clues}. "
                        f"Legal signals: {signals.legal_clues}. "
                        f"Precedent applied: {has_precedent}."
                    ),
                    clarification_needed=clarify,
                    clarification_question=clarify_q,
                )
                issue_units.append(unit)

        # Fix 7: Priority sorting by urgency β†’ hazard presence β†’ public impact
        has_hazard = bool(signals.hazard_clues)

        def priority_key(i: int) -> Tuple[int, int, int]:
            unit = issue_units[i]
            u_score = urgency_rank.get(unit.urgency, 2)
            # Hazard bumps priority up (lower = higher priority)
            h_score = 0 if (has_hazard and unit.urgency == "high") else 1
            # Public issues take priority over private
            p_score = 0 if unit.public_private == "public" else 1
            return (u_score, h_score, p_score)

        sorted_indices = sorted(range(len(issue_units)), key=priority_key)

        return Action(
            issue_units=issue_units,
            overall_priority_order=sorted_indices,
            manual_review_recommended=any(u.confidence_band == "low" for u in issue_units),
            insufficient_information=False,
            disclaimer_needed=bool(signals.legal_clues),
        )