"""Independent cubic-singles and six-term virtual-ladder regression on the H4 torus.

cpp/src/dlpno_ccsd.cpp builds T1full_i = U_ii t1_i; u_i(P,a) then contracts
Bvv_P[a,e] T1full_i[e] and projects only a into U_ij. w_j(P,m) contracts
Bov_P[m,f] T1full_j[f]. Writing d_i=V_ii t_i, d_j=V_jj t_j, s_m=V_ij.T S d_m:

 Q_ab  = +sum_ef (a e|b f) d_i[e] d_j[f]                  (u_i.T u_j)
 CX_ab = -sum_mef (a e|m f) d_i[e] d_j[f] s_m[b]          (-A.T t1p)
 CY_ab = -sum_mef s_m[a] (m e|b f) d_i[e] d_j[f]          (-t1p B)

Here e,f denote complete virtual directions, not target PNO indices.
The explicit 12-AO integrals and independent placed PAO/PNO frames below
provide the reference. The half-weighted term is accounted for separately
when isolating the native cumulative snapshots; it is not a production term.
"""

import json
from itertools import combinations_with_replacement, product

import numpy as np

try:
    from . import bipole_virtual_singles as fixture
except ImportError:
    import bipole_virtual_singles as fixture

bare = fixture.bare
TOL, FORBIDDEN = bare.ACTION_TOL, bare.FORBIDDEN_TOL
SINGLES = fixture.SINGLES
SCALES = (-1.25, 0.5)
NAMES = ('quadratic', 'cubic_x', 'cubic_y', 'sum')


def dense_terms(eri, v, occ, di, dj, sm):
    """No target projector on di,dj; sm[m,a] projects output singles only."""
    q = np.einsum('uvxy,ua,v,xb,y->ab', eri, v, di, v, dj, optimize=True)
    x = -np.einsum('uvxy,ua,v,xm,y,mb->ab', eri, v, di, occ, dj, sm, optimize=True)
    y = -np.einsum('uvxy,um,v,xb,y,ma->ab', eri, occ, di, v, dj, sm, optimize=True)
    return q, x, y, q+x+y


