"""Phase 5 validation: UFO export of an SM-lite electroweak model.

Checks:
- the generated directory is a valid python package importable with the
  shipped object_library,
- internal parameters resolve numerically in dependency order,
- coupling values evaluate to the pinned SM numbers (i g m_W for hWW, ...),
- vertices reference existing particles/couplings/lorentz objects.
"""

import importlib
import sys

import pytest
import sympy as sp

from feynlag import (
    Dmu, ExternalParameter, InternalParameter, Lagrangian, Model,
    ParameterSet, Rotation, SU2, Scalar, U1, conjugate_pair, dag,
    rotation_2x2,
)
from feynlag.export.ufo import UFOParticle, write_ufo
from feynlag import verify_ufo_numeric


@pytest.fixture(scope="module")
def sm_ufo(tmp_path_factory):
    gw = ExternalParameter("gw", 0.6535, positive=True)
    # texs with a quote and backslashes (incl. the \t of \theta) pin that the
    # writer escapes them: an unquoted '{g'}' is a SyntaxError in the UFO
    g1 = ExternalParameter("g1", 0.3580, positive=True, tex="{g'}")
    SU2L, U1Y = SU2("SU2L", coupling=gw), U1("U1Y", coupling=g1)

    v = ExternalParameter("v", 246.0, positive=True, unit_dim=1)
    lam = ExternalParameter("lam", 0.129, tex=r"\lambda_{\theta}")
    mu2 = InternalParameter("mu2", unit_dim=2)
    MH = InternalParameter("MH", positive=True, unit_dim=1)
    MW = InternalParameter("MW", positive=True, unit_dim=1)
    MZ = InternalParameter("MZ", positive=True, unit_dim=1)
    MH.define(sp.sqrt(2 * lam.s) * v.s)
    MW.define(gw.s * v.s / 2)
    MZ.define(sp.sqrt(gw.s**2 + g1.s**2) * v.s / 2)

    H = Scalar("H", reps={SU2L: 2, U1Y: sp.Rational(1, 2)},
               component_names=["Gp", "H0"])
    H.expand_vev({H.components[1]: v})

    HdH = (dag(H) * H.mat)[0]
    V = -mu2.s * HdH + lam.s * HdH**2
    DH = Dmu(H)
    L = Lagrangian()
    L.add((dag(DH) * DH)[0], sector="kinetic")
    L.add(-V, sector="potential")

    W = SU2L.bosons("W")
    B = U1Y.bosons("B")
    model = Model("SMlite", gauge_groups=[SU2L, U1Y], fields=[H, W, B],
                  parameters=[gw, g1, v, lam, mu2, MH, MW, MZ],
                  lagrangian=L)
    model.solve_tadpoles([mu2])

    g, gp = gw.s, g1.s
    W1, W2, W3 = W.components
    Bc = B.components[0]
    Z, A = sp.symbols("Z A", real=True)
    thetaW = sp.atan(gp / g)
    model.rotate(Rotation([W3, Bc], [Z, A], rotation_2x2(-thetaW)))
    Wp, Wm = sp.symbols("Wp Wm")
    U = sp.Matrix([[1, -sp.I], [1, sp.I]]) / sp.sqrt(2)
    model.rotate(Rotation([W1, W2], [Wp, Wm], U, kind="unitary"))

    h = sp.Symbol("H0_r", real=True)
    G0 = sp.Symbol("H0_i", real=True)
    Gp = H.components[0]
    Gm, cmap = conjugate_pair(Gp, "Gm")
    fields = [h, G0, Gp, Gm, Z, A, Wp, Wm]

    verts = (model.vertices(fields, sector="potential",
                            conjugate_map=cmap, simplifier=sp.simplify)
             + model.vertices(fields, sector="kinetic",
                              conjugate_map=cmap, simplifier=sp.simplify))


    # Gauge self-couplings via the library, not by hand: gauge_vertices
    # derives the weak->physical U from the registered Rotations and uses a
    # canonical leg ordering. Building this from raw cubic_couplings and
    # deduplicating by leg multiset kept an ARBITRARY representative ordering,
    # and since the tensor is totally antisymmetric that choice set the sign.
    vvv = {v.particles: v.coupling
           for v in model.gauge_vertices(groups=[SU2L], basis=[Wp, Wm, Z, A],
                                         include=("VVV",))}

    particles = [
        UFOParticle(h, 25, "h", spin=1, mass="MH"),
        UFOParticle(G0, 250, "G0", spin=1, mass="MZ", goldstone=True),
        UFOParticle(Gp, 251, "G+", antiname="G-", spin=1, mass="MW",
                    charge=1, antisymbol=Gm, goldstone=True),
        UFOParticle(Z, 23, "Z", spin=3, mass="MZ"),
        UFOParticle(A, 22, "a", spin=3, texname=r"\gamma"),
        UFOParticle(Wp, 24, "W+", antiname="W-", spin=3, mass="MW",
                    charge=1, antisymbol=Wm),
    ]

    out = tmp_path_factory.mktemp("ufo") / "SMlite_UFO"
    params = ParameterSet(gw, g1, v, lam, mu2, MH, MW, MZ)
    write_ufo(out, "SMlite", params, particles, bosonic_vertices=verts,
              vvv=vvv)
    return out, model, dict(g=0.6535, gp=0.3580, v=246.0, lam=0.129)


