"""
Offline preprocessing for HAC that preserves training-time randomness.

What is baked offline:
  - video/flow decode from mp4
  - base Resize(scale=(-1, 256)) equivalent: resize short side to 256
  - raw audio samples + samplerate

What is intentionally NOT baked offline:
  - SampleFrames temporal sampling
  - RandomResizedCrop / CenterCrop / Flip
  - Normalize / FormatShape
  - audio time-window extraction, noise, and time masking

Those are re-applied in dataloader_fast_rewrite.py at training/validation time.
"""

from __future__ import annotations

import argparse
import csv
import os
from dataclasses import dataclass
from pathlib import Path
from typing import Iterable, Iterator, Optional

import imageio.v3 as iio
import numpy as np
import soundfile as sf
import torch
import torch.nn.functional as F
from tqdm import tqdm


@dataclass(frozen=True)
class HACItem:
    video_id: str
    label: int
    domain: str
    mode_prefix: str  # "train" or "val"


def _read_csv_items(datapath: Path, split: str, source: bool, domains: Iterable[str], mode_prefix: str) -> Iterator[HACItem]:
    """Mirror HACDOMAIN's CSV logic exactly, including test+train target when source=False."""
    split_dir = datapath / "HAC_Splits"
    for dom in domains:
        split_file = split_dir / f"HAC_{split}_only_{dom}.csv"
        with split_file.open(newline="") as f:
            for row in csv.reader(f):
                if not row:
                    continue
                yield HACItem(video_id=row[0], label=int(row[1]), domain=dom, mode_prefix=mode_prefix)

        if split == "test" and not source:
            train_file = split_dir / f"HAC_train_only_{dom}.csv"
            with train_file.open(newline="") as f:
                for row in csv.reader(f):
                    if not row:
                        continue
                    yield HACItem(video_id=row[0], label=int(row[1]), domain=dom, mode_prefix=mode_prefix)


def _resize_short_side_tensor(frames_nchw: torch.Tensor, short_side: int, chunk_size: int) -> torch.Tensor:
    """Resize a uint8/float tensor in NCHW layout so min(H, W) == short_side."""
    if frames_nchw.ndim != 4:
        raise ValueError(f"Expected NCHW frames, got shape={tuple(frames_nchw.shape)}")

    _, _, h, w = frames_nchw.shape
    if h <= 0 or w <= 0:
        raise ValueError(f"Invalid frame size H={h}, W={w}")

    if h < w:
        new_h = short_side
        new_w = int(round(w * short_side / h))
    else:
        new_w = short_side
        new_h = int(round(h * short_side / w))

    out_chunks = []
    for start in range(0, frames_nchw.shape[0], chunk_size):
        chunk = frames_nchw[start:start + chunk_size].float()
        try:
            resized = F.interpolate(
                chunk,
                size=(new_h, new_w),
                mode="bilinear",
                align_corners=False,
                antialias=True,
            )
        except TypeError:
            # Older PyTorch versions do not expose antialias in F.interpolate.
            resized = F.interpolate(chunk, size=(new_h, new_w), mode="bilinear", align_corners=False)
        out_chunks.append(resized.clamp_(0, 255).round().to(torch.uint8).cpu())

    return torch.cat(out_chunks, dim=0)


def _ensure_rgb(video: np.ndarray) -> np.ndarray:
    """Return video as THWC uint8 RGB."""
    if video.ndim == 3:
        # Grayscale THW -> fake RGB. This should not normally happen for RGB mp4s.
        video = np.repeat(video[..., None], 3, axis=-1)
    if video.ndim != 4:
        raise ValueError(f"Expected video THWC/THW, got shape={video.shape}")
    if video.shape[-1] == 4:
        video = video[..., :3]
    if video.shape[-1] == 1:
        video = np.repeat(video, 3, axis=-1)
    if video.shape[-1] != 3:
        raise ValueError(f"Expected 3 RGB channels, got shape={video.shape}")
    return np.ascontiguousarray(video.astype(np.uint8, copy=False))


