"""Explicit teaching axioms for formal polynomials over an exact rational base."""

from judgment_shell import Arithmetic, ArithmeticLaws, Expr, LibError, Session, TypeExpr, arrow
from exact_arithmetic_setup import numeric_setup
from ring_assumptions import assume_commutative_ring_laws


@fieldwise_init
struct PolynomialSetup(Copyable):
    """The polynomial carrier, exact base, and checked registration inputs."""

    var kind: TypeExpr
    var numbers: Arithmetic
    var base: Arithmetic
    var coeff: Expr
    var indet: Expr
    var laws: ArithmeticLaws

    def register_polynomials(self, name: String) raises LibError -> Arithmetic:
        """Explicitly enable polynomial literals using this provider's base and laws.

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

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

        Raises:
            LibError: For invalid owners, operations, evidence, names, or exhausted
                work. Failure publishes no theory or profile change.
        """
        return self.numbers.polynomials(name, self.base, self.coeff, self.indet, self.laws)


def polynomial_setup(
    mut s: Session, base_kind: TypeExpr,
    carrier_name: String = "Polynomial", prefix: String = "P",
) raises LibError -> PolynomialSetup:
    """Assume polynomial laws over an existing exact rational base.

    The base must belong to this session and already support exact rationals.
    Check it before publishing the polynomial carrier or its assumptions.
    The caller chooses fresh names and explicitly registers polynomial literals.
    """
    var base = s.arithmetic(base_kind)
    _ = base.rational(0, 1)
    var P = s.declare_type(carrier_name)
    var zero = s.symbol(prefix + ".zero", P)
    var one = s.symbol(prefix + ".one", P)
    var binary = arrow(P, arrow(P, P))
    var add = s.symbol(prefix + ".add", binary)
    var mul = s.symbol(prefix + ".mul", binary)
    var neg = s.symbol(prefix + ".neg", arrow(P, P))
    var coeff = s.symbol(prefix + ".coeff", arrow(base_kind, P))
    var indet = s.symbol(prefix + ".indet", arrow(base_kind, P))
    var numbers = s.arithmetic(P, zero, one, add, mul, neg)
    var entries = assume_commutative_ring_laws(s, P, zero, one, prefix)
    var names: List[String] = ["coeff_zero", "coeff_one", "coeff_add", "coeff_mul"]
    var arities: List[Int] = [0, 0, 2, 2]
    for i in range(len(names)):
        var law = s.axiom(prefix + "." + names[i])
        var x = zero
        var y = zero
        if arities[i] > 0:
            x = law.var("x", base_kind)
        if arities[i] > 1:
            y = law.var("y", base_kind)
        var statement: Expr
        if i == 0:
            statement = coeff(base.number(0)) == zero
        elif i == 1:
            statement = coeff(base.number(1)) == one
        elif i == 2:
            statement = coeff(x + y) == coeff(x) + coeff(y)
        else:
            statement = coeff(x * y) == coeff(x) * coeff(y)
        entries.append((names[i], law.finish(statement)))
    return PolynomialSetup(P, numbers, base, coeff, indet, ArithmeticLaws(entries^))


def polynomial_setup(mut s: Session) raises -> PolynomialSetup:
    """Create the original Q base and Polynomial fixture with explicit axioms."""
    var q = numeric_setup(s, "Q", rational=True)
    _ = q.register_rationals("rationals")
    return polynomial_setup(s, base_kind=q.kind)