_UFO_SUBMODULES = ("object_library", "function_library", "coupling_orders",
                   "parameters", "couplings", "lorentz", "particles",
                   "vertices")


def _import_ufo(path):
    """Load a UFO directory the MadGraph way (dir on sys.path, absolute
    imports); return object_library holding the all_* registries."""
    sys.path.insert(0, str(path))
    for mod in _UFO_SUBMODULES:
        sys.modules.pop(mod, None)
    try:
        import object_library
        for mod in _UFO_SUBMODULES[1:]:
            importlib.import_module(mod)
        return object_library
    finally:
        sys.path.pop(0)


def test_ufo_roundtrip_evaluates_cleanly(sm_ufo):
    """Every exported parameter and coupling evaluates to a finite number —
    the self-verification FeynRules-style generators cannot do."""
    path, model, num = sm_ufo
    report = verify_ufo_numeric(path)
    assert report.ok, report.failures
    assert report.couplings          # at least one coupling was evaluated
    assert report.parameters


def test_ufo_uses_absolute_imports(sm_ufo):
    """UFO submodules must use absolute imports (``from object_library …``) so
    MadGraph can run them as standalone scripts (param-card generation); a
    relative ``from .object_library`` breaks with 'no known parent package'."""
    path, model, num = sm_ufo
    for fname in ("parameters.py", "couplings.py", "particles.py",
                  "vertices.py", "lorentz.py", "function_library.py",
                  "__init__.py"):
        text = (path / fname).read_text()
        assert "from ." not in text and "from . import" not in text, \
            f"{fname} has a relative import"


def test_texnames_survive_export(sm_ufo):
    """LaTeX names reach the UFO verbatim (quotes and backslashes escaped)."""
    out, *_ = sm_ufo
    lib = _import_ufo(out)
    tex = {p.name: p.texname for p in lib.all_parameters}
    assert tex["g1"] == "{g'}" and tex["lam"] == r"\lambda_{\theta}"
    (photon,) = [p for p in lib.all_particles if p.name == "a"]
    assert photon.texname == r"\gamma"


def test_ufo_roundtrip_reproduces_pinned_hWW(sm_ufo):
    """Some evaluated coupling equals the pinned hWW value i g² v/2."""
    path, model, num = sm_ufo
    report = verify_ufo_numeric(path)
    g, v = num["g"], num["v"]
    target = complex(0, 1) * g**2 * v / 2
    assert any(abs(val - target) < 1e-9 for val in report.couplings.values()), \
        sorted(report.couplings.items())


def test_ufo_roundtrip_flags_nonfinite(sm_ufo):
    """A non-finite external input propagates to a reported failure rather
    than a silent NaN — the check has teeth."""
    path, model, num = sm_ufo
    report = verify_ufo_numeric(path, external_values={"v": float("inf")})
    assert not report.ok


def test_validate_umbrella_includes_ufo_roundtrip(sm_ufo):
    """Model.validate(ufo_path=...) runs invariance, skips anomalies (no
    fermions), and round-trips the exported UFO — all in one report."""
    path, model, num = sm_ufo
    report = model.validate(ufo_path=path)
    assert report.ok, report.summary()
    assert report.checks["invariance"].ok
    assert report.checks["anomalies"] is None          # scalar+gauge only
    assert report.checks["ufo_roundtrip"].ok


def test_ufo_imports(sm_ufo):
    path, model, num = sm_ufo
    ufo = _import_ufo(path)
    assert ufo.all_particles
    assert ufo.all_vertices
    assert ufo.all_couplings
    assert ufo.all_lorentz
    assert ufo.all_parameters


