"""abreka exp fail — mark an experiment as failed."""

from __future__ import annotations

import click

from abreka.config import find_abreka_dir
from abreka.models import ExperimentStatus
from abreka.persistence import (
    load_experiment,
    normalize_exp_id,
    save_experiment,
    update_index_entry,
)


@click.command("fail")
@click.argument("exp_id")
@click.option("--reason", "-r", required=True, help="Explanation of what went wrong.")
def fail_cmd(exp_id: str, reason: str):
    """Mark an experiment as failed with an explanation."""
    abreka_dir = find_abreka_dir()
    exp_id = normalize_exp_id(exp_id)
    exp = load_experiment(abreka_dir, exp_id)

    exp.status = ExperimentStatus.FAILED
    exp.failure_reason = reason

    save_experiment(exp, abreka_dir)
    update_index_entry(abreka_dir, exp)
    click.echo(f"Experiment {exp_id} marked as failed")
