"""Step definitions for plan CLI helper function branch coverage."""

from __future__ import annotations

from datetime import UTC, datetime, timedelta
from typing import Any

from behave import given, then, when


@given("the plan branch coverage helpers are initialized")
def step_plan_br_init(context: Any) -> None:
    """Import plan module helpers."""
    from cleveragents.cli.commands.plan import _format_relative_time

    context._format_relative_time = _format_relative_time
    context.result = None


@given("a plan-br naive datetime 2 hours in the past")
def step_plan_br_2h_past(context: Any) -> None:
    """Create a naive datetime 2 hours ago."""
    context.dt = datetime.now(UTC).replace(tzinfo=None) - timedelta(hours=2)


@given("a plan-br naive datetime 10 minutes in the future")
def step_plan_br_10m_future(context: Any) -> None:
    """Create a datetime 10 minutes in the future."""
    context.dt = datetime.now(UTC) + timedelta(minutes=10)


@given("a plan-br naive datetime 1 minute in the past")
def step_plan_br_1m_past(context: Any) -> None:
    """Create a naive datetime 1 minute ago."""
    context.dt = datetime.now(UTC).replace(tzinfo=None) - timedelta(minutes=1)


@given("a plan-br naive datetime 1 hour in the past")
def step_plan_br_1h_past(context: Any) -> None:
    """Create a naive datetime 1 hour ago."""
    context.dt = datetime.now(UTC).replace(tzinfo=None) - timedelta(hours=1)


@given("a plan-br naive datetime 1 day in the past")
def step_plan_br_1d_past(context: Any) -> None:
    """Create a naive datetime 1 day ago."""
    context.dt = datetime.now(UTC).replace(tzinfo=None) - timedelta(days=1)


@given("a plan-br naive datetime 2 minutes in the past")
def step_plan_br_2m_past(context: Any) -> None:
    """Create a naive datetime 2 minutes ago."""
    context.dt = datetime.now(UTC).replace(tzinfo=None) - timedelta(minutes=2)


@given("a plan-br naive datetime 2 days in the past")
def step_plan_br_2d_past(context: Any) -> None:
    """Create a naive datetime 2 days ago."""
    context.dt = datetime.now(UTC).replace(tzinfo=None) - timedelta(days=2)


@given("a plan-br naive datetime 30 seconds in the past")
def step_plan_br_30s_past(context: Any) -> None:
    """Create a naive datetime 30 seconds ago."""
    context.dt = datetime.now(UTC).replace(tzinfo=None) - timedelta(seconds=30)


@when("I call _format_relative_time on it")
def step_call_format_relative_time(context: Any) -> None:
    """Call _format_relative_time."""
    context.result = context._format_relative_time(context.dt)


@then('the plan-br relative time should contain "{text}"')
def step_plan_br_contains(context: Any, text: str) -> None:
    """Verify result contains text."""
    assert text in context.result, f"Expected '{text}' in '{context.result}'"


@then('the plan-br relative time should be "{expected}"')
def step_plan_br_equals(context: Any, expected: str) -> None:
    """Verify result equals expected."""
    assert context.result == expected, f"Expected '{expected}', got '{context.result}'"