def test_ufo_parameters_resolve(sm_ufo):
    path, model, num = sm_ufo
    ufo = _import_ufo(path)
    import cmath  # noqa: F401  (UFO value strings use cmath)

    namespace = {"cmath": cmath, "complexconjugate":
                 lambda z: complex(z).conjugate(), "abs": abs}
    values = {}
    for p in ufo.all_parameters:
        if p.nature == "external":
            values[p.name] = float(p.value)
        else:
            values[p.name] = complex(eval(p.value, namespace, dict(values)))
    # pinned numbers
    g, gp, v, lam = num["g"], num["gp"], num["v"], num["lam"]
    assert abs(values["MW"] - g * v / 2) < 1e-10
    assert abs(values["MZ"] - ((g**2 + gp**2) ** 0.5) * v / 2) < 1e-10
    assert abs(values["MH"] - (2 * lam) ** 0.5 * v) < 1e-10
    assert abs(values["mu2"] - lam * v**2) < 1e-10


def test_ufo_couplings_pinned(sm_ufo):
    """The hWW coupling value must equal i g² v/2 = i g m_W numerically."""
    path, model, num = sm_ufo
    ufo = _import_ufo(path)
    import cmath

    namespace = {"cmath": cmath, "complexconjugate":
                 lambda z: complex(z).conjugate(), "abs": abs,
                 "complex": complex}
    # resolve parameters into the namespace
    for p in ufo.all_parameters:
        namespace[p.name] = (float(p.value) if p.nature == "external"
                             else complex(eval(p.value, namespace)))

    g, v = num["g"], num["v"]
    target = complex(0, 1) * g**2 * v / 2

    # find the vertex with particles {h, W+, W-}
    for vert in ufo.all_vertices:
        names = sorted(p.name for p in vert.particles)
        if names == ["W+", "W-", "h"]:
            coupling = list(vert.couplings.values())[0]
            value = complex(eval(coupling.value, namespace))
            assert abs(value - target) < 1e-9, (value, target)
            break
    else:
        pytest.fail("hWW vertex not found in UFO output")


def test_ufo_vertices_reference_valid_objects(sm_ufo):
    path, model, num = sm_ufo
    ufo = _import_ufo(path)
    particle_names = {p.name for p in ufo.all_particles}
    coupling_names = {c.name for c in ufo.all_couplings}
    lorentz_names = {l.name for l in ufo.all_lorentz}
    for vert in ufo.all_vertices:
        for p in vert.particles:
            assert p.name in particle_names
        for l in vert.lorentz:
            assert l.name in lorentz_names
        for c in vert.couplings.values():
            assert c.name in coupling_names
        # spins of the lorentz structures match the particles
        for l in vert.lorentz:
            assert l.spins == [p.spin for p in vert.particles], \
                (vert.name, l.spins, [p.spin for p in vert.particles])


def _eval_ufo_couplings(ufo, num):
    """``{sorted particle names: {lorentz name: complex value}}``."""
    import cmath
    ns = {"cmath": cmath, "complex": complex, "abs": abs,
          "complexconjugate": lambda z: complex(z).conjugate()}
    for p in ufo.all_parameters:
        ns[p.name] = (float(p.value) if p.nature == "external"
                      else complex(eval(p.value, ns)))
    out = {}
    for vert in ufo.all_vertices:
        lor = [l.name for l in vert.lorentz]
        key = tuple(sorted(p.name for p in vert.particles))
        out[key] = {lor[j]: complex(eval(c.value, ns))
                    for (i, j), c in vert.couplings.items()}
    return out


def test_exported_cubic_gauge_couplings(sm_ufo):
    """AWW/ZWW at MadGraph's own leg orderings and conventions:
    ``GC_4 = +i*ee`` for ``[a,W-,W+]``, ``GC_53 = +i*cw*ee/sw`` for
    ``[W-,W+,Z]``.

    Nothing in this file asserted any VVV value before, which is why its
    export sat with the opposite sign: the fixture deduplicated raw
    cubic_couplings by leg multiset, and the arbitrary representative
    ordering it kept determined the sign.
    """
    path, model, num = sm_ufo
    vals = _eval_ufo_couplings(_import_ufo(path), num)
    g, gp = num["g"], num["gp"]
    e = g * gp / (g ** 2 + gp ** 2) ** 0.5
    cw = g / (g ** 2 + gp ** 2) ** 0.5

    got = vals[tuple(sorted(["a", "W+", "W-"]))]["VVV1"]
    assert abs(got - complex(0, 1) * e) < 1e-9, got
    got = vals[tuple(sorted(["W+", "W-", "Z"]))]["VVV1"]
    assert abs(got - complex(0, 1) * g * cw) < 1e-9, got


