"""abreka backlog add — create a new backlog item."""

from __future__ import annotations

import click

from abreka.config import find_abreka_dir
from abreka.models import BacklogItem
from abreka.persistence import (
    next_backlog_id,
    save_backlog_item,
    update_backlog_index_entry,
)


@click.command("add")
@click.option("--title", "-t", required=True, help="Short summary of the idea.")
@click.option("--description", "-d", default="", help="Longer rationale or notes.")
@click.option("--tag", "tags", multiple=True, help="Tags for categorization.")
@click.option("--priority", "-p", type=int, default=0, help="Priority (higher = more important).")
@click.option("--source-exp", default="", help="Experiment ID that spawned this idea.")
def add_cmd(title: str, description: str, tags: tuple[str, ...], priority: int, source_exp: str):
    """Add a new idea to the backlog."""
    abreka_dir = find_abreka_dir()
    item_id = next_backlog_id(abreka_dir)

    item = BacklogItem(
        id=item_id,
        title=title,
        description=description,
        tags=list(tags),
        priority=priority,
        source_exp=source_exp,
    )

    save_backlog_item(item, abreka_dir)
    update_backlog_index_entry(abreka_dir, item)

    click.echo(item_id)
