"""Tests for abreka backlog subcommands."""

import json
import os

from click.testing import CliRunner

from abreka.cli import main


class TestBacklogAdd:
    def test_creates_item(self, abreka_project):
        runner = CliRunner()
        os.chdir(abreka_project)
        result = runner.invoke(main, [
            "backlog", "add",
            "--title", "Try attention mechanism",
            "--description", "Based on findings from exp 0002",
            "--tag", "arch",
            "--priority", "3",
            "--source-exp", "0002",
        ])
        assert result.exit_code == 0, result.output
        assert "0001" in result.output

        # Verify TOML file exists
        toml_path = abreka_project / "abreka" / "backlog" / "0001.toml"
        assert toml_path.is_file()

        # Verify index updated
        index = json.loads((abreka_project / "abreka" / "backlog_index.json").read_text())
        assert "0001" in index
        assert index["0001"]["title"] == "Try attention mechanism"
        assert index["0001"]["priority"] == 3

    def test_sequential_ids(self, abreka_project):
        runner = CliRunner()
        os.chdir(abreka_project)
        r1 = runner.invoke(main, ["backlog", "add", "--title", "First"])
        r2 = runner.invoke(main, ["backlog", "add", "--title", "Second"])
        assert "0001" in r1.output
        assert "0002" in r2.output


class TestBacklogEdit:
    def test_edit_fields(self, abreka_project):
        runner = CliRunner()
        os.chdir(abreka_project)
        runner.invoke(main, ["backlog", "add", "--title", "Original title"])
        result = runner.invoke(main, [
            "backlog", "edit", "1",
            "--title", "Updated title",
            "--priority", "5",
            "--tag", "new-tag",
        ])
        assert result.exit_code == 0

        index = json.loads((abreka_project / "abreka" / "backlog_index.json").read_text())
        assert index["0001"]["title"] == "Updated title"
        assert index["0001"]["priority"] == 5
        assert index["0001"]["tags"] == ["new-tag"]


class TestBacklogList:
    def test_empty(self, abreka_project):
        runner = CliRunner()
        os.chdir(abreka_project)
        result = runner.invoke(main, ["backlog", "list"])
        assert "No backlog" in result.output

    def test_with_items(self, abreka_project):
        runner = CliRunner()
        os.chdir(abreka_project)
        runner.invoke(main, ["backlog", "add", "--title", "First"])
        runner.invoke(main, ["backlog", "add", "--title", "Second"])
        result = runner.invoke(main, ["backlog", "list"])
        assert result.exit_code == 0
        assert "0001" in result.output
        assert "0002" in result.output

    def test_filter_by_status(self, abreka_project):
        runner = CliRunner()
        os.chdir(abreka_project)
        runner.invoke(main, ["backlog", "add", "--title", "First"])
        runner.invoke(main, ["backlog", "add", "--title", "Second"])
        runner.invoke(main, ["backlog", "resolve", "1"])
        result = runner.invoke(main, ["backlog", "list", "--status", "open"])
        assert "0002" in result.output
        assert "0001" not in result.output


class TestBacklogShow:
    def test_show_item(self, abreka_project):
        runner = CliRunner()
        os.chdir(abreka_project)
        runner.invoke(main, ["backlog", "add", "--title", "Test idea", "--priority", "3"])
        result = runner.invoke(main, ["backlog", "show", "1"])
        assert result.exit_code == 0
        assert "Test idea" in result.output


class TestBacklogResolve:
    def test_resolve(self, abreka_project):
        runner = CliRunner()
        os.chdir(abreka_project)
        runner.invoke(main, ["backlog", "add", "--title", "Idea"])
        result = runner.invoke(main, ["backlog", "resolve", "1", "--exp", "0005"])
        assert result.exit_code == 0

        index = json.loads((abreka_project / "abreka" / "backlog_index.json").read_text())
        assert index["0001"]["status"] == "resolved"
        assert index["0001"]["resolved_exp"] == "0005"


class TestBacklogDismiss:
    def test_dismiss(self, abreka_project):
        runner = CliRunner()
        os.chdir(abreka_project)
        runner.invoke(main, ["backlog", "add", "--title", "Bad idea"])
        result = runner.invoke(main, ["backlog", "dismiss", "1", "--reason", "Already tried"])
        assert result.exit_code == 0

        import tomllib
        toml_path = abreka_project / "abreka" / "backlog" / "0001.toml"
        with open(toml_path, "rb") as f:
            data = tomllib.load(f)
        assert data["status"] == "dismissed"
        assert data["dismiss_reason"] == "Already tried"


class TestBacklogDelete:
    def test_delete(self, abreka_project):
        runner = CliRunner()
        os.chdir(abreka_project)
        runner.invoke(main, ["backlog", "add", "--title", "To delete"])
        result = runner.invoke(main, ["backlog", "delete", "1", "--yes"])
        assert result.exit_code == 0

        assert not (abreka_project / "abreka" / "backlog" / "0001.toml").exists()
        index = json.loads((abreka_project / "abreka" / "backlog_index.json").read_text())
        assert "0001" not in index


class TestBacklogPrioritize:
    def test_prioritize(self, abreka_project):
        runner = CliRunner()
        os.chdir(abreka_project)
        runner.invoke(main, ["backlog", "add", "--title", "Idea"])
        result = runner.invoke(main, ["backlog", "prioritize", "1", "10"])
        assert result.exit_code == 0

        index = json.loads((abreka_project / "abreka" / "backlog_index.json").read_text())
        assert index["0001"]["priority"] == 10
