"""abreka exp new — create a new experiment."""

from __future__ import annotations

import click

from abreka.config import find_abreka_dir
from abreka.models import Experiment
from abreka.persistence import (
    allocate_experiment_id,
    create_experiment_dir,
    save_experiment,
    update_index_entry,
)


@click.command("new")
@click.option("--hypothesis", "-h", default="", help="Experiment hypothesis.")
@click.option("--method", "-m", default="", help="Experiment method.")
@click.option("--parent", "-p", "parents", multiple=True, help="Parent experiment ID(s).")
@click.option("--tag", "-t", "tags", multiple=True, help="Tags for categorization.")
def new_cmd(hypothesis: str, method: str, parents: tuple[str, ...], tags: tuple[str, ...]):
    """Create a new experiment in 'proposed' status."""
    abreka_dir = find_abreka_dir()
    exp_id = allocate_experiment_id(abreka_dir)

    exp = Experiment(
        id=exp_id,
        hypothesis=hypothesis,
        method=method,
        parents=list(parents),
        tags=list(tags),
    )

    create_experiment_dir(abreka_dir, exp_id)
    save_experiment(exp, abreka_dir)
    update_index_entry(abreka_dir, exp)

    click.echo(exp_id)
