"""abreka backlog delete — remove a backlog item."""

from __future__ import annotations

import click

from abreka.config import find_abreka_dir
from abreka.persistence import (
    backlog_dir,
    delete_backlog_item,
    load_backlog_index,
    normalize_backlog_id,
)


@click.command("delete")
@click.argument("item_id")
@click.option("--yes", "-y", is_flag=True, help="Skip confirmation.")
def delete_cmd(item_id: str, yes: bool):
    """Delete a backlog item."""
    abreka_dir = find_abreka_dir()
    item_id = normalize_backlog_id(item_id)

    toml_path = backlog_dir(abreka_dir) / f"{item_id}.toml"
    index = load_backlog_index(abreka_dir)

    if item_id not in index and not toml_path.exists():
        raise click.ClickException(f"Backlog item {item_id} not found.")

    if not yes:
        click.confirm(f"Delete backlog item {item_id}?", abort=True)

    delete_backlog_item(abreka_dir, item_id)
    click.echo(f"Deleted backlog item {item_id}")
