"""Train a convolutional autoencoder on recorded TH06 observation grids."""

from __future__ import annotations

import argparse
from pathlib import Path

import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, IterableDataset, get_worker_info


CHECKPOINT_VERSION = 2


class GridBatchDataset(IterableDataset[torch.Tensor]):
    """Load non-empty grids sequentially, one compressed chunk at a time."""

    def __init__(
        self,
        files: list[Path],
        batch_size: int,
        shuffle: bool = True,
    ):
        super().__init__()
        self.files = files
        self.batch_size = batch_size
        self.shuffle = shuffle

        self.sample_count = 0
        self.empty_samples = 0
        self.active_indices: list[np.ndarray] = []

        for path in files:
            with np.load(path) as data:
                danger = data["danger"]
                powerup = data["powerup"]

                if (
                    danger.shape != powerup.shape
                    or danger.ndim != 3
                    or danger.shape[1:] != (32, 32)
                ):
                    raise ValueError(
                        f"Invalid grid shapes in {path}: "
                        f"{danger.shape} and {powerup.shape}"
                    )

                if not np.isfinite(danger).all():
                    raise ValueError(f"Non-finite danger values in {path}")

                if not np.isfinite(powerup).all():
                    raise ValueError(f"Non-finite powerup values in {path}")

                data_min = min(float(danger.min()), float(powerup.min()))
                data_max = max(float(danger.max()), float(powerup.max()))

                if data_min < 0.0 or data_max > 1.0:
                    raise ValueError(
                        f"Grid values in {path} are outside [0, 1]: "
                        f"min={data_min}, max={data_max}. "
                        "Normalize the dataset before training."
                    )

                active = np.flatnonzero(
                    np.any(danger != 0, axis=(1, 2))
                    | np.any(powerup != 0, axis=(1, 2))
                )

                self.active_indices.append(active)
                self.sample_count += len(active)
                self.empty_samples += len(danger) - len(active)

        if self.sample_count == 0:
            raise RuntimeError("Dataset contains no non-empty samples")

    def __len__(self) -> int:
        return (self.sample_count + self.batch_size - 1) // self.batch_size

    def __iter__(self):
        if get_worker_info() is not None:
            raise RuntimeError(
                "Sequential chunk loading requires DataLoader workers=0"
            )

        for path, active_indices in zip(self.files, self.active_indices):
            indices = active_indices.copy()

            if self.shuffle:
                np.random.shuffle(indices)

            with np.load(path) as data:
                danger = data["danger"]
                powerup = data["powerup"]

                for start in range(0, len(indices), self.batch_size):
                    batch_indices = indices[start:start + self.batch_size]

                    batch = np.stack(
                        (
                            danger[batch_indices],
                            powerup[batch_indices],
                        ),
                        axis=1,
                    )

                    yield torch.from_numpy(
                        batch.astype(np.float32, copy=False)
                    )


class GridAutoencoder(nn.Module):
    def __init__(self, latent_dim: int = 64):
        super().__init__()

        self.encoder = nn.Sequential(
            nn.Conv2d(2, 16, 3, padding=1),
            nn.ReLU(),

            nn.Conv2d(16, 32, 3, stride=2, padding=1),
            nn.ReLU(),

            nn.Conv2d(32, 64, 3, stride=2, padding=1),
            nn.ReLU(),

            nn.Flatten(),

            nn.Linear(64 * 8 * 8, latent_dim),
        )

        self.decoder = nn.Sequential(
            nn.Linear(latent_dim, 64 * 8 * 8),
            nn.ReLU(),

            nn.Unflatten(1, (64, 8, 8)),

            nn.ConvTranspose2d(
                64,
                32,
                4,
                stride=2,
                padding=1,
            ),
            nn.ReLU(),

            nn.ConvTranspose2d(
                32,
                16,
                4,
                stride=2,
                padding=1,
            ),
            nn.ReLU(),

            # Deliberately NO Sigmoid here.
            #
            # A Sigmoid allowed the decoder to collapse toward zero on
            # the extremely sparse grids. Once saturated near zero,
            # gradients through the Sigmoid became effectively zero.
            nn.Conv2d(16, 2, 3, padding=1),
        )

    def forward(self, inputs: torch.Tensor) -> torch.Tensor:
        return self.decoder(self.encoder(inputs))


def weighted_mse(
    reconstruction: torch.Tensor,
    target: torch.Tensor,
) -> torch.Tensor:
    """
    MSE with increased weight on active pixels.

    Empty pixels:
        weight = 1

    Target = 0.5:
        weight = 6

    Target = 1:
        weight = 11
    """
    weight = 1.0 + 10.0 * target
    return ((reconstruction - target).square() * weight).mean()


def training_device() -> torch.device:
    if torch.cuda.is_available():
        return torch.device("cuda")

    if getattr(torch, "xpu", None) is not None and torch.xpu.is_available():
        return torch.device("xpu")

    if torch.backends.mps.is_available():
        return torch.device("mps")

    return torch.device("cpu")


