"""Load native-grid sensor data using an explicit, frozen sensor policy."""
from __future__ import annotations

from dataclasses import dataclass, replace
import hashlib
import json
import os
from pathlib import Path
from functools import lru_cache

import cv2
import numpy as np

from .manifest import FrameRecord, _pcd_grid
from .pcd import read_pcd


@dataclass(frozen=True)
class NormalizationStats:
    depth_scale: float
    intensity_scale: float


def _read_opencv_yaml(path):
    storage = cv2.FileStorage(str(path), cv2.FILE_STORAGE_READ)
    try:
        result = storage.getFirstTopLevelNode().mat() if storage.isOpened() else None
        if result is None or not result.size:
            raise ValueError(f"empty or unreadable OpenCV matrix: {path}")
        return result
    except cv2.error as exc:
        raise ValueError(f"invalid OpenCV matrix: {path}") from exc
    finally:
        storage.release()


def _image(path):
    path = Path(path)
    result = _read_opencv_yaml(path) if path.suffix.lower() in {".yaml", ".yml"} else cv2.imread(str(path), cv2.IMREAD_UNCHANGED)
    if result is None:
        raise ValueError(f"cannot read image: {path}")
    return result


def policy_digest(policy):
    return hashlib.sha256(json.dumps(policy or {}, sort_keys=True).encode()).hexdigest()


def decoder_digest():
    digest=hashlib.sha256()
    for name in ('loader.py','pcd.py'):
        digest.update((Path(__file__).parent/name).read_bytes())
    return digest.hexdigest()


@lru_cache(maxsize=20000)
def _check_cache(path,size,mtime_ns,expected):
    digest=hashlib.sha256()
    with Path(path).open('rb') as stream:
        for chunk in iter(lambda:stream.read(4*1024*1024),b''):digest.update(chunk)
    if digest.hexdigest()!=expected:raise ValueError('cached tensor content fingerprint mismatch')
    return True


