"""Tests for scheduler."""
import pytest
from ayn_antivirus.core.scheduler import Scheduler, _cron_to_schedule, _parse_cron_field
from ayn_antivirus.config import Config


def test_scheduler_init():
    config = Config()
    s = Scheduler(config)
    assert s is not None
    assert s.config is config


def test_cron_parse_simple():
    """Standard daily-at-midnight expression."""
    result = _cron_to_schedule("0 0 * * *")
    assert result["minutes"] == [0]
    assert result["hours"] == [0]


def test_cron_parse_step():
    """Every-5-minutes expression."""
    result = _cron_to_schedule("*/5 * * * *")
    assert 0 in result["minutes"]
    assert 5 in result["minutes"]
    assert 55 in result["minutes"]
    assert len(result["minutes"]) == 12


def test_cron_parse_range():
    """Specific range of hours."""
    result = _cron_to_schedule("30 9-17 * * *")
    assert result["minutes"] == [30]
    assert result["hours"] == list(range(9, 18))


def test_cron_parse_invalid():
    """Invalid cron expression raises ValueError."""
    with pytest.raises(ValueError, match="5-field"):
        _cron_to_schedule("bad input")


def test_schedule_scan():
    config = Config()
    s = Scheduler(config)
    # Scheduling should not crash
    s.schedule_scan("0 0 * * *", "full")
    s.schedule_scan("30 2 * * *", "quick")
    # Jobs should have been registered
    jobs = s._scheduler.get_jobs()
    assert len(jobs) >= 2


def test_schedule_update():
    config = Config()
    s = Scheduler(config)
    s.schedule_update(interval_hours=6)
    jobs = s._scheduler.get_jobs()
    assert len(jobs) >= 1


def test_parse_cron_field_literal():
    assert _parse_cron_field("5", 0, 59) == [5]


def test_parse_cron_field_comma():
    assert _parse_cron_field("1,3,5", 0, 59) == [1, 3, 5]


def test_parse_cron_field_wildcard():
    result = _parse_cron_field("*", 0, 6)
    assert result == [0, 1, 2, 3, 4, 5, 6]
