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

from dataclasses import dataclass

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


@dataclass
class PolynomialSetup:
    """The polynomial carrier, exact base, and checked registration inputs."""

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

    def register_polynomials(self, name):
        """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:
            JudgmentError: 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(s, base_kind=None, carrier_name="Polynomial", prefix="P"):
    """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.
    Without `base_kind`, it first creates the original Q base with explicit axioms.
    """
    if base_kind is None:
        q = numeric_setup(s, "Q", rational=True)
        q.register_rationals("rationals")
        base_kind = q.kind
    base = s.arithmetic(base_kind)
    base.rational(0, 1)
    P = s.declare_type(carrier_name)
    zero = s.symbol(prefix + ".zero", P)
    one = s.symbol(prefix + ".one", P)
    binary = arrow(P, arrow(P, P))
    add = s.symbol(prefix + ".add", binary)
    mul = s.symbol(prefix + ".mul", binary)
    neg = s.symbol(prefix + ".neg", arrow(P, P))
    coeff = s.symbol(prefix + ".coeff", arrow(base_kind, P))
    indet = s.symbol(prefix + ".indet", arrow(base_kind, P))
    numbers = s.arithmetic(P, zero, one, add, mul, neg)
    entries = assume_commutative_ring_laws(s, P, zero, one, prefix)
    names = ["coeff_zero", "coeff_one", "coeff_add", "coeff_mul"]
    arities = [0, 0, 2, 2]
    for i in range(len(names)):
        law = s.axiom(prefix + "." + names[i])
        x = law.var("x", base_kind) if arities[i] > 0 else zero
        y = law.var("y", base_kind) if arities[i] > 1 else zero
        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))
