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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
# # from Crypto.PublicKey import RSA
# # from Crypto.Hash import SHA256
# # from Crypto.Signature import pss


# # # ----------------------------
# # # 1. Generate RSA Key Pair
# # # ----------------------------
# # def generate_rsa_keys(bits=2048):
# #     key = RSA.generate(bits)

# #     private_key = key.export_key()
# #     public_key = key.publickey().export_key()

# #     return private_key, public_key


# # # ----------------------------
# # # 2. Save Keys to Files
# # # ----------------------------
# # def save_key(key_data, filename):
# #     with open(filename, "wb") as f:
# #         f.write(key_data)


# # # ----------------------------
# # # 3. Load Private Key
# # # ----------------------------
# # def load_private_key(filename="private.pem"):
# #     with open(filename, "rb") as f:
# #         return RSA.import_key(f.read())


# # # ----------------------------
# # # 4. Load Public Key
# # # ----------------------------
# # def load_public_key(filename="public.pem"):
# #     with open(filename, "rb") as f:
# #         return RSA.import_key(f.read())


# # # ----------------------------
# # # 5. Sign Message (RSA-PSS)
# # # ----------------------------
# # def sign_message(message, private_key):
# #     if isinstance(message, str):
# #         message = message.encode("utf-8")

# #     hash_obj = SHA256.new(message)
# #     signature = pss.new(private_key).sign(hash_obj)

# #     return signature


# # # ----------------------------
# # # 6. Verify Signature
# # # ----------------------------
# # def verify_signature(message, signature, public_key):
# #     if isinstance(message, str):
# #         message = message.encode("utf-8")

# #     hash_obj = SHA256.new(message)

# #     try:
# #         pss.new(public_key).verify(hash_obj, signature)
# #         return True
# #     except (ValueError, TypeError):
# #         return False


# # # ----------------------------
# # # 7. Example Usage
# # # ----------------------------
# # if __name__ == "__main__":

# #     # Generate keys
# #     private_key, public_key = generate_rsa_keys()

# #     # Save keys
# #     save_key(private_key, "private.pem")
# #     save_key(public_key, "public.pem")

# #     # Load keys
# #     priv = load_private_key("private.pem")
# #     pub = load_public_key("public.pem")

# #     # Message
# #     msg = "This is a legal RAG secure request"

# #     # Sign
# #     signature = sign_message(msg, priv)
# #     print("Signature generated:", signature.hex())

# #     # Verify
# #     is_valid = verify_signature(msg, signature, pub)
# #     print("Signature valid?", is_valid)


# import requests
# import os
# from dotenv import load_dotenv

# load_dotenv()

# API_TOKEN = os.getenv("kanoon_token")

# BASE_URL = "https://api.indiankanoon.org/search/"


# def search(query):
#     headers = {
#         "Authorization": f"Token {API_TOKEN}",
#         "Content-Type": "application/x-www-form-urlencoded"
#     }

#     data = {
#         "formInput": query
#     }

#     response = requests.post(BASE_URL, headers=headers, data=data)

#     print("STATUS:", response.status_code)
#     print("RESPONSE:", response.text)

#     try:
#         return response.json()
#     except:
#         return response.text


# print(search('"fundamental rights"'))




# from neo4j import GraphDatabase

# driver = GraphDatabase.driver(
#     "bolt://localhost:7687",
#     auth=("neo4j", "test12345")
# )

# with driver.session() as session:
#     query = """
#     MATCH (a:Article {number: 14})
#     RETURN a.text AS content
#     """

#     result = session.run(query)
#     print(result.single())

# driver.close()
# from embeddings_generator import LegalEmbedder
# from vector_db import QdrantIngestor

# question = "what does article 14 of the indian constitution say?"

# # Create embedding

# embedder = LegalEmbedder(model_name="all-MiniLM-L6-v2")
# embedding = embedder.embed_texts(question)

# # Search Qdrant

# qdrant = QdrantIngestor()
# hits = qdrant.search(
#     # collection_name="legal_constitution",
#     query_vector=embedding.tolist()[0],
#     limit=5
# )