def independent_reference(inputs):
    """Compute controls and predictions before invoking the new production action."""
    s, eri, virtual, occ, bases, placed, amps = inputs
    rotations = {ij: v.T @ s @ placed[ij] for ij, v in bases.items()}
    singles = [rotations[m, m] @ SINGLES[m % 2] for m in range(6)]
    d = np.column_stack([bases[m, m] @ singles[m] for m in range(6)])
    t = {ij: u @ amps[ij] @ u.T for ij, u in rotations.items()}
    pair_t = {ij: v @ t[ij] @ v.T for ij, v in bases.items()}
    pairs = list(combinations_with_replacement(range(6), 2))
    zero, one = fixture.native_snapshots(eri, occ, virtual, s, pairs, bases, t, singles)
    refs, assembly_refs = {}, {}
    bad = {name: {} for name in NAMES}
    bad['cubic_sum'] = {}
    wrong_legs = {name: {} for name in ('x', 'y', 'sum')}
    native_error = {name: {} for name in ('isolated_cubic', 'sixterm', 'zero', 'reconstructed')}
    changed = {name: {} for name in ('linear_T2', 'half_weighted_change')}
    signal = {rank: {name: {} for name in NAMES} for rank in (2, 3)}
    outside = dict(norm=0.)
    projector = 0.
    for ij, v in bases.items():
        i, j = ij
        u = rotations[ij]
        proj = v.T @ s
        sm = d.T @ s @ v
        terms = dense_terms(eri, v, occ, d[:, i], d[:, j], sm)
        refs[ij] = tuple(u.T @ a @ u for a in terms)
        projected_i, projected_j = v @ proj @ d[:, i], v @ proj @ d[:, j]
        wrong = dense_terms(eri, v, occ, projected_i, projected_j, sm)
        if i != j:
            for m, projected in ((i, projected_i), (j, projected_j)):
                lost = d[:, m]-projected
                norm = float(np.sqrt(max(0., lost @ s @ lost)))
                if norm > outside['norm']:
                    outside = dict(norm=norm, ij=list(ij), occupied=m, rank=v.shape[1])
            for name, value, ref in zip(NAMES, wrong, refs[ij], strict=True):
                bare.witness(bad[name], ij, u.T @ value @ u, ref)
            bare.witness(bad['cubic_sum'], ij, u.T @ (wrong[1]+wrong[2]) @ u, refs[ij][1]+refs[ij][2])
            swapped_inputs = dense_terms(eri, v, occ, d[:, j], d[:, i], sm)
            for name, got, expected in zip(wrong_legs, (swapped_inputs[1],swapped_inputs[2],swapped_inputs[1]+swapped_inputs[2]),
                                           (terms[1],terms[2],terms[1]+terms[2]), strict=True):
                bare.witness(wrong_legs[name], ij, u.T @ got @ u, u.T @ expected @ u)
        for name, value in zip(NAMES, refs[ij], strict=True):
            bare.witness(signal[v.shape[1]][name], ij, value, np.zeros_like(value))
        projector = max(projector, float(abs((v@v.T-placed[ij]@placed[ij].T)@s).max()))
        # Explicitly account for the other singles-dependent native updates.
        kernel = np.einsum('uvxy,vy->ux', eri, pair_t[ij], optimize=True)
        singles_kernel = np.einsum('uvxy,v,y->ux', eri, d[:, i], d[:, j], optimize=True)
        linear_t2 = -v.T @ kernel @ occ @ sm - sm.T @ occ.T @ kernel @ v
        assembly_refs[ij] = u.T @ (v.T @ kernel @ v + linear_t2 + terms[3]) @ u
        if ij not in zero:
            continue
        z, z1 = occ.T @ kernel @ occ, occ.T @ singles_kernel @ occ
        half0, half_change = np.zeros_like(terms[0]), np.zeros_like(terms[0])
        for (m, n), t_mn in pair_t.items():
            tp = proj @ t_mn @ proj.T
            ss = np.outer(sm[m], sm[n])
            half0 += .5*z[m, n]*tp
            half_change += .5*(z[m, n]*ss + z1[m, n]*(tp+ss))
        def witness(name, got, ref):
            bare.witness(native_error[name], ij, u.T @ got @ u, u.T @ ref @ u)
        witness('isolated_cubic', one[ij]-zero[ij]-linear_t2-half_change-terms[0], terms[1]+terms[2])
        witness('sixterm', one[ij]-half0-half_change, v.T @ kernel @ v+linear_t2+terms[3])
        witness('zero', zero[ij], v.T @ kernel @ v + half0)
        witness('reconstructed', one[ij]-zero[ij], terms[3]+linear_t2+half_change)
        for name, value in [('linear_T2', linear_t2), ('half_weighted_change', half_change)]:
            bare.witness(changed[name], ij, u.T @ value @ u, np.zeros_like(value))
    assert len(refs)==36 and sum(v[0].size for v in refs.values())==174
    assert outside['norm'] > 1e-6 and projector <= TOL
    assert all(v['abs'] > TOL for v in bad.values())
    assert all(v['abs'] > TOL for v in wrong_legs.values())
    assert all(v['abs'] > TOL for row in signal.values() for v in row.values())
    assert all(v['abs'] <= TOL for v in native_error.values())
    assert all(v['abs'] > TOL for v in changed.values())
    record = dict(premature_projection=bad, wrong_contracted_legs=wrong_legs, native=native_error, other_changed_terms=changed,
                  signals_by_rank=signal, outside_target=outside, projector_error=projector)
    return refs, assembly_refs, record


def compare(prepared, refs, assembly_refs, parts, assembled):
    spaces = prepared.pair_build.spaces
    worst = {name: {} for name in ('x', 'y', 'sum', 'sixterm')}
    ranks = {r: {name: {} for name in worst} for r in (2, 3)}
    counts = {2: 0, 3: 0}
    wrong, reversal = {}, {}
    for ij, reference in refs.items():
        p = spaces.resolve(*ij)
        x, y, summed = parts.placed(spaces, *ij)
        rank = x.shape[0]
        counts[rank] += rank**2
        expected = (reference[1], reference[2], reference[1]+reference[2], assembly_refs[ij])
        got = (x, y, summed, spaces.orient_tensor(*ij, assembled))
        for name, actual, target in zip(worst, got, expected, strict=True):
            assert actual.shape==target.shape and np.isfinite(actual).all()
            bare.witness(worst[name], (p.row, *ij), actual, target)
            bare.witness(ranks[rank][name], (p.row, *ij), actual, target)
        other = parts.placed(spaces, *ij[::-1])
        bare.witness(reversal, (p.row, *ij), x, other[1].T)
        if p.transposed:
            bare.witness(wrong, (p.row, *ij), parts.x[p.row].T, reference[1])
    assert sum(counts.values())==174 and min(counts.values())>0
    assert all(v['abs']<=TOL for v in worst.values()), worst
    assert reversal['abs']<=TOL and wrong['abs']>TOL
    return dict(worst=worst, worst_by_rank=ranks, components_per_term=counts, ordered_pairs=36,
                reversal_error=reversal, reversed_without_leg_exchange=wrong)


