"""AZ-283 — DescriptorNormaliser helper AC tests.

Verifies the contract at ``_docs/02_document/contracts/shared_helpers/descriptor_normaliser.md`` v1.0.0.
"""

from __future__ import annotations

import ast
from pathlib import Path

import numpy as np
import pytest

from gps_denied_onboard.helpers import DescriptorNormaliser, DescriptorNormaliserError


def test_ac1_unit_vector_example() -> None:
    out = DescriptorNormaliser.l2_normalise(np.array([3.0, 4.0], dtype=np.float32))
    np.testing.assert_allclose(out, np.array([0.6, 0.8], dtype=np.float32), atol=1e-6)
    assert float(np.linalg.norm(out)) == pytest.approx(1.0, abs=1e-6)


def test_ac2_batch_normalisation() -> None:
    batch = np.array([[3.0, 4.0], [1.0, 0.0]], dtype=np.float32)
    out = DescriptorNormaliser.l2_normalise_batch(batch)
    np.testing.assert_allclose(out[0], np.array([0.6, 0.8], dtype=np.float32), atol=1e-6)
    np.testing.assert_allclose(out[1], np.array([1.0, 0.0], dtype=np.float32), atol=1e-6)
    for row in out:
        assert float(np.linalg.norm(row)) == pytest.approx(1.0, abs=1e-6)


def test_ac3_fp16_dtype_preservation() -> None:
    rng = np.random.default_rng(2026)
    x = rng.standard_normal(512).astype(np.float16)
    out = DescriptorNormaliser.l2_normalise(x)
    assert out.dtype == np.float16
    assert float(np.linalg.norm(out.astype(np.float32))) == pytest.approx(1.0, abs=1e-3)


def test_ac4_fp32_dtype_preservation() -> None:
    rng = np.random.default_rng(2026)
    x = rng.standard_normal(512).astype(np.float32)
    out = DescriptorNormaliser.l2_normalise(x)
    assert out.dtype == np.float32
    assert float(np.linalg.norm(out)) == pytest.approx(1.0, abs=1e-6)


def test_ac5_zero_vector_handling() -> None:
    zeros = np.zeros(128, dtype=np.float32)
    out = DescriptorNormaliser.l2_normalise(zeros)
    np.testing.assert_array_equal(out, zeros)
    assert not np.any(np.isnan(out))


def test_ac5b_zero_row_in_batch_remains_zero() -> None:
    batch = np.array([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]], dtype=np.float32)
    out = DescriptorNormaliser.l2_normalise_batch(batch)
    np.testing.assert_array_equal(out[0], np.zeros(3, dtype=np.float32))
    np.testing.assert_allclose(out[1], np.array([1.0, 0.0, 0.0], dtype=np.float32))


def test_ac6_idempotence_fp32() -> None:
    rng = np.random.default_rng(2026)
    x = rng.standard_normal(64).astype(np.float32)
    once = DescriptorNormaliser.l2_normalise(x)
    twice = DescriptorNormaliser.l2_normalise(once)
    assert once.tobytes() == twice.tobytes()


def test_ac7_idempotence_fp16_within_half_precision_tol() -> None:
    rng = np.random.default_rng(2026)
    x = rng.standard_normal(64).astype(np.float16)
    once = DescriptorNormaliser.l2_normalise(x)
    twice = DescriptorNormaliser.l2_normalise(once)
    np.testing.assert_allclose(twice.astype(np.float32), once.astype(np.float32), atol=1e-3)


def test_ac8_no_in_place_mutation() -> None:
    x = np.array([3.0, 4.0, 0.0], dtype=np.float32)
    snapshot = x.copy()
    _ = DescriptorNormaliser.l2_normalise(x)
    np.testing.assert_array_equal(x, snapshot)


def test_ac9_metric_is_inner_product_exact_string() -> None:
    assert DescriptorNormaliser.descriptor_metric() == "inner_product"


def test_ac10_float64_dtype_rejected() -> None:
    with pytest.raises(DescriptorNormaliserError, match=r"float16.*float32|float32.*float16"):
        DescriptorNormaliser.l2_normalise(np.array([1.0, 2.0], dtype=np.float64))


def test_ac11_shape_contract_single_rejects_2d() -> None:
    with pytest.raises(DescriptorNormaliserError, match=r"1-D"):
        DescriptorNormaliser.l2_normalise(np.zeros((2, 3), dtype=np.float32))


def test_ac11_shape_contract_batch_rejects_1d() -> None:
    with pytest.raises(DescriptorNormaliserError, match=r"2-D"):
        DescriptorNormaliser.l2_normalise_batch(np.zeros(128, dtype=np.float32))


def test_ac12_no_upward_imports_to_components() -> None:
    module_path = (
        Path(__file__).resolve().parents[2]
        / "src"
        / "gps_denied_onboard"
        / "helpers"
        / "descriptor_normaliser.py"
    )
    tree = ast.parse(module_path.read_text(encoding="utf-8"))
    bad: list[str] = []
    for node in ast.walk(tree):
        if isinstance(node, ast.ImportFrom) and node.module:
            if node.module.startswith("gps_denied_onboard.components"):
                bad.append(node.module)
        elif isinstance(node, ast.Import):
            for alias in node.names:
                if alias.name.startswith("gps_denied_onboard.components"):
                    bad.append(alias.name)
    assert not bad, f"descriptor_normaliser must not import components.*; found: {bad}"