# # article_ids = [hit.payload["article_id"] for hit in hits]
# print("Top matching article IDs:", hits)

# # # Fetch from Neo4j
# # query = """
# # MATCH (a:Article)
# # WHERE a.article_id IN $ids
# # RETURN a.article_id, a.text
# # """

# # result = session.run(query, ids=article_ids)

# # for row in result:
#     # print(row["a.text"])



# from parser import ConstitutionParser
# from record_generator import ConstitutionRecordGenerator

# with open("text_extracted_ocr_output.txt", "r", encoding="utf8") as f:
#         text = f.read()

# parser = ConstitutionParser()
# parts = parser.parse(text)

# generator = ConstitutionRecordGenerator()

# records = generator.generate(parts)

# print("Total Records:", len(records))

# print(records[0])






from __future__ import annotations

from db.neo4j_store import Neo4jStore


class BNSGraphBuilder:

    DOCUMENT_ID = "BNS"

    def __init__(self, store: Neo4jStore):
        self.store = store

    # =====================================================
    # NODE FACTORY
    # =====================================================

    def _props(self, node_id: str, node_type: str, **kwargs):
        return {
            "id": node_id,
            "document": "BNS",
            "node_type": node_type,
            **kwargs
        }

    # =====================================================
    # BUILD ENTRY
    # =====================================================

    def build(self, bns):

        self._create_document()

        if getattr(bns, "parts", []):
            self._build_parts(bns)
        else:
            self._build_chapters(bns.chapters, self.DOCUMENT_ID)

    # =====================================================
    # DOCUMENT
    # =====================================================

    def _create_document(self):

        self.store.merge_node(
            label="Document",
            node_id=self.DOCUMENT_ID,
            properties=self._props(
                self.DOCUMENT_ID,
                "Document",
                name="Bharatiya Nyaya Sanhita, 2023"
            )
        )

    # =====================================================
    # PARTS
    # =====================================================

    def _build_parts(self, bns):

        for part in bns.parts:

            part_id = f"BNS-PART-{part.part_no}"

            self.store.merge_node(
                label="Part",
                node_id=part_id,
                properties=self._props(
                    part_id,
                    "Part",
                    part_no=part.part_no,
                    title=part.title,
                    text=part.text
                )
            )

            # Document → Part
            self.store.merge_relationship(
                self.DOCUMENT_ID,
                part_id,
                "HAS_PART"
            )

            self._build_chapters(part.chapters, part_id)

    # =====================================================
    # CHAPTERS
    # =====================================================

    def _build_chapters(self, chapters, parent_id):

        for chapter in chapters:

            chapter_id = f"BNS-CH-{chapter.chapter_no}"

            self.store.merge_node(
                label="Chapter",
                node_id=chapter_id,
                properties=self._props(
                    chapter_id,
                    "Chapter",
                    chapter_no=chapter.chapter_no,
                    title=chapter.chapter_title,
                    text=chapter.text
                )
            )

            # Parent → Child (IMPORTANT for retrieval)
            self.store.merge_relationship(
                parent_id,
                chapter_id,
                "HAS_CHAPTER"
            )

            self._build_sections(chapter.sections, chapter_id)

    # =====================================================
    # SECTIONS
    # =====================================================

    def _build_sections(self, sections, chapter_id):

        for section in sections:

            section_id = f"BNS-{section.section_no}"

            self.store.merge_node(
                label="Section",
                node_id=section_id,
                properties=self._props(
                    section_id,
                    "Section",
                    section_no=section.section_no,
                    title=section.section_title,
                    text=section.text
                )
            )

            # Parent → Child (CRITICAL FIX)
            self.store.merge_relationship(
                chapter_id,
                section_id,
                "HAS_SECTION"
            )

            # Optional reverse link (useful for normalization)
            self.store.merge_relationship(
                section_id,
                chapter_id,
                "BELONGS_TO"
            )

            self._build_clauses(section, section_id)
            self._build_explanations(section, section_id)
            self._build_illustrations(section, section_id)
            self._build_references(section, section_id)

    # =====================================================
    # CLAUSES
    # =====================================================

    def _build_clauses(self, section, section_id):

        for clause in getattr(section, "clauses", []):

            clause_id = f"{section_id}({clause.clause_no})"

            self.store.merge_node(
                label="Clause",
                node_id=clause_id,
                properties=self._props(
                    clause_id,
                    "Clause",
                    clause_no=clause.clause_no,
                    text=clause.text
                )
            )

            self.store.merge_relationship(
                section_id,
                clause_id,
                "HAS_CLAUSE"
            )

            self.store.merge_relationship(
                clause_id,
                section_id,
                "BELONGS_TO"
            )

            self._build_subclauses(clause, clause_id)

    # =====================================================
    # SUBCLAUSES
    # =====================================================

    def _build_subclauses(self, clause, clause_id):

        for sub in getattr(clause, "sub_clauses", []):

            sub_id = f"{clause_id}({sub.sub_clause_no})"

            self.store.merge_node(
                label="SubClause",
                node_id=sub_id,
                properties=self._props(
                    sub_id,
                    "SubClause",
                    sub_clause_no=sub.sub_clause_no,
                    text=sub.text
                )
            )

            self.store.merge_relationship(
                clause_id,
                sub_id,
                "HAS_SUBCLAUSE"
            )

            self.store.merge_relationship(
                sub_id,
                clause_id,
                "BELONGS_TO"
            )

            self._build_roman_clauses(sub, sub_id)

    # =====================================================
    # ROMAN CLAUSES
    # =====================================================

    def _build_roman_clauses(self, sub, sub_id):

        for roman in getattr(sub, "roman_clauses", []):

            roman_id = f"{sub_id}({roman.roman_no})"

            self.store.merge_node(
                label="RomanClause",
                node_id=roman_id,
                properties=self._props(
                    roman_id,
                    "RomanClause",
                    roman_no=roman.roman_no,
                    text=roman.text
                )
            )

            self.store.merge_relationship(
                sub_id,
                roman_id,
                "HAS_ROMANCLAUSE"
            )

            self.store.merge_relationship(
                roman_id,
                sub_id,
                "BELONGS_TO"
            )

    # =====================================================
    # EXPLANATIONS
    # =====================================================

    def _build_explanations(self, section, section_id):

        for idx, explanation in enumerate(
            getattr(section, "explanations", []),
            start=1
        ):

            explanation_id = f"{section_id}-EXPL-{idx}"

            self.store.merge_node(
                label="Explanation",
                node_id=explanation_id,
                properties=self._props(
                    explanation_id,
                    "Explanation",
                    text=explanation.text
                )
            )

            self.store.merge_relationship(
                section_id,
                explanation_id,
                "HAS_EXPLANATION"
            )

            self.store.merge_relationship(
                explanation_id,
                section_id,
                "BELONGS_TO"
            )

    # =====================================================
    # ILLUSTRATIONS
    # =====================================================

    def _build_illustrations(self, section, section_id):

        for idx, illustration in enumerate(
            getattr(section, "illustrations", []),
            start=1
        ):

            illustration_id = f"{section_id}-ILL-{idx}"

            self.store.merge_node(
                label="Illustration",
                node_id=illustration_id,
                properties=self._props(
                    illustration_id,
                    "Illustration",
                    illustration_no=getattr(illustration, "illustration_no", None),
                    text=illustration.text
                )
            )

            self.store.merge_relationship(
                section_id,
                illustration_id,
                "HAS_ILLUSTRATION"
            )

            self.store.merge_relationship(
                illustration_id,
                section_id,
                "BELONGS_TO"
            )

    # =====================================================
    # REFERENCES
    # =====================================================

    def _build_references(self, section, section_id):

        for ref in getattr(section, "references", []):

            target_section = getattr(ref, "section_no", None)

            if not target_section:
                continue

            target_id = f"BNS-{target_section}"

            self.store.merge_relationship(
                section_id,
                target_id,
                "REFERENCES"
            )