"""Tests for task lifecycle timestamps and recurrence logic."""
from datetime import date, datetime, timezone
from unittest.mock import AsyncMock, MagicMock, patch


# ── Timestamp side-effect tests ──────────────────────────────────────────────

async def test_update_note_sets_started_at_on_in_progress():
    """started_at is set when status transitions to in_progress."""
    mock_note = MagicMock()
    mock_note.status = "in_progress"
    mock_note.started_at = None
    mock_note.recurrence_rule = None

    mock_session = make_mock_session()
    mock_result = MagicMock()
    mock_result.scalars.return_value.first.return_value = mock_note
    mock_session.execute = AsyncMock(return_value=mock_result)
    mock_session.commit = AsyncMock()
    mock_session.refresh = AsyncMock()

    with patch("scribe.services.notes.async_session", return_value=mock_session):
        from scribe.services.notes import update_note
        await update_note(1, 1, status="in_progress")

    assert mock_note.started_at is not None


async def test_update_note_sets_completed_at_on_done():
    """completed_at is set when status transitions to done."""
    mock_note = MagicMock()
    mock_note.status = "done"
    mock_note.started_at = datetime(2026, 3, 1, tzinfo=timezone.utc)
    mock_note.completed_at = None
    mock_note.recurrence_rule = None

    mock_session = make_mock_session()
    mock_result = MagicMock()
    mock_result.scalars.return_value.first.return_value = mock_note
    mock_session.execute = AsyncMock(return_value=mock_result)
    mock_session.commit = AsyncMock()
    mock_session.refresh = AsyncMock()

    with patch("scribe.services.notes.async_session", return_value=mock_session):
        from scribe.services.notes import update_note
        await update_note(1, 1, status="done")

    assert mock_note.completed_at is not None


async def test_update_note_clears_timestamps_on_todo():
    """started_at and completed_at are cleared when status resets to todo."""
    mock_note = MagicMock()
    mock_note.status = "todo"
    mock_note.started_at = datetime(2026, 3, 1, tzinfo=timezone.utc)
    mock_note.completed_at = datetime(2026, 3, 15, tzinfo=timezone.utc)
    mock_note.recurrence_rule = None

    mock_session = make_mock_session()
    mock_result = MagicMock()
    mock_result.scalars.return_value.first.return_value = mock_note
    mock_session.execute = AsyncMock(return_value=mock_result)
    mock_session.commit = AsyncMock()
    mock_session.refresh = AsyncMock()

    with patch("scribe.services.notes.async_session", return_value=mock_session):
        from scribe.services.notes import update_note
        await update_note(1, 1, status="todo")

    assert mock_note.started_at is None
    assert mock_note.completed_at is None


async def test_update_note_preserves_started_at_if_already_set():
    """started_at is not overwritten on a second transition to in_progress."""
    original_start = datetime(2026, 3, 1, tzinfo=timezone.utc)
    mock_note = MagicMock()
    mock_note.status = "in_progress"
    mock_note.started_at = original_start
    mock_note.recurrence_rule = None

    mock_session = make_mock_session()
    mock_result = MagicMock()
    mock_result.scalars.return_value.first.return_value = mock_note
    mock_session.execute = AsyncMock(return_value=mock_result)
    mock_session.commit = AsyncMock()
    mock_session.refresh = AsyncMock()

    with patch("scribe.services.notes.async_session", return_value=mock_session):
        from scribe.services.notes import update_note
        await update_note(1, 1, status="in_progress")

    assert mock_note.started_at == original_start


# ── Recurrence rule validation ────────────────────────────────────────────────

import pytest
from tests.helpers import make_mock_session


def test_validate_interval_rule_valid():
    from scribe.services.recurrence import validate_recurrence_rule
    validate_recurrence_rule({"type": "interval", "every": 3, "unit": "month"})  # no error


def test_validate_interval_rule_bad_unit():
    from scribe.services.recurrence import validate_recurrence_rule
    with pytest.raises(ValueError, match="unit"):
        validate_recurrence_rule({"type": "interval", "every": 3, "unit": "fortnight"})


def test_validate_interval_rule_bad_every():
    from scribe.services.recurrence import validate_recurrence_rule
    with pytest.raises(ValueError, match="every"):
        validate_recurrence_rule({"type": "interval", "every": 0, "unit": "week"})


def test_validate_calendar_monthly_valid():
    from scribe.services.recurrence import validate_recurrence_rule
    validate_recurrence_rule({"type": "calendar", "unit": "month", "day_of_month": 1})


def test_validate_calendar_annual_valid():
    from scribe.services.recurrence import validate_recurrence_rule
    validate_recurrence_rule(
        {"type": "calendar", "unit": "year", "day_of_month": 15, "month": 6}
    )


def test_validate_calendar_bad_day():
    from scribe.services.recurrence import validate_recurrence_rule
    with pytest.raises(ValueError, match="day_of_month"):
        validate_recurrence_rule({"type": "calendar", "unit": "month", "day_of_month": 32})


def test_validate_calendar_annual_missing_month():
    from scribe.services.recurrence import validate_recurrence_rule
    with pytest.raises(ValueError, match="month"):
        validate_recurrence_rule(
            {"type": "calendar", "unit": "year", "day_of_month": 15}
        )


def test_validate_unknown_type():
    from scribe.services.recurrence import validate_recurrence_rule
    with pytest.raises(ValueError, match="type"):
        validate_recurrence_rule({"type": "cron", "every": 1, "unit": "day"})


