"""Runs a saved MIME experiment on Modal. Written by `mime modal`, and the only file of the run that imports modal.

    modal run modal_app.py::main --duration 5    # check the environment, run 5 s, save the run on the runs Volume
    modal run modal_app.py::main --duration 5 --record <observation id>.<output>,... --every 0.01
                                                 # the same, recording those per-step outputs, one sample every 10 ms
    modal run modal_app.py::verify               # check the environment, then rerun the reference steps
    modal run modal_app.py::verify --run <id>    # the same for a run saved earlier: bit for bit in its environment
    modal run modal_app.py::fetch                # store the assets the experiment's lock references on the Volume

The image is built once per MIME release, Python series and variant, never per experiment, and every input to it is
pinned by hash or digest (uv included). The experiment, its assets and XLA's cache travel beside the image, never
inside it. Each function runs in at most MAX_CONTAINERS containers (one unless `mime modal --max-containers` said
otherwise) and is never retried.
"""

import hashlib
import json
import os
import pathlib
import subprocess
import time
import uuid

@@CONSTANTS@@

# Another image builder builds another image: the version is pinned, and the run records it.
os.environ["MODAL_IMAGE_BUILDER_VERSION"] = IMAGE_BUILDER_VERSION

import modal  # noqa: E402

HERE = pathlib.Path(__file__).resolve().parent
PINNED = "--require-hashes --no-deps --no-build"
# uv is copied from its image by digest alone: Modal's builder refuses a reference with both a tag and a digest, which
# is all `uv_pip_install(uv_version=)` can give it. This one reference is what the build pulls and what the run records.
UV_IMAGE = f"ghcr.io/astral-sh/uv@{UV_DIGEST}"
UV_BINARY = "/.uv/uv"
PINS_DIR = "/.pins"
EXPERIMENT_DIR = "/experiment"
LAB_FILE = "/mime-run/lab-requirements.txt"
RUNS_DIR = "/runs"
dev_wheel_sha256 = None


def _dev_wheel():
    """A wheel of the MIME checkout, built here, and the requirements file that names it by its sha256. Hatchling
    builds reproducibly, so an unchanged checkout gives the same bytes and the layer stays cached."""
    global dev_wheel_sha256
    out = HERE / "dev-wheel"
    out.mkdir(exist_ok=True)
    for old in out.glob("*.whl"):
        old.unlink()
    subprocess.run(["uv", "build", "--wheel", "--out-dir", str(out), CHECKOUT], check=True)
    (wheel,) = out.glob("*.whl")
    dev_wheel_sha256 = hashlib.sha256(wheel.read_bytes()).hexdigest()
    requirements = out / "mime-requirements.txt"
    requirements.write_text(f"mime-engine @ file:///wheels/{wheel.name} \\\n    --hash=sha256:{dev_wheel_sha256}\n")
    return wheel, requirements


def _install(image, requirements, options):
    """What `Image.uv_pip_install` runs, with the uv this script copied in: the requirements file is placed in the image
    and installed into the image's Python."""
    held = f"{PINS_DIR}/{pathlib.Path(requirements).name}"
    image = image.add_local_file(str(requirements), held, copy=True)
    return image.run_commands(f"{UV_BINARY} pip install --python $(command -v python) --compile-bytecode {options} "
                              f"--requirements {held}")


def _image():
    # 1. The base: Python and Debian, pinned by digest.
    image = modal.Image.from_registry(BASE)
    # uv, from its image by digest: a layer of its own, which the installs below share.
    image = image.dockerfile_commands(f"COPY --from={UV_IMAGE} /uv {UV_BINARY}")
    # 2. The variant's pins, MIME's requirements file byte for byte: every one by version and hash, nothing built.
    image = _install(image, HERE / REQUIREMENTS, PINNED)
    # 3. MIME's own wheel, by version and its hash on PyPI; before a release, a wheel of the checkout by its hash.
    if MIME_WHEEL_SHA256 is not None:
        image = _install(image, HERE / "mime-requirements.txt", PINNED)
    elif modal.is_local():
        wheel, requirements = _dev_wheel()
        image = image.add_local_file(str(wheel), f"/wheels/{wheel.name}", copy=True)
        image = _install(image, requirements, PINNED)
    # 4. The lab's distributions the lock names: by hash when the lab gave hashes, by version otherwise.
    if LAB is not None:
        image = _install(image, HERE / LAB, PINNED if LAB_HASHED else "--no-deps")
        image = image.add_local_file(str(HERE / LAB), LAB_FILE, copy=False)
    # The experiment is mounted when a container starts: never a layer, so a new experiment rebuilds nothing.
    return image.add_local_dir(str((HERE / EXPERIMENT).resolve()), EXPERIMENT_DIR, copy=False)


app = modal.App(f"mime-{NAME}")
assets = modal.Volume.from_name("mime-assets", create_if_missing=True, version=2)
xla = modal.Volume.from_name("mime-xla-cache", create_if_missing=True, version=2)
runs = modal.Volume.from_name("mime-runs", create_if_missing=True, version=2)
image = _image()

