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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
"""
agents.py β€” Specialist document extraction agents for UK Motor Insurance.

Architecture
────────────
  PDF path
    β†’ docling  (PDF β†’ Markdown)
    β†’ PIIMasker.mask()
    β†’ InsuranceExtractionAgents.classify_document()  [LLM: llama-3.1-8b-instant]
    β†’ extract_schedule() | extract_certificate()     [LLM: llama-4-scout-17b]
    β†’ UKMotorGoldenRecord (with source_document provenance)
"""
from __future__ import annotations

import json
import logging
import os
import time
from pathlib import Path
from typing import Any

import instructor
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import PdfPipelineOptions
from docling.document_converter import DocumentConverter, PdfFormatOption
from groq import Groq
from pydantic import ValidationError

from privacy import PIIMasker
from prompts import PromptRegistry
from schema import DocumentType, SourceMetadata, UKMotorGoldenRecord
from settings import settings

logger = logging.getLogger(__name__)

# ---------------------------------------------------------------------------
# Groq clients β€” extraction (instructor-wrapped) + classifier (raw Groq)
# ---------------------------------------------------------------------------


def _build_extraction_client() -> instructor.Instructor:
    api_key = os.environ.get("GROQ_API_KEY")
    if not api_key:
        raise EnvironmentError(
            "GROQ_API_KEY environment variable is not set. "
            "Export it before running the pipeline."
        )
    return instructor.from_groq(Groq(api_key=api_key), mode=instructor.Mode.JSON)


def _build_groq_client() -> Groq:
    api_key = os.environ.get("GROQ_API_KEY")
    if not api_key:
        raise EnvironmentError(
            "GROQ_API_KEY environment variable is not set. "
            "Export it before running the pipeline."
        )
    return Groq(api_key=api_key)


# Models resolved at import time from settings.yaml / env vars
_EXTRACTION_MODEL: str = settings.llm.model
_CLASSIFIER_MODEL: str = settings.llm.classifier_model


def _build_docling_converter() -> DocumentConverter:
    """Build a DocumentConverter configured from settings.docling."""
    opts = PdfPipelineOptions()
    opts.do_ocr = settings.docling.do_ocr
    opts.do_table_structure = settings.docling.do_table_structure
    return DocumentConverter(
        format_options={InputFormat.PDF: PdfFormatOption(pipeline_options=opts)}
    )

# ---------------------------------------------------------------------------
# Document type classifier (keyword heuristic β€” fast, zero API calls)
# ---------------------------------------------------------------------------

_CLASSIFICATION_KEYWORDS: dict[DocumentType, list[str]] = {
    DocumentType.SCHEDULE: [
        # Phrases that only appear in a Schedule, not in a Certificate
        "policy schedule",
        "schedule of insurance",
        "schedule number",
        "premium payable",
        "compulsory excess",
        "voluntary excess",
        "no claims bonus",
        "ncb",
        "windscreen excess",
    ],
    DocumentType.CERTIFICATE: [
        # Phrases that are definitive for a Certificate document
        "certificate of motor insurance",
        "motor insurance certificate",
        "certificate number",
        "persons entitled to drive",
        "class of use",
        "road traffic act",
        "this is to certify",
    ],
    DocumentType.STATEMENT_OF_FACT: [
        "statement of fact",
        "statement of insurance",
        "proposal form",
        "claims history",
        "motoring convictions",
        "annual mileage",
    ],
    DocumentType.POLICY_BOOKLET: [
        "policy booklet",
        "policy wording",
        "terms and conditions",
        "what is covered",
        "general conditions",
        "complaints procedure",
    ],
}


def _keyword_classify(text: str) -> str:
    """Keyword heuristic fallback classifier. Returns DocumentType.value string."""
    lower = text.lower()
    scores: dict[DocumentType, int] = {dt: 0 for dt in _CLASSIFICATION_KEYWORDS}

    for doc_type, keywords in _CLASSIFICATION_KEYWORDS.items():
        for kw in keywords:
            if kw in lower:
                scores[doc_type] += 1

    best_type, best_score = max(scores.items(), key=lambda kv: kv[1])
    return best_type.value if best_score > 0 else DocumentType.UNKNOWN.value


def _str_to_doc_type(s: str) -> DocumentType:
    """Convert a string to DocumentType, falling back to UNKNOWN."""
    try:
        return DocumentType(s)
    except ValueError:
        return DocumentType.UNKNOWN


# ---------------------------------------------------------------------------
# Extraction failure sentinel
# ---------------------------------------------------------------------------


class ExtractionFailedError(RuntimeError):
    """
    Raised when the LLM fails to produce a valid UKMotorGoldenRecord after
    exhausting all retries.  Callers should treat the document as failed and
    skip it rather than propagating an empty record silently.
    """