def momentum_checks(result, eri):
    """Both integral classes: all forbidden band components and one strong witness/q."""
    from vibeqc.dlpno.bipole_virtual_singles import mixed_transition
    h = result.terminal_transitions
    phase = np.exp(1j*np.outer(np.arange(3)*bare.FIXTURE['lengths'][0],
                              result.kpoints_cart[:, 0]))/np.sqrt(3)
    mo = np.einsum('tk,kub->ktub', phase, result.mo_coeffs).reshape(3, 12, 4)
    records = {}
    for kind in ('vvvv', 'ovvv'):
        first = (2, 3) if kind=='vvvv' else (0, 1)
        records[kind] = dict(allowed={}, forbidden={}, forbidden_components=0, witnesses={})
        rec = records[kind]
        choices = {}
        for k in product(range(3), repeat=4):
            p, q, r, t = k
            ref = np.einsum('uvxy,ua,vb,xc,yd->abcd', eri, mo[p][:, first].conj(),
                             mo[q][:, 2:], mo[r][:, 2:].conj(), mo[t][:, 2:], optimize=True)
            def read(bands):
                if kind=='vvvv':
                    return h.virtual_transition(*k, bands=bands, max_bytes=h.required_bytes)
                return mixed_transition(result, k, bands=bands, max_bytes=h.required_bytes)
            if (p-q+r-t) % 3:
                for idx in product(range(2), repeat=4):
                    bands = (first[idx[0]], 2+idx[1], 2+idx[2], 2+idx[3])
                    got = read(bands)
                    expected = ref[idx]
                    assert got==0 and abs(expected)<=FORBIDDEN
                    bare.witness(rec['forbidden'], (*k, *bands), np.asarray(got), np.asarray(expected))
                    rec['forbidden_components'] += 1
            else:
                idx = np.unravel_index(abs(ref).argmax(), ref.shape)
                transfer = (p-q+1)%3-1
                expected = ref[idx]
                if transfer not in choices or abs(expected)>abs(choices[transfer][2]):
                    bands = (first[idx[0]], 2+int(idx[1]), 2+int(idx[2]), 2+int(idx[3]))
                    choices[transfer] = k, bands, expected
        for transfer, (k, bands, expected) in choices.items():
            if kind=='vvvv':
                got = h.virtual_transition(*k, bands=bands, max_bytes=h.required_bytes)
            else:
                got = mixed_transition(result, k, bands=bands, max_bytes=h.required_bytes)
            assert abs(expected)>TOL
            rec['witnesses'][transfer] = dict(momenta=list(k), bands=list(bands),
                                              reference=[float(expected.real), float(expected.imag)])
            bare.witness(rec['allowed'], (*k, *bands), np.asarray(got), np.asarray(expected))
        assert set(choices)=={-1, 0, 1} and rec['forbidden_components']==54*16
        assert rec['allowed']['abs']<=TOL
    return records


def controls(result, prepared, action, limits):
    from copy import copy
    from dataclasses import replace
    system, basis, mesh, options, kw = bare.op.rhf_fixture(3, scf=bare.SCF, **bare.FIXTURE)
    other = bare.run_pbc_bipole_rhf(system, basis, mesh, options,
                                    terminal_transition_bytes=bare.LIMIT, **kw)
    space = copy(prepared.pair_build.spaces)
    columns = list(space.pair_coefficients)
    columns[0] = columns[0].copy()
    columns[0][:, 0] *= -1
    space.pair_coefficients = tuple(columns)
    bad_frame = replace(prepared, pair_build=replace(prepared.pair_build, spaces=space))
    refused = {}
    for name, owner, pair_input, budget, exc, message in [
        ('foreign_handle', replace(result, terminal_transitions=other.terminal_transitions),
         prepared, limits, ValueError, 'different terminal result'),
        ('foreign_preparation', other, prepared, limits, ValueError, 'same terminal generation'),
        ('changed_frame', result, bad_frame, limits, ValueError, 'frame differs'),
        ('short_bytes', result, prepared, dict(limits, max_bytes=limits['max_bytes']-1), MemoryError, 'bytes'),
        ('short_work', result, prepared, dict(limits, max_work=limits['max_work']-1), ValueError, 'work units'),
    ]:
        try:
            action(owner, pair_input, SINGLES, **budget)
        except exc as err:
            assert message in str(err), (name, str(err))
            refused[name] = str(err)
        else:
            raise AssertionError(f'{name} admitted')
    energy = result.energy
    try:
        result.energy = energy + .01
        try:
            action(result, prepared, SINGLES, **limits)
        except ValueError as err:
            assert 'state changed' in str(err)
            refused['stale_terminal_state'] = str(err)
        else:
            raise AssertionError('stale terminal state admitted')
    finally:
        result.energy = energy
    result.terminal_transitions._validate(result, limits['max_bytes'])
    return refused



