"""abreka backlog dismiss — mark a backlog item as dismissed."""

from __future__ import annotations

from datetime import datetime, timezone

import click

from abreka.config import find_abreka_dir
from abreka.models import BacklogStatus
from abreka.persistence import (
    load_backlog_item,
    normalize_backlog_id,
    save_backlog_item,
    update_backlog_index_entry,
)


@click.command("dismiss")
@click.argument("item_id")
@click.option("--reason", "-r", default="", help="Why this idea was dismissed.")
def dismiss_cmd(item_id: str, reason: str):
    """Mark a backlog item as dismissed."""
    abreka_dir = find_abreka_dir()
    item_id = normalize_backlog_id(item_id)
    item = load_backlog_item(abreka_dir, item_id)

    item.status = BacklogStatus.DISMISSED
    item.dismiss_reason = reason
    item.updated_at = datetime.now(timezone.utc)

    save_backlog_item(item, abreka_dir)
    update_backlog_index_entry(abreka_dir, item)
    click.echo(f"Backlog item {item_id} dismissed")
