#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Suite19 immutable salvage / resume support.

This module DOES NOT:
- change Suite19 model/cache signatures;
- rewrite representation/probe caches;
- infer missing scientific results;
- mark incomplete branches complete.

It only:
1. inventories completed Fold artifacts;
2. validates chronology and serialized caches;
3. fingerprints expensive artifacts;
4. verifies they were not changed during a resume.
"""

from __future__ import annotations

import argparse
import gc
import hashlib
import json
from datetime import datetime, timezone
from pathlib import Path

import joblib
import numpy as np
import pandas as pd


SCHEMA_VERSION = 1

DEFAULT_ENCODERS = [
    "tabm",
    "modern_nca",
    "modern_nca_hard",
    "modern_nca_mechanism",
    "rfm",
]

DEFAULT_SEEDS = [
    42,
    43,
    44,
]


def _utc_now():
    return datetime.now(
        timezone.utc
    ).isoformat()


def _sha256(path: Path, chunk=8 * 1024 * 1024):
    h = hashlib.sha256()

    with path.open("rb") as f:
        while True:
            b = f.read(chunk)

            if not b:
                break

            h.update(b)

    return h.hexdigest()


def _atomic_json(
    obj,
    path: Path,
):
    path.parent.mkdir(
        parents=True,
        exist_ok=True,
    )

    tmp = path.with_suffix(
        path.suffix + ".tmp"
    )

    tmp.write_text(
        json.dumps(
            obj,
            indent=2,
            sort_keys=True,
            default=str,
        )
        + "\n",
        encoding="utf-8",
    )

    tmp.replace(path)


def _bool_series(x):
    if x.dtype == bool:
        return x

    return (
        x.astype(str)
        .str.strip()
        .str.lower()
        .isin(
            [
                "true",
                "1",
                "yes",
                "y",
            ]
        )
    )


def _audit_csv(path: Path):
    result = {
        "path": str(path),
        "exists": path.exists(),
        "safe": False,
        "violations": None,
        "error": None,
    }

    if not path.exists():
        result["error"] = "missing"
        return result

    try:
        d = pd.read_csv(path)

        if not len(d):
            result["safe"] = True
            result["violations"] = 0
            return result

        if (
            "strict_train_before_query"
            not in d.columns
        ):
            result["error"] = (
                "strict_train_before_query "
                "column missing"
            )
            return result

        strict = _bool_series(
            d[
                "strict_train_before_query"
            ]
        )

        if "available" in d.columns:
            available = _bool_series(
                d["available"]
            )
        else:
            available = np.ones(
                len(d),
                dtype=bool,
            )

        violations = int(
            np.sum(
                np.asarray(available)
                & ~np.asarray(strict)
            )
        )

        result["violations"] = (
            violations
        )

        result["safe"] = (
            violations == 0
        )

        return result

    except Exception as e:
        result["error"] = (
            f"{type(e).__name__}: {e}"
        )

        return result


def _validate_joblib(path: Path):
    result = {
        "path": str(path),
        "load_ok": False,
        "class": None,
        "signature": None,
        "chronology_violations": None,
        "error": None,
    }

    try:
        obj = joblib.load(path)

        result["load_ok"] = True
        result["class"] = (
            f"{type(obj).__module__}."
            f"{type(obj).__name__}"
        )

        sig = getattr(
            obj,
            "signature",
            None,
        )

        if sig is not None:
            result["signature"] = str(sig)

        cv = getattr(
            obj,
            "chronology_violations",
            None,
        )

        if cv is not None:
            result[
                "chronology_violations"
            ] = int(cv)

            if int(cv) != 0:
                result["load_ok"] = False
                result["error"] = (
                    "serialized object contains "
                    f"{int(cv)} chronology "
                    "violations"
                )

        del obj
        gc.collect()

    except Exception as e:
        result["error"] = (
            f"{type(e).__name__}: {e}"
        )

    return result


def _selection_info(
    fold_dir: Path,
    encoder: str,
):
    path = (
        fold_dir
        / f"{encoder}_selection_candidates.csv"
    )

    result = {
        "path": str(path),
        "exists": path.exists(),
        "candidate_rows": 0,
        "ok_candidates": 0,
        "error_candidates": 0,
    }

    if not path.exists():
        return result

    try:
        d = pd.read_csv(path)

        result["candidate_rows"] = len(d)

        if "status" in d.columns:
            status = (
                d.status
                .astype(str)
                .str.lower()
            )

            result["ok_candidates"] = int(
                (status == "ok").sum()
            )

            result["error_candidates"] = int(
                status.isin(
                    [
                        "error",
                        "rejected",
                        "reject",
                    ]
                ).sum()
            )

    except Exception as e:
        result["error"] = repr(e)

    return result


def _probe_owner(
    filename: str,
    encoders,
):
    stem = Path(filename).stem

    matches = [
        e
        for e in encoders
        if stem.startswith(e + "_")
    ]

    if not matches:
        return None

    # modern_nca_hard must win over
    # modern_nca.
    return max(
        matches,
        key=len,
    )


def _probe_files_for(
    probe_dir: Path,
    encoder: str,
    encoders,
):
    if not probe_dir.exists():
        return []

    out = []

    for p in probe_dir.glob(
        "*.joblib"
    ):
        if (
            _probe_owner(
                p.name,
                encoders,
            )
            == encoder
        ):
            out.append(p)

    return sorted(out)


def _seed_output_state(
    fold_dir: Path,
    encoder: str,
    seed: int,
):
    sd = (
        fold_dir
        / encoder
        / f"seed_{seed}"
    )

    chrono = _audit_csv(
        sd / "chronology_audit.csv"
    )

    probe = _audit_csv(
        sd
        / "probe_chronology_audit.csv"
    )

    meta = (
        sd / "model_metadata.json"
    )

    complete = bool(
        chrono["safe"]
        and probe["safe"]
        and meta.exists()
    )

    return {
        "seed": int(seed),
        "directory": str(sd),
        "chronology": chrono,
        "probe_chronology": probe,
        "model_metadata_exists":
            meta.exists(),
        "complete": complete,
    }


def _seed_metrics_state(
    fold_dir: Path,
    encoder: str,
    seeds,
):
    path = (
        fold_dir
        / encoder
        / "seed_target_metrics.csv"
    )

    out = {
        "path": str(path),
        "exists": path.exists(),
        "seeds_found": [],
        "all_requested_seeds": False,
    }

    if not path.exists():
        return out

    try:
        d = pd.read_csv(path)

        if "seed" in d.columns:
            found = sorted(
                set(
                    int(x)
                    for x
                    in pd.to_numeric(
                        d.seed,
                        errors="coerce",
                    ).dropna()
                )
            )

            out["seeds_found"] = found
            out[
                "all_requested_seeds"
            ] = set(seeds).issubset(
                set(found)
            )

    except Exception as e:
        out["error"] = repr(e)

    return out


def _artifact_record(
    path: Path,
    kind: str,
    encoder: str,
):
    st = path.stat()

    return {
        "path": str(
            path.resolve()
        ),
        "encoder": encoder,
        "kind": kind,
        "bytes": int(st.st_size),
        "mtime_ns": int(
            st.st_mtime_ns
        ),
        "sha256": _sha256(path),
    }


def inspect_encoder(
    fold_dir: Path,
    encoder: str,
    seeds,
    all_encoders,
    validate_joblib=True,
):
    cache = fold_dir / "cache"
    probe_cache = (
        fold_dir / "probe_cache"
    )

    selection_rep = sorted(
        cache.glob(
            f"{encoder}_selection_*.joblib"
        )
    )

    production_rep = sorted(
        cache.glob(
            f"{encoder}_production_*.joblib"
        )
    )

    probes = _probe_files_for(
        probe_cache,
        encoder,
        all_encoders,
    )

    selection = _selection_info(
        fold_dir,
        encoder,
    )

    selected_config = (
        fold_dir
        / f"{encoder}_selected_config.json"
    )

    seed_metrics = (
        _seed_metrics_state(
            fold_dir,
            encoder,
            seeds,
        )
    )

    seed_states = [
        _seed_output_state(
            fold_dir,
            encoder,
            seed,
        )
        for seed in seeds
    ]

    joblib_validation = []

    if validate_joblib:
        for p in (
            selection_rep
            + production_rep
            + probes
        ):
            joblib_validation.append(
                _validate_joblib(p)
            )

    joblib_safe = all(
        x["load_ok"]
        for x in joblib_validation
    ) if validate_joblib else True

    chronology_safe = all(
        x["complete"]
        for x in seed_states
    )

    ok_candidates = int(
        selection.get(
            "ok_candidates",
            0,
        )
    )

    expected_probe_min = (
        ok_candidates
        + len(seeds)
    )

    complete = bool(
        selected_config.exists()
        and ok_candidates >= 1
        and len(selection_rep)
            >= ok_candidates
        and len(production_rep)
            >= len(seeds)
        and len(probes)
            >= expected_probe_min
        and seed_metrics[
            "all_requested_seeds"
        ]
        and chronology_safe
        and joblib_safe
    )

    unsafe = bool(
        not chronology_safe
        and any(
            x[
                "chronology"
            ].get(
                "violations",
                0,
            )
            not in (
                None,
                0,
            )
            or x[
                "probe_chronology"
            ].get(
                "violations",
                0,
            )
            not in (
                None,
                0,
            )
            for x in seed_states
        )
    )

    all_candidate_errors = bool(
        selection["candidate_rows"] > 0
        and selection[
            "ok_candidates"
        ] == 0
    )

    if unsafe:
        status = "unsafe"

    elif complete:
        status = "complete"

    elif (
        all_candidate_errors
        and not selected_config.exists()
    ):
        status = (
            "pending_failed_selection"
        )

    elif (
        not selection["exists"]
        and not selected_config.exists()
        and not selection_rep
        and not production_rep
    ):
        status = "missing"

    else:
        status = "partial"

    return {
        "encoder": encoder,
        "status": status,
        "selected_config_exists":
            selected_config.exists(),
        "selected_config_path":
            str(selected_config),
        "selection": selection,
        "selection_rep_cache_count":
            len(selection_rep),
        "production_rep_cache_count":
            len(production_rep),
        "production_seed_count":
            sum(
                x["complete"]
                for x in seed_states
            ),
        "probe_cache_count":
            len(probes),
        "expected_probe_min":
            expected_probe_min,
        "seed_metrics": seed_metrics,
        "seed_states": seed_states,
        "joblib_validation":
            joblib_validation,
        "_immutable_paths": (
            selection_rep
            + production_rep
            + probes
        ) if complete else [],
    }


def build_salvage_manifest(
    out_dir,
    fold=1,
    encoders=None,
    seeds=None,
    validate_joblib=True,
):
    out_dir = Path(out_dir)

    encoders = (
        list(encoders)
        if encoders is not None
        else list(DEFAULT_ENCODERS)
    )

    seeds = (
        list(seeds)
        if seeds is not None
        else list(DEFAULT_SEEDS)
    )

    fold_dir = (
        out_dir / f"fold_{fold}"
    )

    if not fold_dir.exists():
        raise FileNotFoundError(
            f"Fold directory missing: "
            f"{fold_dir}"
        )

    branches = {}

    immutable = []

    for enc in encoders:
        state = inspect_encoder(
            fold_dir,
            enc,
            seeds,
            encoders,
            validate_joblib=
                validate_joblib,
        )

        paths = state.pop(
            "_immutable_paths"
        )

        branches[enc] = state

        for p in paths:
            kind = (
                "probe_cache"
                if p.parent.name
                == "probe_cache"
                else (
                    "representation_selection"
                    if "_selection_"
                    in p.name
                    else
                    "representation_production"
                )
            )

            immutable.append(
                _artifact_record(
                    p,
                    kind,
                    enc,
                )
            )

    return {
        "schema_version":
            SCHEMA_VERSION,
        "created_at": _utc_now(),
        "out_dir": str(
            out_dir.resolve()
        ),
        "fold": int(fold),
        "fold_dir": str(
            fold_dir.resolve()
        ),
        "seeds": [
            int(x)
            for x in seeds
        ],
        "encoders": encoders,
        "branches": branches,
        "immutable_artifacts":
            immutable,
        "immutable_bytes": int(
            sum(
                x["bytes"]
                for x in immutable
            )
        ),
    }


def verify_salvage_manifest(
    manifest_path,
):
    manifest_path = Path(
        manifest_path
    )

    obj = json.loads(
        manifest_path.read_text(
            encoding="utf-8"
        )
    )

    mismatches = []

    checked = 0

    for rec in obj.get(
        "immutable_artifacts",
        [],
    ):
        p = Path(rec["path"])

        checked += 1

        if not p.exists():
            mismatches.append(
                {
                    "path": str(p),
                    "reason": "missing",
                }
            )
            continue

        size = p.stat().st_size

        if size != rec["bytes"]:
            mismatches.append(
                {
                    "path": str(p),
                    "reason":
                        "size_changed",
                    "expected":
                        rec["bytes"],
                    "actual": size,
                }
            )
            continue

        digest = _sha256(p)

        if digest != rec["sha256"]:
            mismatches.append(
                {
                    "path": str(p),
                    "reason":
                        "sha256_changed",
                    "expected":
                        rec["sha256"],
                    "actual": digest,
                }
            )

    return {
        "ok": len(mismatches) == 0,
        "checked": checked,
        "mismatches": mismatches,
    }


def _parse_csv_strings(x):
    return [
        y.strip()
        for y in str(x).split(",")
        if y.strip()
    ]


def _parse_seeds(x):
    return [
        int(y)
        for y in _parse_csv_strings(x)
    ]


def _print_manifest_summary(
    manifest,
):
    print(
        "\n"
        + "=" * 100
    )

    print(
        "SUITE19 FOLD SALVAGE AUDIT"
    )

    print(
        "=" * 100
    )

    print(
        f"fold={manifest['fold']} "
        f"immutable_artifacts="
        f"{len(manifest['immutable_artifacts'])} "
        f"immutable_GiB="
        f"{manifest['immutable_bytes'] / 1024**3:.3f}"
    )

    print()

    for enc in manifest[
        "encoders"
    ]:
        b = manifest[
            "branches"
        ][enc]

        print(
            f"{enc:24s} "
            f"status={b['status']:26s} "
            f"selection_rep="
            f"{b['selection_rep_cache_count']:2d} "
            f"production_rep="
            f"{b['production_rep_cache_count']:2d} "
            f"probe="
            f"{b['probe_cache_count']:2d} "
            f"seeds="
            f"{b['production_seed_count']}"
        )


def main(argv=None):
    p = argparse.ArgumentParser(
        description=(
            "Suite19 immutable Fold "
            "salvage/resume guard"
        )
    )

    sub = p.add_subparsers(
        dest="command",
        required=True,
    )

    ps = sub.add_parser(
        "snapshot"
    )

    ps.add_argument(
        "--out_dir",
        required=True,
    )

    ps.add_argument(
        "--fold",
        type=int,
        default=1,
    )

    ps.add_argument(
        "--encoders",
        default=",".join(
            DEFAULT_ENCODERS
        ),
    )

    ps.add_argument(
        "--seeds",
        default="42,43,44",
    )

    ps.add_argument(
        "--manifest",
        default="",
    )

    ps.add_argument(
        "--expect_complete",
        default="",
    )

    ps.add_argument(
        "--expect_pending",
        default="",
    )

    ps.add_argument(
        "--skip_joblib_validation",
        action="store_true",
    )

    pv = sub.add_parser(
        "verify"
    )

    pv.add_argument(
        "--manifest",
        required=True,
    )

    args = p.parse_args(argv)

    if args.command == "snapshot":
        encoders = _parse_csv_strings(
            args.encoders
        )

        seeds = _parse_seeds(
            args.seeds
        )

        m = build_salvage_manifest(
            args.out_dir,
            fold=args.fold,
            encoders=encoders,
            seeds=seeds,
            validate_joblib=(
                not args
                .skip_joblib_validation
            ),
        )

        _print_manifest_summary(m)

        expect_complete = (
            _parse_csv_strings(
                args.expect_complete
            )
        )

        expect_pending = (
            _parse_csv_strings(
                args.expect_pending
            )
        )

        errors = []

        for enc in expect_complete:
            status = m[
                "branches"
            ][enc]["status"]

            if status != "complete":
                errors.append(
                    f"{enc}: expected "
                    f"complete, got {status}"
                )

        for enc in expect_pending:
            status = m[
                "branches"
            ][enc]["status"]

            if status not in {
                "pending_failed_selection",
                "partial",
                "missing",
            }:
                errors.append(
                    f"{enc}: expected "
                    f"pending, got {status}"
                )

        manifest_path = (
            Path(args.manifest)
            if args.manifest
            else (
                Path(args.out_dir)
                / f"fold_{args.fold}"
                / "salvage"
                / (
                    f"fold_{args.fold}"
                    "_salvage_manifest.json"
                )
            )
        )

        _atomic_json(
            m,
            manifest_path,
        )

        print(
            f"\n[MANIFEST] "
            f"{manifest_path}"
        )

        if errors:
            print(
                "\n[SALVAGE FAILED]"
            )

            for e in errors:
                print(
                    "  - " + e
                )

            return 2

        print(
            "\n[SALVAGE PASS] "
            "Existing expensive artifacts "
            "are validated and fingerprinted."
        )

        return 0

    result = (
        verify_salvage_manifest(
            args.manifest
        )
    )

    print(
        "\n"
        + "=" * 100
    )

    print(
        "SUITE19 SALVAGE "
        "IMMUTABILITY VERIFY"
    )

    print(
        "=" * 100
    )

    print(
        f"checked={result['checked']} "
        f"mismatches="
        f"{len(result['mismatches'])}"
    )

    if not result["ok"]:
        for r in result[
            "mismatches"
        ]:
            print(
                "  FAIL",
                json.dumps(r),
            )

        return 3

    print(
        "[PASS] All salvaged "
        "expensive cache artifacts "
        "remain byte-identical."
    )

    return 0


if __name__ == "__main__":
    raise SystemExit(main())