def load_frame(record: FrameRecord, *, shape=None, return_metadata=False):
    """Return [D(mm), I(raw), validD, validI]; unknown geometry stays missing.

    PCD channels share one native grid. Raster substitution requires an explicit
    policy confirming its source and alignment; no x-axis/unit heuristics run.
    """
    from .eligibility import require_eligible
    require_eligible(record)
    policy = record.sensor_config or {}
    metadata = {"policy_id": policy.get("policy_id"), "policy_sha256": policy_digest(policy),
                "depth_source": None, "depth_units": "mm", "intensity_source": None,
                "depth_unavailable_reason": policy.get("unavailable_reason", "No verified sensor policy")}
    if record.cache_path and Path(record.cache_path).is_file():
        if record.cache_sha256:
            stat=Path(record.cache_path).stat()
            _check_cache(record.cache_path,stat.st_size,stat.st_mtime_ns,record.cache_sha256)
        with np.load(record.cache_path, allow_pickle=False) as cache:
            saved = json.loads(str(cache["metadata"]))
            if saved.get("policy_sha256") != metadata["policy_sha256"] or saved.get("source_sha256") != record.source_sha256:
                raise ValueError("sensor cache does not match frozen source/policy")
            array = cache["frame"]
        if array.dtype!=np.float32 or array.shape!=tuple(policy.get('grid',array.shape[:2]))+(4,) or not np.isfinite(array).all():
            raise ValueError('cached tensor shape/dtype/values invalid')
        return (array, saved) if return_metadata else array
    configured_grid = tuple(policy["grid"]) if policy.get("grid") else None
    if shape is not None and configured_grid is not None and tuple(shape) != configured_grid:
        raise ValueError("explicit grid conflicts with sensor policy")
    shape = tuple(shape) if shape is not None else configured_grid
    points = None
    if record.pcd:
        grid = _pcd_grid(record.pcd)
        if shape is None:
            shape = grid
        elif grid is not None and shape != grid:
            raise ValueError("PCD grid differs from sensor policy")
        if shape is None:
            raise ValueError("no loadable registered image grid for unorganized PCD")
        _, points = read_pcd(record.pcd)
        if len(points) != shape[0] * shape[1]:
            raise ValueError("PCD length does not match registered sensor grid")
    elif shape is None:
        for key in ("intensity", "depth"):
            if key in record.paths:
                shape = _image(record.paths[key]).shape[:2]
                break
    if shape is None:
        raise ValueError("no loadable sensor grid")
    depth = np.zeros(shape, np.float32)
    intensity = np.zeros(shape, np.float32)
    valid_d = np.zeros(shape, bool)
    valid_i = np.zeros(shape, bool)
    if points is not None:
        intensity = points[:, 3].reshape(shape).copy()
        valid_i = np.isfinite(intensity) & (intensity >= 0)
        if not np.any(intensity[valid_i] > 0):
            valid_i[:] = False
        metadata["intensity_source"] = "PCD native intensity"
    elif "intensity" in record.paths:
        intensity = _image(record.paths["intensity"])
        if intensity.shape != shape:
            raise ValueError("intensity shape differs from native grid; implicit reshape forbidden")
        valid_i = np.isfinite(intensity) & (intensity >= 0)
        if not np.any(intensity[valid_i] > 0):
            valid_i[:] = False
        metadata["intensity_source"] = str(record.paths["intensity"])
    if policy.get("depth_available"):
        scale = policy.get("scale_to_mm")
        if not isinstance(scale, (int, float)) or not np.isfinite(scale) or scale <= 0:
            raise ValueError("verified depth policy lacks a positive metric scale")
        source = policy.get("depth_source")
        if source == "pcd":
            axis = policy.get("depth_axis")
            if points is None or axis not in (0, 1, 2) or policy.get("depth_kind", "axial") != "axial":
                raise ValueError("invalid registered PCD axial-depth policy")
            depth = points[:, axis].reshape(shape) * scale
            valid_d = np.isfinite(points[:, :3]).all(axis=1).reshape(shape) & (depth > 0)
        elif source == "depth" and policy.get("rasters_aligned"):
            depth = _image(record.paths["depth"])
            if depth.shape != shape:
                raise ValueError("registered depth raster has a different grid")
            depth = depth * scale
            valid_d = np.isfinite(depth) & (depth > 0)
        else:
            raise ValueError("depth source/alignment not registered")
        metadata.update(depth_source=source, depth_unavailable_reason=None)
    depth = np.where(valid_d, depth, 0)
    intensity = np.where(valid_i, intensity, 0)
    frame = np.stack((depth, intensity, valid_d, valid_i), axis=-1).astype(np.float32)
    metadata["source_sha256"] = record.source_sha256
    return (frame, metadata) if return_metadata else frame


def cache_frame(record, destination):
    array, metadata = load_frame(replace(record, cache_path=None), return_metadata=True)
    destination = Path(destination)
    destination.parent.mkdir(parents=True, exist_ok=True)
    temporary = destination.with_name(destination.name + ".tmp")
    with temporary.open("wb") as stream:
        np.savez(stream, frame=array, metadata=json.dumps(metadata))
    os.replace(temporary, destination)
    return replace(record, cache_path=str(destination),cache_sha256=None)


def fit_normalization(records):
    """Fit only supplied source-training frames, with deterministic bounded samples."""
    rng = np.random.default_rng(0)
    reservoirs = [np.empty(0, np.float32), np.empty(0, np.float32)]
    for record in records:
        array = load_frame(record)
        for channel in (0, 1):
            values = array[..., channel][array[..., channel + 2] > 0]
            if len(values) > 512:
                values = values[rng.choice(len(values), 512, replace=False)]
            combined = np.concatenate((reservoirs[channel], values))
            if len(combined) > 250000:
                combined = combined[rng.choice(len(combined), 250000, replace=False)]
            reservoirs[channel] = combined
    def scale(values):
        return max(float(np.percentile(values, 99)), 1e-6) if len(values) else 1.0
    return NormalizationStats(scale(reservoirs[0]), scale(np.log1p(reservoirs[1])))


def apply_normalization(frame, stats):
    output = np.asarray(frame, dtype=np.float32).copy()
    output[..., 0] = np.clip(output[..., 0] / stats.depth_scale, 0, 1) * output[..., 2]
    output[..., 1] = np.clip(np.log1p(np.maximum(output[..., 1], 0)) / stats.intensity_scale, 0, 1) * output[..., 3]
    return output