def train(
    dataset_directory: Path,
    output_path: Path,
    latent_dim: int = 64,
    batch_size: int = 256,
    epochs: int = 30,
) -> None:
    files = sorted(dataset_directory.glob("chunk_*.npz"))

    if not files:
        raise RuntimeError(
            f"No dataset chunks found in {dataset_directory}"
        )

    dataset = GridBatchDataset(
        files,
        batch_size,
        shuffle=True,
    )

    device = training_device()

    loader = DataLoader(
        dataset,
        batch_size=None,
        num_workers=0,
        pin_memory=device.type in {"cuda", "xpu"},
    )

    checkpoint = None
    completed_epochs = 0

    # ------------------------------------------------------------------
    # Checkpoint handling
    #
    # The old checkpoint was trained with a Sigmoid output and plain MSE.
    # Do NOT resume it: start a fresh model with the fixed architecture.
    # ------------------------------------------------------------------

    if output_path.exists():
        candidate = torch.load(
            output_path,
            map_location=device,
            weights_only=False,
        )

        if (
            isinstance(candidate, dict)
            and candidate.get("checkpoint_version") == CHECKPOINT_VERSION
            and "model" in candidate
        ):
            checkpoint = candidate
            latent_dim = checkpoint.get("latent_dim", latent_dim)
            completed_epochs = checkpoint.get("completed_epochs", 0)

            print(
                f"Resuming checkpoint at epoch "
                f"{completed_epochs:,} from {output_path}."
            )
        else:
            print(
                f"Ignoring incompatible old checkpoint at {output_path}; "
                "starting fresh with the fixed autoencoder."
            )

    model = GridAutoencoder(
        latent_dim=latent_dim,
    ).to(device)

    optimizer = torch.optim.AdamW(
        model.parameters(),
        lr=1e-3,
        weight_decay=1e-5,
    )

    if checkpoint is not None:
        model.load_state_dict(checkpoint["model"])

        if "optimizer" in checkpoint:
            optimizer.load_state_dict(checkpoint["optimizer"])

    print(
        f"Training on {dataset.sample_count:,} non-empty samples "
        f"from {len(files)} chunks using {device}; "
        f"removed {dataset.empty_samples:,} empty samples."
    )

    print(
        f"latent_dim={latent_dim}, "
        f"batch_size={batch_size}, "
        f"loss=weighted_mse"
    )

    output_path.parent.mkdir(
        parents=True,
        exist_ok=True,
    )

    for epoch in range(
        completed_epochs,
        completed_epochs + epochs,
    ):
        model.train()

        total_loss = 0.0
        processed_samples = 0

        for inputs in loader:
            inputs = inputs.to(
                device,
                non_blocking=True,
            )

            # Forward pass.
            reconstruction = model(inputs)

            # Weighted reconstruction loss.
            loss = weighted_mse(
                reconstruction,
                inputs,
            )

            # Backpropagation.
            optimizer.zero_grad(set_to_none=True)
            loss.backward()
            optimizer.step()

            total_loss += (
                loss.item() * inputs.size(0)
            )

            processed_samples += inputs.size(0)

        epoch_loss = total_loss / processed_samples

        # Diagnostic parameter norm.
        with torch.no_grad():
            param_norm = sum(
                parameter.norm().item()
                for parameter in model.parameters()
            )

        # Diagnostic gradient norm.
        grad_norm = sum(
            parameter.grad.norm().item()
            for parameter in model.parameters()
            if parameter.grad is not None
        )

        # Inspect the final decoder output distribution.
        model.eval()

        with torch.no_grad():
            sample_inputs = next(iter(loader)).to(
                device,
                non_blocking=True,
            )

            sample_reconstruction = model(sample_inputs)

            reconstruction_mean = (
                sample_reconstruction.mean().item()
            )

            reconstruction_max = (
                sample_reconstruction.max().item()
            )

            reconstruction_min = (
                sample_reconstruction.min().item()
            )

            target_mean = sample_inputs.mean().item()

            target_active_fraction = (
                (sample_inputs > 0.01)
                .float()
                .mean()
                .item()
            )

            reconstruction_active_fraction = (
                (sample_reconstruction > 0.01)
                .float()
                .mean()
                .item()
            )

        print(
            f"epoch {epoch + 1}: "
            f"loss={epoch_loss:.12f}"
        )

        print(
            f"  params={param_norm:.6f} "
            f"grads={grad_norm:.6f}"
        )

        print(
            f"  target_mean={target_mean:.6f} "
            f"target_active={target_active_fraction:.4%}"
        )

        print(
            f"  reconstruction="
            f"[{reconstruction_min:.4f}, "
            f"{reconstruction_max:.4f}] "
            f"mean={reconstruction_mean:.6f} "
            f"active={reconstruction_active_fraction:.4%}"
        )

        # Save checkpoint.
        torch.save(
            {
                "checkpoint_version": CHECKPOINT_VERSION,
                "model": model.state_dict(),
                "optimizer": optimizer.state_dict(),
                "latent_dim": latent_dim,
                "completed_epochs": epoch + 1,
                "loss": "weighted_mse",
                "output_activation": "none",
            },
            output_path,
        )

    print(f"Saved autoencoder to {output_path}")


def parse_args() -> argparse.Namespace:
    cnn_directory = Path(__file__).resolve().parent
    repository_directory = cnn_directory.parent.parent

    parser = argparse.ArgumentParser(
        description=__doc__
    )

    parser.add_argument(
        "--dataset-dir",
        type=Path,
        default=repository_directory / "dataset",
    )

    parser.add_argument(
        "--output",
        type=Path,
        default=cnn_directory / "weights" / "grid_autoencoder.pt",
    )

    parser.add_argument(
        "--latent-dim",
        type=int,
        default=64,
    )

    parser.add_argument(
        "--batch-size",
        type=int,
        default=256,
    )

    parser.add_argument(
        "--epochs",
        type=int,
        default=30,
    )

    return parser.parse_args()


if __name__ == "__main__":
    arguments = parse_args()

    train(
        arguments.dataset_dir,
        arguments.output,
        arguments.latent_dim,
        arguments.batch_size,
        arguments.epochs,
    )
