from __future__ import annotations
from typing import TYPE_CHECKING
from . import media_state_prompt, po_image_supplement, text_ar, text_list_completion
from . import grounding, identifier_spelling
from .backend import ClairicFlashModel
from .media import media_warmup_groups

if TYPE_CHECKING:
    from typing import Any
    from .api import PredictRequest
    from .backend import EncodedRequest


class Clairic27BModel(ClairicFlashModel):
    RELEASE_MODEL_ID = "clairic-27b"
    COMPACT_LIST_PROMPTS = False
    DATE_ROLE_GUIDANCE = False
    SOURCE_ROW_ELIGIBILITY = True
    FINANCIAL_SERVICE_GUIDANCE = False
    FINANCIAL_FEE_GUIDANCE = True
    SPELL_VISUAL_PO_REFERENCES = True
    FOCUS_SPARSE_IMAGES = False
    PRESERVE_IMAGE_ENTITY_NAMES = True
    FOCUS_IMAGE_PARTY_NAMES = False
    LITERAL_IMAGE_PAYMENT_ROWS = True
    BIND_IMAGE_FINANCIAL_LINES = False
    IMAGE_TABLE_ROW_SCALE = 4
    BIND_IMAGE_RAGGED_ROWS = False

    def encode(self, request: PredictRequest, **options: Any) -> EncodedRequest:
        supplement_disabled = options.pop("po_supplement_disabled", False)
        encoded = self._encode_without_po_supplement(request, **options)
        detail = any(
            (
                options.get(name)
                for name in (
                    "identifier_view",
                    "party_name_view",
                    "physical_row_view",
                    "identifier_spelling_disabled",
                    "pdf_spelling_view",
                    "po_detail_view",
                )
            )
        )
        if (
            type(self) is not Clairic27BModel
            or supplement_disabled
            or detail
            or (not po_image_supplement.selected_fields(request, encoded))
        ):
            return encoded
        if (
            self.settings.model_id != "clairic-27b"
            or self.max_concurrency != self.settings.max_concurrency
            or self.max_concurrency != 1
        ):
            return encoded
        return po_image_supplement.prepare(
            request,
            encoded,
            encode_child=lambda child: self.encode(child, po_supplement_disabled=True),
            tokenizer=self.tokenizer,
            max_length=self.settings.max_length,
            media_groups=media_warmup_groups,
        )

    def _encode_without_po_supplement(
        self, request: PredictRequest, **options: Any
    ) -> EncodedRequest:
        detail = any(
            (
                options.get(name)
                for name in (
                    "identifier_view",
                    "party_name_view",
                    "physical_row_view",
                    "identifier_spelling_disabled",
                    "pdf_spelling_view",
                    "po_detail_view",
                )
            )
        )
        if type(self) is Clairic27BModel and options.get("po_detail_view"):
            options = {**options, "pdf_spelling_view": True}
        encoded = super().encode(request, **options)
        if type(self) is not Clairic27BModel or detail:
            return encoded
        (record, forecasts, tasks, typed, eos) = encoded
        active = {name for (name, _, _) in tasks}
        contracts = record.get("_meta", {}).get("identifier_spelling", {})
        po_fields = {
            name: request.fields[name]
            for (name, contract) in contracts.items()
            if name in active
            and contract.get("wire") == identifier_spelling.VERSION
            and (len(request.images) in (1, 2))
            and (not request.documents)
        }
        number_fields = {
            name: spec
            for (name, spec) in request.fields.items()
            if name in active
            and spec.get("type") == "number"
            and (spec.get("source_only") is True)
            and (not spec.get("knowledge"))
            and (not grounding.state_only(spec))
            and (len(request.images) > 1 or request.documents)
        }
        selected = {**po_fields, **number_fields}
        if not selected:
            return encoded
        originals = record.get("_original_images", ())
        if len(originals) < len(request.images) or (
            not request.documents and len(originals) != len(request.images)
        ):
            raise ValueError(
                "27B scalar detail must retain every original image in order"
            )
        (child, _, replacements, child_typed, child_eos) = super().encode(
            request.model_copy(update={"fields": selected}),
            identifier_view=True,
            pdf_spelling_view=True,
            image_views=lambda: originals,
            image_view_key="27b-literal-scalars-all-content2-v1",
        )
        if child_typed is not None or child_eos != eos:
            raise ValueError(
                "27B scalar detail must retain the original scalar AR contract"
            )
        by_name = {task[0]: task for task in replacements}
        if set(by_name) != set(selected) or len(replacements) != len(selected):
            raise ValueError(
                "27B scalar detail tasks differ from selected public fields"
            )
        updated = [by_name.get(task[0], task) for task in tasks]
        salts = dict(record.get("_ar_task_salts", {}))
        for name in selected:
            record["fields"][name] = child["fields"][name]
            record["_ar_constraints"][name] = child["_ar_constraints"][name]
            salts[name] = child["_cache_salt"]
        record["_meta"]["model_document_scalar_view"] = {
            "version": "27b-literal-scalars-all-content2-v1",
            "fields": list(selected),
            "po_fields": list(po_fields),
            "number_fields": list(number_fields),
            "image_count": len(originals),
        }
        record.update(
            _ar_tasks=updated,
            _ar_task_salts=salts,
            _ar_prefix=[],
            _ar_group_prefixes=[],
        )
        record["_ar_media_groups"] = media_warmup_groups(
            updated,
            default_salt=record["_cache_salt"],
            task_salts=salts,
            annotations=record["_meta"]["extraction_contract"]["fields"],
        )
        return (record, forecasts, updated, typed, eos)

    def _encode_request(self, request: PredictRequest) -> EncodedRequest:
        encoded = super()._encode_request(request)
        if (
            type(self) is Clairic27BModel
            and self.settings.model_id == "clairic-27b"
            and (self.max_concurrency == self.settings.max_concurrency == 1)
            and bool(
                getattr(request, "images", ()) or getattr(request, "documents", ())
            )
        ):
            encoded[0]["_first_item_list_gating"] = True
            fallback = encoded[0].get("_identifier_spelling_fallback")
            if fallback is not None:
                fallback["_first_item_list_gating"] = True
            encoded = media_state_prompt.prepare(
                request,
                encoded,
                tokenizer=self.tokenizer,
                max_length=self.settings.max_length,
                media_groups=media_warmup_groups,
            )
        return encoded

    async def _ar_all(
        self, record: dict[str, Any], eos: int
    ) -> dict[str, dict[str, Any]]:
        caller_record = record
        if len(record["_ar_tasks"]) < 64 and (not record.get("_media_payload")):
            record = {**record, "_ar_prefix": None, "_ar_group_prefixes": []}
        if (
            type(self) is Clairic27BModel
            and self.settings.model_id == "clairic-27b"
            and (self.max_concurrency == self.settings.max_concurrency == 1)
            and text_list_completion.eligible(record, record["_ar_tasks"])
        ):
            predictions = await text_ar.text_ar_all(self, record, eos)
            if record is not caller_record:
                caller_record["_unexecuted_list_items"] = record[
                    "_unexecuted_list_items"
                ]
            return predictions
        return await super()._ar_all(record, eos)