def run(*, oracle_only=False, momentum_evidence=None):
    result, prepared, doubles = fixture.prepare()
    inputs = fixture.reference_inputs(result, prepared, doubles)
    refs, assembly_refs, oracle = independent_reference(inputs)
    print(json.dumps(dict(preproduction_oracle=oracle, component_gate=TOL)), flush=True)
    if oracle_only:
        return oracle
    from vibeqc.dlpno.bipole_virtual_cubic import WORK_BOUND, cubic_virtual_ladder, resource_census
    from vibeqc.dlpno.bipole_virtual_disconnected import (
        WORK_BOUND as QUADRATIC_WORK, disconnected_virtual_ladder)
    from vibeqc.dlpno.bipole_virtual_singles import (
        WORK_BOUND as LINEAR_WORK, linear_virtual_ladder_terms)
    h, spaces = result.terminal_transitions, prepared.pair_build.spaces
    limits = dict(max_bytes=h.required_bytes, max_work=WORK_BOUND)
    refused = controls(result, prepared, cubic_virtual_ladder, limits)
    zero = cubic_virtual_ladder(result, prepared, [0*t for t in SINGLES], **limits)
    assert all(np.count_nonzero(a)==0 for leg in zero for a in leg)
    parts = cubic_virtual_ladder(result, prepared, SINGLES, **limits)
    # Preserve the accepted four contributions: sequential bounded actions.
    quadratic = disconnected_virtual_ladder(result, prepared, SINGLES,
                                             max_bytes=h.required_bytes, max_work=QUADRATIC_WORK)
    baseline = h.bare_virtual_ladder(result, prepared, doubles,
                                     max_bytes=h.required_bytes, max_work=h.ladder_work_bound)
    mixed = linear_virtual_ladder_terms(result, prepared, doubles, SINGLES,
                                        max_bytes=h.required_bytes, max_work=LINEAR_WORK)
    assembled = tuple(a+b+c+d for a, b, c, d in zip(baseline, mixed.total, quadratic, parts.total, strict=True))
    comparison = compare(prepared, refs, assembly_refs, parts, assembled)
    scaling = {}
    for scale in SCALES:
        scaled = cubic_virtual_ladder(result, prepared, [scale*t for t in SINGLES], **limits)
        errors = {name: {} for name in ('x', 'y', 'sum')}
        for ij in refs:
            for name, actual, original in zip(errors, scaled.placed(spaces, *ij),
                                               parts.placed(spaces, *ij), strict=True):
                bare.witness(errors[name], (spaces.resolve(*ij).row, *ij), actual, scale**3*original)
        assert all(error['abs']<=TOL for error in errors.values()), errors
        scaling[scale] = errors
    assert all(not a.flags.writeable for leg in parts for a in leg)
    if momentum_evidence is None:
        momentum = momentum_checks(result, inputs[1])
        momentum_reuse = None
    else:
        # Reuse is allowed only for the exact terminal source, source record,
        # pair build and unchanged absolute gates; adapter/native hashes are
        # part of that source record. The caller records the original job ID.
        e = momentum_evidence
        assert e['source']==h.identity and e['source_record']==h.provenance
        assert e['pair_build']==prepared.pair_build.identity
        assert e['gates']==dict(component=TOL, forbidden=FORBIDDEN)
        momentum = e['momentum']
        for record in momentum.values():
            assert record['forbidden_components']==864 and record['forbidden']['abs']<=FORBIDDEN
            assert set(map(int, record['witnesses']))=={-1, 0, 1} and record['allowed']['abs']<=TOL
        momentum_reuse = e['job']
    return dict(**comparison, oracle=oracle, scaling=scaling, controls=refused,
                momentum=momentum, momentum_reuse=momentum_reuse, zero_singles_max=0.,
                source=h.identity, source_record=h.provenance, pair_build=prepared.pair_build.identity,
                raw_mp2=prepared.solution.e_pair_per_cell, singles=[t.tolist() for t in SINGLES],
                doubles_for_native=[t.tolist() for t in doubles], ranks=list(spaces.pair_ranks),
                representatives=spaces.representative_pairs, limits=limits, resources=resource_census(),
                gates=dict(component=TOL, forbidden=FORBIDDEN),
                components={k: [a.tolist() for a in v] for k, v in parts._asdict().items()},
                accepted_outputs={k:[a.tolist() for a in v] for k,v in
                                  [('disconnected',quadratic),('bare',baseline),('x',mixed.x),('y',mixed.y)]},
                sixterm=[a.tolist() for a in assembled],
                assembly_work=WORK_BOUND+QUADRATIC_WORK+h.ladder_work_bound+LINEAR_WORK)


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--oracle-only', action='store_true')
    print(json.dumps(run(oracle_only=parser.parse_args().oracle_only)), flush=True)
