from __future__ import annotations

import pytest

from api.services.lifecycle import (
    has_active_task_status,
    should_activate_parent_for_active_tasks,
    should_activate_parent_for_task_start,
    status_value,
    transition_task_status,
    transition_workstream_status,
)


@pytest.mark.parametrize("parent_status", ["proposed", "ready", "backlog"])
def test_task_start_activates_planning_parent(parent_status):
    assert should_activate_parent_for_task_start(
        previous_task_status="todo",
        new_task_status="progress",
        parent_workstream_status=parent_status,
    )


@pytest.mark.parametrize("parent_status", ["active", "blocked", "finished", "archived"])
def test_task_start_does_not_rewrite_non_planning_parent(parent_status):
    assert not should_activate_parent_for_task_start(
        previous_task_status="todo",
        new_task_status="progress",
        parent_workstream_status=parent_status,
    )


def test_task_start_requires_todo_to_progress_transition():
    assert not should_activate_parent_for_task_start(
        previous_task_status="progress",
        new_task_status="progress",
        parent_workstream_status="ready",
    )
    assert not should_activate_parent_for_task_start(
        previous_task_status="todo",
        new_task_status="done",
        parent_workstream_status="ready",
    )


def test_has_active_task_status_ignores_terminal_and_todo_statuses():
    assert has_active_task_status(["todo", "done", "cancel"]) is False
    assert has_active_task_status(["todo", "wait"]) is True
    assert has_active_task_status(["progress"]) is True


def test_active_task_state_activates_planning_parent_for_renormalization():
    assert should_activate_parent_for_active_tasks(
        parent_workstream_status="proposed",
        task_statuses=["todo", "progress"],
    )


def test_active_task_state_does_not_unblock_blocked_parent():
    assert not should_activate_parent_for_active_tasks(
        parent_workstream_status="blocked",
        task_statuses=["progress"],
    )


def test_status_value_unwraps_enum_like_values():
    class Status:
        value = "In_Progress"

    assert status_value(Status()) == "progress"


def test_transition_workstream_status_normalizes_aliases():
    class Workstream:
        status = "todo"

    ws = Workstream()
    result = transition_workstream_status(ws, "done")

    assert ws.status == "finished"
    assert result.previous_status == "ready"
    assert result.target_status == "finished"
    assert result.changed is True


def test_transition_task_status_activates_parent_once():
    class Task:
        status = "todo"

    class Workstream:
        status = "ready"

    task = Task()
    ws = Workstream()
    result = transition_task_status(task, "progress", parent_workstream=ws)

    assert task.status == "progress"
    assert ws.status == "active"
    assert result.parent_activated is True
    assert result.previous_status == "todo"
    assert result.target_status == "progress"