def test_exported_vss_goldstone_coupling(sm_ufo):
    """``A G+ G-`` against MadGraph's ``V_11 [a, G-, G+] : GC_3 = -i*ee``.

    VSS1 = P(1,2) - P(1,3) is antisymmetric in legs 2,3, so the charged pair
    there flips it under the field->particle relabelling — the same rule that
    flips the electroweak cubics (feynlag.export.ufo.legs).  Nothing applied
    it on the VSS path until that module existed, so this assertion fails
    before the fix: this fixture exports Goldstones, so it is the one place
    the bug was actually reachable.
    """
    path, model, num = sm_ufo
    ufo = _import_ufo(path)
    vals = _eval_ufo_couplings(ufo, num)
    g, gp = num["g"], num["gp"]
    e = g * gp / (g ** 2 + gp ** 2) ** 0.5

    key = tuple(sorted(["a", "G+", "G-"]))
    assert key in vals, sorted(vals)
    # emitted leg order decides the sign; read it back off the vertex
    vert = next(v for v in ufo.all_vertices
                if tuple(sorted(p.name for p in v.particles)) == key)
    names = [p.name for p in vert.particles]
    got = vals[key]["VSS1"]
    # MG: (a, G-, G+) -> -i e ; swapping the two scalars flips VSS1
    expected = complex(0, -1) * e
    if names.index("G+") < names.index("G-"):
        expected = -expected
    assert abs(got - expected) < 1e-9, (names, got, expected)


def test_exported_charged_goldstone_couplings(sm_ufo):
    """Every charged-Goldstone VSS/VVS coupling against MadGraph's stock sm
    (issue #22).

    These were off by exactly ±i. Two factors, both mappings from feynlag's
    conventions onto UFO's and both applied in `export/ufo/legs.py`:

    * VSS1 carries one power of momentum and feynlag's `d_mu -> i p_mu`
      differs from UFO's by a sign there, so a VSS picks up -1
      unconditionally;
    * feynlag's charged Goldstone carries a phase `i**(-q)` relative to
      MadGraph's, so each charged-Goldstone leg contributes `i**q`.

    It was a pure convention, not an error: MadGraph agreed between unitary
    and Feynman gauge BEFORE the alignment, and a deliberately-broken phase
    failed that same check.

    The last two rows are the controls. A conjugate pair contributes
    `i * (1/i) = 1`, so those vertices matched all along and must NOT move --
    if aligning the phase shifts them, the alignment is wrong.
    """
    path, model, num = sm_ufo
    vals = _eval_ufo_couplings(_import_ufo(path), num)
    g, gp, v = num["g"], num["gp"], num["v"]
    e = g * gp / (g ** 2 + gp ** 2) ** 0.5
    sw = gp / (g ** 2 + gp ** 2) ** 0.5
    cw = g / (g ** 2 + gp ** 2) ** 0.5
    j = complex(0, 1)

    expected = {
        ("W+", "G-", "h"): -e / (2 * sw),            # GC_37
        ("W-", "G+", "h"): -e / (2 * sw),            # GC_37
        ("a", "W+", "G-"): e ** 2 * v / (2 * sw),    # GC_75
        ("a", "W-", "G+"): -e ** 2 * v / (2 * sw),   # GC_74
        ("W+", "G0", "G-"): -j * e / (2 * sw),       # GC_38
        ("W-", "G0", "G+"): j * e / (2 * sw),        # GC_39
        ("W+", "Z", "G-"): -e ** 2 * v / (2 * cw),   # GC_66
        ("W-", "Z", "G+"): e ** 2 * v / (2 * cw),    # GC_67
        # controls: legs close under conjugation, so the phase cancels
        ("a", "G-", "G+"): -j * e,                   # GC_3
        ("W-", "W+", "h"): j * g ** 2 * v / 2,       # GC_72
    }
    for legs, want in expected.items():
        key = tuple(sorted(legs))
        assert key in vals, f"{legs} not exported; have {sorted(vals)}"
        got = list(vals[key].values())[0]
        assert abs(got - want) < 1e-6 * max(1.0, abs(want)), (legs, got, want)
