"""Final coverage push: targeting the last coverable missed lines."""

from __future__ import annotations

import numpy as np
import pytest

from scpn_quantum_control.hardware.classical import _expectation_pauli


def test_expectation_pauli_z():
    """Cover classical.py line 185: Z Pauli case."""
    # |0> state: <Z> = +1
    psi = np.array([1, 0, 0, 0], dtype=complex)  # 2-qubit |00>
    z0 = _expectation_pauli(psi, 2, 0, "Z")
    z1 = _expectation_pauli(psi, 2, 1, "Z")
    assert abs(z0 - 1.0) < 1e-10
    assert abs(z1 - 1.0) < 1e-10

    # |11> state: <Z> = -1 for both qubits
    psi_11 = np.array([0, 0, 0, 1], dtype=complex)
    z0_11 = _expectation_pauli(psi_11, 2, 0, "Z")
    z1_11 = _expectation_pauli(psi_11, 2, 1, "Z")
    assert abs(z0_11 - (-1.0)) < 1e-10
    assert abs(z1_11 - (-1.0)) < 1e-10


def test_inhibitor_all_equal_target():
    """Cover spn_to_qcircuit.py line 82: all inhibitors == target → bare Ry."""
    from qiskit import QuantumCircuit

    from scpn_quantum_control.bridge.spn_to_qcircuit import inhibitor_anti_control

    qc = QuantumCircuit(3)
    # 2 inhibitors both equal to target → controls list empty → bare ry
    inhibitor_anti_control(qc, [1, 1], target=1, theta=0.5)
    from qiskit.circuit.library import RYGate

    assert any(isinstance(inst.operation, RYGate) for inst in qc.data)


def test_inhibitor_single_self():
    """Cover spn_to_qcircuit.py line 84: single inhibitor == target → bare Ry."""
    from qiskit import QuantumCircuit

    from scpn_quantum_control.bridge.spn_to_qcircuit import inhibitor_anti_control

    qc = QuantumCircuit(3)
    inhibitor_anti_control(qc, [2], target=2, theta=0.4)
    from qiskit.circuit.library import RYGate

    assert any(isinstance(inst.operation, RYGate) for inst in qc.data)


def test_qec_odd_syndrome_defects():
    """Cover control_qec.py line 79: odd defects → duplicate first defect."""
    from scpn_quantum_control.qec.control_qec import ControlQEC

    qec = ControlQEC(distance=3)
    n_data = 2 * 3**2
    # Construct an error pattern that produces an odd number of defects
    rng = np.random.default_rng(0)
    for _ in range(100):
        err_x = (rng.random(n_data) < 0.15).astype(np.int8)
        err_z = np.zeros(n_data, dtype=np.int8)
        syn_z, _ = qec.get_syndrome(err_x, err_z)
        n_defects = int(syn_z.sum())
        if n_defects % 2 == 1:
            corr = qec.decoder.decode(syn_z)
            assert corr.shape == (n_data,)
            return
    pytest.skip("no odd syndrome found in 100 random samples")


def test_qec_decode_and_correct_failure():
    """Cover control_qec.py line 217: decoder returns False on heavy errors."""
    from scpn_quantum_control.qec.control_qec import ControlQEC

    qec = ControlQEC(distance=3)
    rng = np.random.default_rng(7)
    failures = 0
    for _ in range(200):
        err_x, err_z = qec.simulate_errors(p_error=0.4, rng=rng)
        if not qec.decode_and_correct(err_x, err_z):
            failures += 1
    assert failures > 0, "Expected some correction failures at p=0.4"
