from datetime import UTC, datetime, timedelta

from worker_watcher.db.repositories import WatcherRepository


def test_retention_dry_run_does_not_delete(connection) -> None:
    repository = WatcherRepository(connection)
    old = (datetime.now(UTC) - timedelta(days=400)).isoformat().replace("+00:00", "Z")
    connection.execute("INSERT INTO watcher_events(event_type,severity,message,created_at,component) VALUES(?,?,?,?,?)",
                       ("test", "info", "old", old, "test"))
    connection.commit()
    counts = repository.retention({"job_observations": 30, "job_runs": 180, "status_events": 180,
                                   "collector_runs": 90, "watcher_events": 90}, dry_run=True)
    assert counts["watcher_events"] == 1
    assert connection.execute("SELECT COUNT(*) FROM watcher_events").fetchone()[0] == 1


def test_retention_apply_deletes_old_watcher_event(connection) -> None:
    repository = WatcherRepository(connection)
    old = (datetime.now(UTC) - timedelta(days=400)).isoformat().replace("+00:00", "Z")
    connection.execute("INSERT INTO watcher_events(event_type,severity,message,created_at,component) VALUES(?,?,?,?,?)",
                       ("test", "info", "old", old, "test"))
    connection.commit()
    repository.retention({"job_observations": 30, "job_runs": 180, "status_events": 180,
                          "collector_runs": 90, "watcher_events": 90}, dry_run=False)
    assert connection.execute("SELECT COUNT(*) FROM watcher_events").fetchone()[0] == 0
