"""Recover unresolved image rows backed by a completed row inventory."""

from copy import deepcopy
import json
from typing import Any

from . import ar, image_rows
from .schema import MAX_LIST_ITEMS


def prepare(
    record: dict[str, Any], predictions: dict[str, dict[str, Any]]
) -> tuple[dict[str, Any], list[dict[str, Any]]] | None:
    if record.get("_null_recovery") or record.get("_row_omission_recovery"):
        return None
    annotations = (
        record.get("_meta", {}).get("extraction_contract", {}).get("fields", {})
    )
    tasks = {name: (name, ids, media) for name, ids, media in record["_ar_tasks"]}
    if len(tasks) != len(record["_ar_tasks"]):
        return None
    contexts, retry_tasks = [], []
    constraints = dict(record.get("_ar_constraints", {}))
    for parent, annotation in annotations.items():
        queries = annotation.get("image_row_fields")
        spec, inventory = record["fields"].get(parent, {}), predictions.get(parent, {})
        value = inventory.get("value")
        if (
            set(annotation) != {"image_row_fields"}
            or not isinstance(queries, list)
            or len(queries) != len(set(queries))
            or not queries
            or not image_rows.is_inventory(spec)
            or spec.get("knowledge")
            or not inventory.get("valid")
            or not inventory.get("complete")
            or not isinstance(value, list)
            or not value
            or len(value) > min(spec.get("max_items", MAX_LIST_ITEMS), len(queries))
            or any(
                not isinstance(row, dict)
                or set(row) != {"label"}
                or not isinstance(row["label"], str)
                or not row["label"].strip()
                for row in value
            )
            or any(child not in predictions for child in queries)
        ):
            continue
        properties = spec.get("items", {}).get("properties", {})
        text_keys = [
            key
            for key, child in properties.items()
            if child.get("type") == "text" and not child.get("knowledge")
        ]
        if not text_keys:
            continue
        labels = [row["label"] for row in value]
        proposals = [predictions[child] for child in queries]
        selected, conflicts = image_rows._select(labels, proposals, text_keys)
        if not conflicts or any(
            row is not None and (not row.get("valid") or not row.get("complete"))
            for row in selected
        ):
            continue
        children, local_constraints = [], {}
        for index in conflicts:
            child = queries[index]
            row, child_spec = predictions[child], record["fields"].get(child, {})
            row_value = row.get("value")
            task = tasks.get(child)
            native = constraints.get(child, {})
            if (
                annotations.get(child) != {"parent": parent, "index": index + 1}
                or child_spec.get("type") != "object"
                or not child_spec.get("_image_row_proposal")
                or child_spec.get("knowledge")
                or not row.get("valid")
                or not row.get("complete")
                or not isinstance(row_value, dict)
                or not row_value
                or any(cell is not None for cell in row_value.values())
                or any(row is previous for previous in selected if previous is not None)
                or task is None
                or task[2] is None
                or set(native) != {"json_schema"}
            ):
                break
            schema = json.loads(native["json_schema"])
            if schema != ar.schema(child_spec) or not set(text_keys) <= set(
                schema.get("properties", {})
            ):
                break
            # A completed inventory requires a matching textual cell; the
            # original selector remains the acceptance test for that label.
            # Full branches retain required fields with the native grammar compiler.
            branches = []
            for key in text_keys:
                branch = deepcopy(schema)
                branch["properties"][key]["type"] = "string"
                branches.append(branch)
            local_constraints[child] = {"json_schema": json.dumps({"anyOf": branches})}
            children.append(child)
        else:
            constraints.update(local_constraints)
            retry_tasks.extend(tasks[child] for child in children)
            contexts.append(
                {
                    "parent": parent,
                    "queries": queries,
                    "labels": labels,
                    "text_keys": text_keys,
                    "selected": selected,
                    "conflicts": conflicts,
                    "children": children,
                }
            )
    if not retry_tasks:
        return None
    repair = {
        **record,
        "_ar_tasks": retry_tasks,
        "_ar_constraints": constraints,
        "_ar_prefix": None,
        "_ar_group_prefixes": [],
        "_ar_media_groups": [],
        "_null_recovery": True,
        "_row_omission_recovery": True,
    }
    return repair, contexts


def merge(
    record: dict[str, Any],
    predictions: dict[str, dict[str, Any]],
    alternatives: dict[str, dict[str, Any]],
    plan: tuple[dict[str, Any], list[dict[str, Any]]],
) -> tuple[dict[str, dict[str, Any]], list[dict[str, Any]]]:
    repair, contexts = plan
    lengths = {name: len(ids) for name, ids, _ in repair["_ar_tasks"]}
    result = dict(predictions)
    evidence = []
    for context in contexts:
        candidates = {}
        for child in context["children"]:
            original, alternative = predictions[child], alternatives.get(child, {})
            account = {
                "tokens": original["tokens"] + alternative.get("tokens", 0),
                "retry_input_tokens": original.get("retry_input_tokens", 0)
                + lengths[child],
            }
            result[child] = {**original, **account}
            value = alternative.get("value")
            if (
                alternative.get("valid")
                and alternative.get("complete")
                and isinstance(value, dict)
                and any(cell is not None for cell in value.values())
            ):
                candidates[child] = {
                    **alternative,
                    **account,
                    "image_row_omission_recovered": True,
                }
        trial = {**result, **candidates}
        selected, conflicts = image_rows._select(
            context["labels"],
            [trial[name] for name in context["queries"]],
            context["text_keys"],
        )
        unchanged = all(
            previous is None or selected[index] is previous
            for index, previous in enumerate(context["selected"])
        )
        accepted = (
            not conflicts and unchanged and len(candidates) == len(context["children"])
        )
        if accepted:
            result.update(candidates)
        evidence.append(
            {
                "parent": context["parent"],
                "original_conflicts": context["conflicts"],
                "retry_children": context["children"],
                "conflicts_after_retry": conflicts,
                "previously_matched_proposals_preserved_exactly": unchanged,
                "accepted": accepted,
            }
        )
    return result, evidence