# ── calculate_next_due ────────────────────────────────────────────────────────

def test_calculate_next_due_interval_days():
    from scribe.services.recurrence import calculate_next_due
    rule = {"type": "interval", "every": 7, "unit": "day"}
    assert calculate_next_due(rule, date(2026, 3, 27)) == date(2026, 4, 3)


def test_calculate_next_due_interval_weeks():
    from scribe.services.recurrence import calculate_next_due
    rule = {"type": "interval", "every": 2, "unit": "week"}
    assert calculate_next_due(rule, date(2026, 3, 27)) == date(2026, 4, 10)


def test_calculate_next_due_interval_months():
    from scribe.services.recurrence import calculate_next_due
    rule = {"type": "interval", "every": 3, "unit": "month"}
    assert calculate_next_due(rule, date(2026, 3, 1)) == date(2026, 6, 1)


def test_calculate_next_due_interval_months_end_of_month():
    """Month addition clamps to last day when target month is shorter."""
    from scribe.services.recurrence import calculate_next_due
    rule = {"type": "interval", "every": 1, "unit": "month"}
    assert calculate_next_due(rule, date(2026, 1, 31)) == date(2026, 2, 28)


def test_calculate_next_due_interval_years():
    from scribe.services.recurrence import calculate_next_due
    rule = {"type": "interval", "every": 1, "unit": "year"}
    assert calculate_next_due(rule, date(2026, 3, 27)) == date(2027, 3, 27)


def test_calculate_next_due_calendar_monthly_future_day():
    """If day_of_month is later this month, return that date."""
    from scribe.services.recurrence import calculate_next_due
    rule = {"type": "calendar", "unit": "month", "day_of_month": 28}
    assert calculate_next_due(rule, date(2026, 3, 1)) == date(2026, 3, 28)


def test_calculate_next_due_calendar_monthly_same_or_past_day():
    """If day_of_month is today or earlier, advance to next month."""
    from scribe.services.recurrence import calculate_next_due
    rule = {"type": "calendar", "unit": "month", "day_of_month": 1}
    assert calculate_next_due(rule, date(2026, 3, 1)) == date(2026, 4, 1)


def test_calculate_next_due_calendar_annual_future():
    """Annual date later this year is returned."""
    from scribe.services.recurrence import calculate_next_due
    rule = {"type": "calendar", "unit": "year", "day_of_month": 15, "month": 6}
    assert calculate_next_due(rule, date(2026, 3, 27)) == date(2026, 6, 15)


def test_calculate_next_due_calendar_annual_past():
    """Annual date already passed this year → next year."""
    from scribe.services.recurrence import calculate_next_due
    rule = {"type": "calendar", "unit": "year", "day_of_month": 15, "month": 1}
    assert calculate_next_due(rule, date(2026, 3, 27)) == date(2027, 1, 15)


# ── spawn_recurring_tasks ─────────────────────────────────────────────────────

async def test_spawn_recurring_tasks_creates_child():
    from unittest.mock import AsyncMock, MagicMock, patch

    mock_task = MagicMock()
    mock_task.id = 1
    mock_task.user_id = 42
    mock_task.title = "Change air filter"
    mock_task.body = ""
    mock_task.priority = "medium"
    mock_task.tags = []
    mock_task.project_id = None
    mock_task.milestone_id = None
    mock_task.due_date = date(2026, 3, 1)
    mock_task.recurrence_rule = {"type": "interval", "every": 3, "unit": "month"}

    mock_session = make_mock_session()
    mock_result = MagicMock()
    mock_result.scalars.return_value.all.return_value = [mock_task]
    mock_session.execute = AsyncMock(return_value=mock_result)
    mock_session.get = AsyncMock(return_value=mock_task)
    mock_session.commit = AsyncMock()

    mock_child = MagicMock()
    mock_child.id = 2
    mock_child.title = "Change air filter"
    mock_child.body = ""

    with (
        patch("scribe.services.recurrence.async_session", return_value=mock_session),
        patch(
            "scribe.services.notes.create_note",
            new_callable=AsyncMock,
            return_value=mock_child,
        ) as mock_create,
    ):
        from scribe.services.recurrence import spawn_recurring_tasks
        count = await spawn_recurring_tasks()

    assert count == 1
    mock_create.assert_called_once()
    kwargs = mock_create.call_args[1]
    assert kwargs["title"] == "Change air filter"
    assert kwargs["due_date"] == date(2026, 6, 1)
    assert kwargs["recurrence_rule"] == {"type": "interval", "every": 3, "unit": "month"}
    assert kwargs["status"] == "todo"


# ── Multi-status filtering ────────────────────────────────────────────────────

async def test_list_notes_multi_status_builds_in_clause():
    """list_notes with a list of statuses executes without error."""
    from unittest.mock import AsyncMock, MagicMock, patch

    mock_session = make_mock_session()
    mock_result = MagicMock()
    mock_result.scalars.return_value.all.return_value = []
    mock_session.execute = AsyncMock(return_value=mock_result)
    mock_session.scalar = AsyncMock(return_value=0)

    with patch("scribe.services.notes.async_session", return_value=mock_session):
        from scribe.services.notes import list_notes
        notes, total = await list_notes(1, status=["todo", "in_progress"], is_task=True)

    assert total == 0
    assert notes == []
    mock_session.scalar.assert_called_once()
    mock_session.execute.assert_called_once()
