"""Explicit teaching assumptions for reusable exact arithmetic profiles.

These are assumed ordered-ring/field laws over an abstract carrier, not a
construction of the mathematical integers or rationals.
"""

from __future__ import annotations

from dataclasses import dataclass

from judgment import Arithmetic, ArithmeticLaws, Expr, JudgmentError, LibErrorKind, Session, TypeExpr, arrow, iff, implies, not_
from ring_assumptions import assume_commutative_ring_laws


@dataclass
class NumericSetup:
    """Typed operations and the complete explicit law bundle."""

    kind: TypeExpr
    numbers: Arithmetic
    lt: Expr
    le: Expr
    inv: Optional[Expr]
    laws: ArithmeticLaws

    def register_integers(self, name):
        """Explicitly enable exact integers using this provider's operations and laws.

        Args:
            name: The client's fresh literal-kind name and private law-name prefix.

        Returns:
            The checked Arithmetic profile with canonical integer arithmetic enabled.

        Raises:
            LibError: For invalid operations, evidence, names, or exhausted work.
                Failure publishes no theory or profile change.
        """
        return self.numbers.integers(name, self.lt, self.le, self.laws)

    def register_rationals(self, name):
        """Explicitly enable exact rationals using this provider's operations and laws.

        Args:
            name: The client's fresh literal-kind name and private law-name prefix.

        Returns:
            The checked Arithmetic profile with canonical rational arithmetic enabled.

        Raises:
            LibError: UNSUPPORTED if this setup has no inverse; otherwise the
                registration's errors for invalid operations, evidence, names,
                or exhausted work. Failure publishes no theory or profile change.
        """
        if self.inv is None:
            raise LibError(LibErrorKind.UNSUPPORTED, "rational registration requires a setup with an inverse")
        return self.numbers.rationals(name, self.lt, self.le, self.inv, self.laws)


def numeric_setup(s, prefix, rational):
    """Declare operations and assume each law explicitly, before exact registration."""
    T = s.declare_type(prefix + ".T")
    zero = s.symbol(prefix + ".zero", T)
    one = s.symbol(prefix + ".one", T)
    binary = arrow(T, arrow(T, T))
    add = s.symbol(prefix + ".add", binary)
    mul = s.symbol(prefix + ".mul", binary)
    neg = s.symbol(prefix + ".neg", arrow(T, T))
    relation = arrow(T, arrow(T, s.prop_type()))
    lt = s.symbol(prefix + ".lt", relation)
    le = s.symbol(prefix + ".le", relation)
    inv = None
    if rational:
        inv = s.symbol(prefix + ".inv", arrow(T, T))
    numbers = s.arithmetic(T, zero, one, add, mul, neg)
    entries = assume_commutative_ring_laws(s, T, zero, one, prefix)
    names = ["lt_irrefl", "lt_trans", "lt_total", "lt_add", "lt_mul", "zero_lt_one", "le_iff"]
    arities = [1, 3, 2, 3, 2, 0, 2]
    if rational:
        names.append("mul_inverse")
        arities.append(1)
    for i in range(len(names)):
        law = s.axiom(prefix + "." + names[i])
        x = zero
        y = zero
        z = zero
        if arities[i] > 0:
            x = law.var("x", T)
        if arities[i] > 1:
            y = law.var("y", T)
        if arities[i] > 2:
            z = law.var("z", T)
        statement = None
        if i == 0:
            statement = not_(lt(x, x))
        elif i == 1:
            statement = implies(lt(x, y), implies(lt(y, z), lt(x, z)))
        elif i == 2:
            statement = lt(x, y) | ((x == y) | lt(y, x))
        elif i == 3:
            statement = implies(lt(x, y), lt(x + z, y + z))
        elif i == 4:
            statement = implies(lt(zero, x), implies(lt(zero, y), lt(zero, x * y)))
        elif i == 5:
            statement = lt(zero, one)
        elif i == 6:
            statement = iff(le(x, y), lt(x, y) | (x == y))
        else:
            statement = implies(not_(x == zero), x * inv(x) == one)
        entries.append((names[i], law.finish(statement)))
    return NumericSetup(T, numbers, lt, le, inv, ArithmeticLaws(entries))