def _ensure_gray(video: np.ndarray) -> np.ndarray:
    """Return optical-flow video as THW uint8 by taking one decoded channel."""
    if video.ndim == 4:
        video = video[..., 0]
    if video.ndim != 3:
        raise ValueError(f"Expected flow THW/THWC, got shape={video.shape}")
    return np.ascontiguousarray(video.astype(np.uint8, copy=False))


def resize_rgb_short_side(video_thwc: np.ndarray, short_side: int, chunk_size: int) -> np.ndarray:
    video_thwc = _ensure_rgb(video_thwc)
    tensor = torch.from_numpy(video_thwc).permute(0, 3, 1, 2).contiguous()  # T,C,H,W
    resized = _resize_short_side_tensor(tensor, short_side=short_side, chunk_size=chunk_size)
    return resized.permute(0, 2, 3, 1).numpy()  # T,H,W,C


def resize_gray_short_side(video_thw: np.ndarray, short_side: int, chunk_size: int) -> np.ndarray:
    video_thw = _ensure_gray(video_thw)
    tensor = torch.from_numpy(video_thw[:, None, :, :]).contiguous()  # T,1,H,W
    resized = _resize_short_side_tensor(tensor, short_side=short_side, chunk_size=chunk_size)
    return resized[:, 0].numpy()  # T,H,W


def _load_audio(audio_path: Path) -> tuple[np.ndarray, int]:
    samples, samplerate = sf.read(str(audio_path), dtype="float32")
    # The original code assumes mono audio. This keeps mono as-is and makes stereo safe.
    if samples.ndim > 1:
        samples = samples.mean(axis=1)
    return np.ascontiguousarray(samples.astype(np.float32, copy=False)), int(samplerate)


def _save_npz(save_path: Path, arrays: dict, compressed: bool) -> None:
    save_path.parent.mkdir(parents=True, exist_ok=True)
    if compressed:
        np.savez_compressed(save_path, **arrays)
    else:
        # Faster to write and much faster to load during training; costs more disk.
        np.savez(save_path, **arrays)


def _process_video_stream(video_path: Path, short_side: int, chunk_size: int, is_flow: bool) -> np.ndarray:
    """Streams a video from disk, resizing it in chunks to prevent Out-Of-Memory (OOM) crashes."""
    resized_chunks = []
    current_chunk = []
    
    # imiter reads the video one frame at a time without loading the whole file into RAM
    for frame in iio.imiter(str(video_path), plugin="pyav"):
        current_chunk.append(frame)
        
        if len(current_chunk) == chunk_size:
            chunk_np = np.stack(current_chunk)
            if is_flow:
                resized = resize_gray_short_side(chunk_np, short_side, chunk_size)
            else:
                resized = resize_rgb_short_side(chunk_np, short_side, chunk_size)
            resized_chunks.append(resized)
            current_chunk = []
            
    # Process any remaining frames
    if current_chunk:
        chunk_np = np.stack(current_chunk)
        if is_flow:
            resized = resize_gray_short_side(chunk_np, short_side, chunk_size)
        else:
            resized = resize_rgb_short_side(chunk_np, short_side, chunk_size)
        resized_chunks.append(resized)
        
    return np.concatenate(resized_chunks, axis=0)


def process_item(
    item: HACItem,
    datapath: Path,
    out_dir: Path,
    short_side: int,
    chunk_size: int,
    use_video: bool,
    use_flow: bool,
    use_audio: bool,
    overwrite: bool,
    compressed: bool,
) -> Optional[str]:
    save_path = out_dir / f"{item.mode_prefix}_{item.domain}_{item.video_id}.npz"
    if save_path.exists() and not overwrite:
        return None

    dom_dir = datapath / item.domain
    arrays: dict[str, np.ndarray | np.integer] = {
        "label": np.int64(item.label),
        "video_id": np.array(item.video_id),
        "domain": np.array(item.domain),
        "mode_prefix": np.array(item.mode_prefix),
    }

    if use_video:
        video_path = dom_dir / "videos" / item.video_id
        # Use the new streaming function instead of imread
        arrays["video"] = _process_video_stream(video_path, short_side, chunk_size, is_flow=False)

    if use_flow:
        stem = os.path.splitext(item.video_id)[0]
        flow_x_path = dom_dir / "flow" / f"{stem}_flow_x.mp4"
        flow_y_path = dom_dir / "flow" / f"{stem}_flow_y.mp4"
        # Stream flow videos
        arrays["flow_x"] = _process_video_stream(flow_x_path, short_side, chunk_size, is_flow=True)
        arrays["flow_y"] = _process_video_stream(flow_y_path, short_side, chunk_size, is_flow=True)

    if use_audio:
        stem = os.path.splitext(item.video_id)[0]
        audio_path = dom_dir / "audio" / f"{stem}.wav"
        samples, samplerate = _load_audio(audio_path)
        arrays["audio_samples"] = samples
        arrays["samplerate"] = np.int64(samplerate)

    _save_npz(save_path, arrays, compressed=compressed)
    return str(save_path)