# ---------------------------------------------------------------------------
# InsuranceExtractionAgents
# ---------------------------------------------------------------------------


class InsuranceExtractionAgents:
    """
    Specialist extraction agents for UK Motor Insurance documents.

    Uses two LLM models:
    - llama-3.1-8b-instant     β€” fast document type classification
    - llama-4-scout-17b-16e    β€” deep structured extraction (Schedule / Certificate)

    Parameters
    ----------
    masker : PIIMasker | None
    max_retries : int
    prompt_registry : PromptRegistry | None
    debug_dir : Path | None
    """

    def __init__(
        self,
        masker: PIIMasker | None = None,
        max_retries: int = settings.llm.max_retries,
        prompt_registry: PromptRegistry | None = None,
        debug_dir: Path | None = None,
    ) -> None:
        self._client = _build_extraction_client()
        self._groq = _build_groq_client()
        self._masker = masker or PIIMasker()
        self._max_retries = max_retries
        self._prompts = prompt_registry or PromptRegistry()
        self._converter = _build_docling_converter()
        self._debug_dir = debug_dir

    # ------------------------------------------------------------------
    # Public API
    # ------------------------------------------------------------------

    def classify_document(self, markdown_text: str) -> str:
        """
        Use llama-3.1-8b-instant to classify the document type.

        The LLM is the primary classifier.  If it fails or returns an invalid
        label, the keyword heuristic is used as a fallback.  A discrepancy
        between the two is logged as a warning to flag low-confidence cases.

        Returns one of: "Schedule", "Certificate", "StatementOfFact",
        "PolicyBooklet", "Unknown".
        """
        keyword_result = _keyword_classify(markdown_text)

        system_prompt = (
            "You are a UK motor insurance document classifier.\n"
            "Given the document text, respond with EXACTLY one word from:\n"
            "Schedule | Certificate | StatementOfFact | PolicyBooklet | Unknown\n\n"
            "- Schedule: Policy Schedule \u2014 excess figures, premium, NCB, "
            "vehicle details, driver ages/DOBs.\n"
            "- Certificate: Certificate of Motor Insurance \u2014 Road Traffic Act, "
            "'persons entitled to drive', 'class of use'.\n"
            "- StatementOfFact: Statement of Fact / Proposal \u2014 claims history, "
            "convictions, annual mileage.\n"
            "- PolicyBooklet: Policy Booklet / Wording \u2014 terms and conditions, "
            "'what is covered', complaints.\n"
            "- Unknown: Cannot determine.\n\n"
            "Respond with ONLY the single classification word. No punctuation."
        )
        try:
            response = self._groq.chat.completions.create(
                model=_CLASSIFIER_MODEL,
                messages=[
                    {"role": "system", "content": system_prompt},
                    {
                        "role": "user",
                        "content": "Classify this document:\n\n" + markdown_text[:4000],
                    },
                ],
                max_tokens=10,
                temperature=0,
            )
            llm_result = response.choices[0].message.content.strip().split()[0]
            valid = {"Schedule", "Certificate", "StatementOfFact", "PolicyBooklet", "Unknown"}
            if llm_result in valid:
                if llm_result != keyword_result:
                    logger.warning(
                        "Classifier discrepancy: LLM=%s, keyword=%s "
                        "(using LLM result β€” verify document type)",
                        llm_result, keyword_result,
                    )
                else:
                    logger.debug("Classifier agreement: LLM=%s \u2713", llm_result)
                return llm_result
            logger.warning(
                "LLM classifier returned '%s' \u2014 falling back to keyword heuristic", llm_result
            )
        except Exception as exc:  # noqa: BLE001
            logger.warning(
                "LLM classifier failed (%s) \u2014 falling back to keyword heuristic", exc
            )
        return keyword_result

    def extract_schedule(self, markdown_text: str, filename: str) -> UKMotorGoldenRecord:
        """
        Extract all financial, vehicle, and driver risk data from a Policy Schedule.

        Instructs the LLM to:
        - Compute total_accidental_damage = standard_compulsory + voluntary
        - Extract driver DOBs and distinguish Full UK vs UK Provisional licence types
        - Separate fire excess from theft excess (they can differ)
        - Extract own_repairer_additional_excess if present
        - Extract premium breakdown and optional extras (float if purchased,
          "Not Selected" if not)
        """
        return self._extract(
            markdown_text,
            filename,
            DocumentType.SCHEDULE,
            self._prompts.get(DocumentType.SCHEDULE),
        )

    def extract_certificate(self, markdown_text: str, filename: str) -> UKMotorGoldenRecord:
        """
        Extract legal permissions from a Certificate of Motor Insurance.

        Instructs the LLM to:
        - Extract the exact "Limitations as to use" / class_of_use clause verbatim
        - Extract the policy_number for cross-reference
        - Record driving_other_cars entitlement (true/false)
        - Leave all financial fields (excess, premium, NCB) as null
        """
        return self._extract(
            markdown_text,
            filename,
            DocumentType.CERTIFICATE,
            self._prompts.get(DocumentType.CERTIFICATE),
        )

    def process(self, pdf_path: str | Path) -> tuple[UKMotorGoldenRecord, str]:
        """
        Full pipeline for one PDF: PDF β†’ Markdown β†’ PII mask β†’ classify β†’ extract.

        Returns
        -------
        tuple[UKMotorGoldenRecord, str]
            The extracted record and the document type string (e.g. "Schedule").

        Raises
        ------
        ExtractionFailedError
            When the LLM fails to extract a valid record after all retries.
        """
        record, doc_type_str, _ = self._process_internal(Path(pdf_path), build_corpus=False)
        return record, doc_type_str

    # ------------------------------------------------------------------
    # Private helpers
    # ------------------------------------------------------------------

    def _process_internal(
        self,
        pdf_path: Path,
        build_corpus: bool,
    ) -> tuple[UKMotorGoldenRecord, str, Any]:
        """
        Unified core pipeline: PDF β†’ Markdown β†’ PII mask β†’ classify β†’ extract,
        optionally building a ProvenanceCorpus from the raw Docling IR.

        Parameters
        ----------
        pdf_path : Path
        build_corpus : bool
            When True, builds a ProvenanceCorpus before PII masking so the
            original text is available for fuzzy matching.

        Returns
        -------
        tuple[UKMotorGoldenRecord, str, ProvenanceCorpus | None]
            (record, doc_type_str, corpus_or_None)

        Raises
        ------
        ExtractionFailedError
            Propagated from _extract() when the LLM fails after all retries.
        """
        from provenance import ProvenanceCorpus  # local import β€” avoids circular dep

        logger.info("Processing%s: %s", " (with provenance)" if build_corpus else "", pdf_path.name)

        # Pre-classify from filename for page-cap selection (no API call)
        pre_type_str = _keyword_classify(pdf_path.stem)
        pre_doc_type = _str_to_doc_type(pre_type_str)
        logger.debug("  Pre-classified from filename: %s", pre_type_str)

        # PDF β†’ Markdown + raw DoclingDocument
        markdown, raw_doc = self._pdf_to_markdown_and_doc(pdf_path, pre_doc_type)

        # Build corpus from original text BEFORE masking (critical for accurate fuzzy match)
        corpus: Any = None
        if build_corpus:
            corpus = ProvenanceCorpus(source_filename=pdf_path.name, doc_type=pre_type_str)
            corpus.add_from_docling(raw_doc, pdf_path.name)
            logger.debug("  Provenance corpus: %d items", len(corpus.items))

        if self._debug_dir and settings.debug.save_markdown:
            _write_debug(self._debug_dir, f"{pdf_path.name}.md", markdown)

        # PII mask
        masked_markdown, _token_map = self._masker.mask(markdown)
        if self._debug_dir and settings.debug.save_masked_markdown:
            _write_debug(self._debug_dir, f"{pdf_path.name}.masked.md", masked_markdown)

        # Classify
        t0 = time.monotonic()
        doc_type_str = self.classify_document(masked_markdown)
        logger.info("  Classified as: %s", doc_type_str)

        # Route to specialist extractor
        if doc_type_str == "Schedule":
            record = self.extract_schedule(masked_markdown, pdf_path.name)
        elif doc_type_str == "Certificate":
            record = self.extract_certificate(masked_markdown, pdf_path.name)
        else:
            logger.info("  Non-primary type '%s' β€” running generic extraction", doc_type_str)
            record = self._extract(
                masked_markdown,
                pdf_path.name,
                _str_to_doc_type(doc_type_str),
                self._prompts.get(_str_to_doc_type(doc_type_str)),
            )

        elapsed = round(time.monotonic() - t0, 3)

        record.source_document = SourceMetadata(
            document_type=_str_to_doc_type(doc_type_str),
            filename=pdf_path.name,
        )

        if self._debug_dir and settings.debug.save_extraction_json:
            _write_debug(
                self._debug_dir,
                f"{pdf_path.name}.extraction.json",
                record.model_dump_json(indent=2),
            )
            fc = getattr(record, "field_citations", None) or {}
            logger.info("  field_citations populated by LLM: %d entries", len(fc))
            if fc:
                import json as _json
                _write_debug(
                    self._debug_dir,
                    f"{pdf_path.name}.field_citations.json",
                    _json.dumps(fc, indent=2, ensure_ascii=False),
                )

        if self._debug_dir and settings.debug.save_metrics:
            metrics: dict = {
                "filename": pdf_path.name,
                "doc_type": doc_type_str,
                "extraction_model": _EXTRACTION_MODEL,
                "classifier_model": _CLASSIFIER_MODEL,
                "response_time_seconds": elapsed,
            }
            if corpus is not None:
                metrics["corpus_items"] = len(corpus.items)
            _append_metrics(self._debug_dir, metrics)

        return record, doc_type_str, corpus

    def _pdf_to_markdown(
        self, pdf_path: Path, doc_type: DocumentType = DocumentType.UNKNOWN
    ) -> str:
        """Convert a PDF to Markdown using docling, respecting per-doc-type page caps."""
        markdown, _ = self._pdf_to_markdown_and_doc(pdf_path, doc_type)
        return markdown

    def _pdf_to_markdown_and_doc(
        self, pdf_path: Path, doc_type: DocumentType = DocumentType.UNKNOWN
    ) -> tuple[str, Any]:
        """Convert PDF to Markdown and also return the raw DoclingDocument for provenance."""
        # Apply page cap during conversion (not just in Markdown export) to prevent
        # Docling's layout model from running out of memory on large PDFs (Policy Booklet).
        max_pg = settings.docling.max_pages.get(doc_type.value)
        convert_kwargs: dict[str, Any] = {}
        if max_pg is not None:
            convert_kwargs["max_num_pages"] = max_pg

        result = self._converter.convert(str(pdf_path), **convert_kwargs)
        doc = result.document
        markdown = doc.export_to_markdown()

        if max_pg is not None:
            separator = "\n---\n"
            parts = markdown.split(separator)
            if len(parts) > max_pg:
                logger.info(
                    "  Page cap applied: %s capped at %d/%d pages",
                    pdf_path.name, max_pg, len(parts),
                )
                markdown = separator.join(parts[:max_pg])

        return markdown, doc

    def process_with_provenance(
        self, pdf_path: str | Path
    ) -> tuple[UKMotorGoldenRecord, str, Any]:
        """
        Like process() but also returns a ProvenanceCorpus built from the Docling IR.

        The corpus is constructed *before* PII masking so that the original text
        strings (not masked tokens) are available for fuzzy matching.

        Returns
        -------
        tuple[UKMotorGoldenRecord, str, ProvenanceCorpus]
            (record, doc_type_str, corpus)

        Raises
        ------
        ExtractionFailedError
            When the LLM fails to extract a valid record after all retries.
        """
        return self._process_internal(Path(pdf_path), build_corpus=True)  # type: ignore[return-value]

    def _extract(
        self,
        text: str,
        filename: str,
        doc_type: DocumentType,
        system_prompt: str,
    ) -> UKMotorGoldenRecord:
        """Call Groq via instructor to extract a UKMotorGoldenRecord."""
        user_message = (
            "Extract all motor insurance data from the following document text. "
            "Return a JSON object that strictly conforms to the UKMotorGoldenRecord schema.\n\n"
            f"--- DOCUMENT TEXT ---\n{text}\n--- END ---"
        )
        try:
            record: UKMotorGoldenRecord = self._client.chat.completions.create(
                model=_EXTRACTION_MODEL,
                response_model=UKMotorGoldenRecord,
                max_retries=self._max_retries,
                messages=[
                    {"role": "system", "content": system_prompt.strip()},
                    {"role": "user", "content": user_message},
                ],
            )
        except (ValidationError, Exception) as exc:
            raise ExtractionFailedError(
                f"Extraction failed for {doc_type.value!r} document '{filename}' "
                f"after {self._max_retries} retries: {exc}"
            ) from exc
        return record


# ---------------------------------------------------------------------------
# Debug helpers (module-level so they can be unit-tested independently)
# ---------------------------------------------------------------------------


def _write_debug(debug_dir: Path, filename: str, content: str) -> None:
    """Write a debug artifact to disk, silently skipping on any I/O error."""
    try:
        (debug_dir / filename).write_text(content, encoding="utf-8")
        logger.debug("Debug artifact saved: %s", filename)
    except OSError as exc:
        logger.warning("Could not write debug artifact %s: %s", filename, exc)


def _append_metrics(debug_dir: Path, metrics: dict) -> None:
    """Append a metrics dict as a JSONL line to extraction_metrics.jsonl."""
    try:
        with (debug_dir / "extraction_metrics.jsonl").open("a", encoding="utf-8") as fh:
            fh.write(json.dumps(metrics) + "\n")
    except OSError as exc:
        logger.warning("Could not write metrics: %s", exc)