"""abreka exp finish — mark an experiment as completed."""

from __future__ import annotations

from datetime import datetime, timezone

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("finish")
@click.argument("exp_id")
def finish_cmd(exp_id: str):
    """Mark an experiment as completed."""
    abreka_dir = find_abreka_dir()
    exp_id = normalize_exp_id(exp_id)
    exp = load_experiment(abreka_dir, exp_id)

    exp.status = ExperimentStatus.COMPLETED
    exp.completed_at = datetime.now(timezone.utc)

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