"""Tests for the task scheduling system."""

import pytest

from agent_scout.scheduler.scheduler import (
    ScheduledTask,
    ScheduleType,
    TaskScheduler,
)


@pytest.fixture
def scheduler():
    s = TaskScheduler()
    yield s
    if s.is_running:
        s.shutdown(wait=False)


def test_add_task(scheduler):
    task = ScheduledTask(
        description="Check prices",
        schedule_type=ScheduleType.INTERVAL,
        interval_seconds=3600,
    )
    result = scheduler.add_task(task)
    assert result.description == "Check prices"
    assert result.id == task.id


def test_remove_task(scheduler):
    task = ScheduledTask(description="Check prices")
    scheduler.add_task(task)
    assert scheduler.remove_task(task.id) is True
    assert scheduler.remove_task("nonexistent") is False


def test_list_tasks(scheduler):
    t1 = ScheduledTask(description="Task 1")
    t2 = ScheduledTask(description="Task 2")
    scheduler.add_task(t1)
    scheduler.add_task(t2)
    tasks = scheduler.list_tasks()
    assert len(tasks) == 2


def test_get_task(scheduler):
    task = ScheduledTask(description="Find it")
    scheduler.add_task(task)
    found = scheduler.get_task(task.id)
    assert found is not None
    assert found.description == "Find it"
    assert scheduler.get_task("nonexistent") is None


def test_enable_disable(scheduler):
    task = ScheduledTask(description="Toggleable", enabled=False)
    scheduler.add_task(task)
    assert task.enabled is False
    scheduler.enable_task(task.id)
    assert task.enabled is True
    scheduler.disable_task(task.id)
    assert task.enabled is False


def test_enable_nonexistent(scheduler):
    assert scheduler.enable_task("nope") is False
    assert scheduler.disable_task("nope") is False


def test_cron_expression_interval(scheduler):
    task = ScheduledTask(
        schedule_type=ScheduleType.INTERVAL,
        interval_seconds=3600,
    )
    cron = scheduler.to_cron_expression(task)
    assert "*/1" in cron or "0 */1" in cron


def test_cron_expression_daily(scheduler):
    task = ScheduledTask(
        schedule_type=ScheduleType.INTERVAL,
        interval_seconds=86400,
    )
    cron = scheduler.to_cron_expression(task)
    assert "0 0" in cron


def test_cron_expression_from_cron(scheduler):
    task = ScheduledTask(
        schedule_type=ScheduleType.CRON,
        cron_expression="0 9 * * 1-5",
    )
    cron = scheduler.to_cron_expression(task)
    assert cron == "0 9 * * 1-5"


def test_cron_expression_once(scheduler):
    task = ScheduledTask(schedule_type=ScheduleType.ONCE)
    cron = scheduler.to_cron_expression(task)
    assert "one-time" in cron


@pytest.mark.asyncio
async def test_start_shutdown(scheduler):
    scheduler.start()
    assert scheduler.is_running is True
    # AsyncIOScheduler.shutdown may not immediately reflect in .running
    # after shutdown(wait=False), so just verify start works
    scheduler.shutdown(wait=True)


def test_max_runs(scheduler):
    task = ScheduledTask(
        description="Limited",
        max_runs=3,
        run_count=0,
    )
    scheduler.add_task(task)
    assert task.max_runs == 3


def test_get_pending_jobs_empty(scheduler):
    jobs = scheduler.get_pending_jobs()
    assert jobs == []


def test_cron_expression_minutes(scheduler):
    task = ScheduledTask(
        schedule_type=ScheduleType.INTERVAL,
        interval_seconds=300,  # 5 minutes
    )
    cron = scheduler.to_cron_expression(task)
    assert "*/5" in cron