ENV = {"MIME_ASSETS_DIR": "/assets", "MIME_OFFLINE": "1", "XLA_FLAGS": XLA_FLAGS, "JAX_ENABLE_X64": "1" if X64 else "0"}
if GPU is not None:
    ENV["JAX_DEFAULT_MATMUL_PRECISION"] = "highest"
else:
    ENV["JAX_PLATFORMS"] = "cpu"
# At most MAX_CONTAINERS containers at once and no retry: a `.map()` or a loop of `.remote()` cannot fan out, and a
# failed run is not paid for again unasked.
LIMITS = dict(timeout=TIMEOUT, max_containers=MAX_CONTAINERS, retries=0)
RUN = dict(image=image, gpu=GPU, cpu=CPU, memory=MEMORY, env=ENV, **LIMITS,
           volumes={"/assets": assets.with_mount_options(read_only=True), "/xla": xla, RUNS_DIR: runs})


def _mime(*arguments):
    """`mime <arguments>` in this process, so that the precision the check applies holds for what follows it."""
    from mime.cli import main as mime

    print("$ mime " + " ".join(arguments), flush=True)
    code = mime(list(arguments))
    if code != 0:
        raise RuntimeError(f"mime {' '.join(arguments[:2])} exited {code}")


def _record_runner(runner, info):
    os.environ["MIME_RUNNER"] = json.dumps(runner, sort_keys=True)
    os.environ["MIME_RUNNER_INFO"] = json.dumps(info, sort_keys=True)


def _check(folder, record=None):
    _mime("env", "check", folder, "--variant", VARIANT, "--base", "--mime-version", MIME_VERSION,
          *(["--gpu", GPU] if GPU is not None else []), *(["--lab", LAB_FILE] if LAB is not None else []),
          *(["--xla-cache", "/xla"] if XLA_CACHE else []), *(["--record", record] if record else []))


@app.function(**RUN)
def run_experiment(duration: float, runner: dict, info: dict, record: str = "", every: float = 0.0) -> str:
    _record_runner(runner, info)
    run_id = time.strftime("%Y%m%dT%H%M%SZ", time.gmtime()) + "-" + uuid.uuid4().hex[:8]
    out = f"{RUNS_DIR}/{NAME}/{run_id}"
    _check(EXPERIMENT_DIR, f"{out}/env-check.json")
    recording = [part for path in record.split(",") if path.strip() for part in ("--record", path.strip())]
    spacing = ["--every", repr(float(every))] if every > 0 else []
    _mime("run", EXPERIMENT_DIR, "--duration", repr(float(duration)), "--out", out, *recording, *spacing)
    runs.commit()
    return run_id


@app.function(**RUN)
def verify_experiment(run_id: str, runner: dict, info: dict) -> int:
    from mime.cli import main as mime

    _record_runner(runner, info)
    folder = f"{RUNS_DIR}/{NAME}/{run_id}/experiment" if run_id else EXPERIMENT_DIR
    _check(folder)
    return mime(["verify", folder])


@app.function(image=image, env={"MIME_ASSETS_DIR": "/assets"}, volumes={"/assets": assets}, **LIMITS)
def fetch_assets() -> None:
    _mime("assets", "fetch", EXPERIMENT_DIR + "/")
    assets.commit()


def _runner():
    """Where the run is made, as two records for its lock. The first decides the results and a load compares it: the
    builder, the base, the variant, the pins' and the uv image's identity (the release MIME's uv.json names, at the
    digest this script copied uv by), the GPU requested. The second is information that does not (the Modal client that
    launched the run, the workspace's image ID, which differs between workspaces for one build): MIME stores it and
    never compares it. Neither changes from one run to the next in one environment."""
    record = {"runner": "modal", "image_builder": IMAGE_BUILDER_VERSION, "base": BASE, "variant": VARIANT,
              "gpu_request": GPU or "none", "requirements_sha256": REQUIREMENTS_SHA256,
              "uv": f"{UV_VERSION}@{UV_IMAGE.rsplit('@', 1)[1]}"}
    if MIME_WHEEL_SHA256 is None:
        record["mime_wheel_sha256"] = dev_wheel_sha256 or "not built"
    if LAB is not None:
        record["lab_pins"] = "hashed" if LAB_HASHED else "versions only"
    info = {"modal_client": modal.__version__}
    try:
        info["image"] = image.object_id
    except Exception:  # noqa: BLE001
        info["image"] = "unreadable"
    return record, info


@app.local_entrypoint()
def main(duration: float = 0.0, record: str = "", every: float = 0.0):
    if duration <= 0:
        raise SystemExit("pass --duration <seconds>: a whole number of the experiment's base steps")
    if ASSETS:
        fetch_assets.remote()
    run_id = run_experiment.remote(duration, *_runner(), record=record, every=every)
    # The destination is the current directory, which exists: the client (1.6.3) fails on a folder it must create.
    print(f"saved run {run_id}; fetch it into the current directory with: modal volume get mime-runs {NAME}/{run_id} . "
          f"and read it with mime.results.RunResult.read(\"{run_id}\")")


@app.local_entrypoint()
def verify(run: str = ""):
    if ASSETS:
        fetch_assets.remote()
    code = verify_experiment.remote(run, *_runner())
    if code != 0:
        raise SystemExit(code)


@app.local_entrypoint()
def fetch():
    fetch_assets.remote()