def build_items(datapath: Path, domains: Iterable[str]) -> list[HACItem]:
    # Training loader expects train_<domain>_<video_id>.npz for HAC_train_only_<domain>.csv.
    train_items = list(_read_csv_items(datapath, split="train", source=True, domains=domains, mode_prefix="train"))

    # Validation/test loader uses val pipeline. For source=False, original HACDOMAIN includes both
    # HAC_test_only_<domain>.csv and HAC_train_only_<domain>.csv, so we bake both under val_*.
    val_items = list(_read_csv_items(datapath, split="test", source=False, domains=domains, mode_prefix="val"))

    # Remove exact duplicate output paths while preserving order.
    seen = set()
    unique: list[HACItem] = []
    for item in train_items + val_items:
        key = (item.mode_prefix, item.domain, item.video_id)
        if key not in seen:
            unique.append(item)
            seen.add(key)
    return unique


def run_preprocessing() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--datapath", type=str, default="./data/", help="Root HAC path containing HAC_Splits/ and domain folders.")
    parser.add_argument("--out-dir", type=str, default=None, help="Default: <datapath>/processed_tensors")
    parser.add_argument("--domains", nargs="+", default=["human", "animal", "cartoon"])
    parser.add_argument("--short-side", type=int, default=256)
    parser.add_argument("--chunk-size", type=int, default=64, help="Number of frames resized per torch chunk.")
    parser.add_argument("--compressed", action="store_true", help="Use np.savez_compressed. Smaller but slower.")
    parser.add_argument("--overwrite", action="store_true")
    parser.add_argument("--no-video", action="store_true")
    parser.add_argument("--no-flow", action="store_true")
    parser.add_argument("--no-audio", action="store_true")
    args = parser.parse_args()

    datapath = Path(args.datapath)
    out_dir = Path(args.out_dir) if args.out_dir is not None else datapath / "processed_tensors"
    out_dir.mkdir(parents=True, exist_ok=True)

    items = build_items(datapath, args.domains)
    print(f"Found {len(items)} offline items -> {out_dir}")
    print("Baking decode + Resize(short_side=256) only. Temporal/spatial/audio randomness stays in FastHAC.")

    skipped = 0
    written = 0
    failed: list[tuple[HACItem, str]] = []

    for item in tqdm(items):
        try:
            result = process_item(
                item=item,
                datapath=datapath,
                out_dir=out_dir,
                short_side=args.short_side,
                chunk_size=args.chunk_size,
                use_video=not args.no_video,
                use_flow=not args.no_flow,
                use_audio=not args.no_audio,
                overwrite=args.overwrite,
                compressed=args.compressed,
            )
            if result is None:
                skipped += 1
            else:
                written += 1
        except Exception as exc:  # noqa: BLE001 - report all corrupt/missing samples and continue.
            failed.append((item, repr(exc)))

    print(f"Done. written={written}, skipped={skipped}, failed={len(failed)}")
    if failed:
        fail_log = out_dir / "preprocess_failures.txt"
        with fail_log.open("w") as f:
            for item, err in failed:
                f.write(f"{item.mode_prefix},{item.domain},{item.video_id},{item.label}: {err}\n")
        print(f"Failure log: {fail_log}")


if __name__ == "__main__":
    run_preprocessing()
