"""Tests for database.py."""

from datetime import datetime

import pytest

from modpack_checker.database import Database


# ---------------------------------------------------------------------------
# add_modpack
# ---------------------------------------------------------------------------


def test_add_modpack_returns_correct_fields(db):
    mp = db.add_modpack(12345, "Test Pack")
    assert mp.curseforge_id == 12345
    assert mp.name == "Test Pack"
    assert mp.current_version is None
    assert mp.last_checked is None
    assert mp.notification_enabled is True


def test_add_modpack_duplicate_raises(db):
    db.add_modpack(12345, "Test Pack")
    with pytest.raises(ValueError, match="already being tracked"):
        db.add_modpack(12345, "Test Pack Again")


# ---------------------------------------------------------------------------
# remove_modpack
# ---------------------------------------------------------------------------


def test_remove_modpack_returns_true(db):
    db.add_modpack(12345, "Test Pack")
    assert db.remove_modpack(12345) is True


def test_remove_modpack_missing_returns_false(db):
    assert db.remove_modpack(99999) is False


def test_remove_modpack_deletes_record(db):
    db.add_modpack(12345, "Test Pack")
    db.remove_modpack(12345)
    assert db.get_modpack(12345) is None


# ---------------------------------------------------------------------------
# get_modpack
# ---------------------------------------------------------------------------


def test_get_modpack_found(db):
    db.add_modpack(111, "Pack A")
    mp = db.get_modpack(111)
    assert mp is not None
    assert mp.name == "Pack A"


def test_get_modpack_not_found(db):
    assert db.get_modpack(999) is None


# ---------------------------------------------------------------------------
# get_all_modpacks
# ---------------------------------------------------------------------------


def test_get_all_modpacks_empty(db):
    assert db.get_all_modpacks() == []


def test_get_all_modpacks_multiple(db):
    db.add_modpack(1, "Pack A")
    db.add_modpack(2, "Pack B")
    db.add_modpack(3, "Pack C")
    modpacks = db.get_all_modpacks()
    assert len(modpacks) == 3
    ids = {mp.curseforge_id for mp in modpacks}
    assert ids == {1, 2, 3}


# ---------------------------------------------------------------------------
# update_version
# ---------------------------------------------------------------------------


def test_update_version_sets_fields(db):
    db.add_modpack(12345, "Test Pack")
    db.update_version(12345, "1.2.3", notification_sent=True)
    mp = db.get_modpack(12345)
    assert mp.current_version == "1.2.3"
    assert mp.last_checked is not None


def test_update_version_nonexistent_raises(db):
    with pytest.raises(ValueError, match="not found"):
        db.update_version(99999, "1.0.0")


def test_update_version_multiple_times(db):
    db.add_modpack(12345, "Test Pack")
    db.update_version(12345, "1.0.0")
    db.update_version(12345, "1.1.0")
    mp = db.get_modpack(12345)
    assert mp.current_version == "1.1.0"


# ---------------------------------------------------------------------------
# get_check_history
# ---------------------------------------------------------------------------


def test_check_history_newest_first(db):
    db.add_modpack(12345, "Test Pack")
    db.update_version(12345, "1.0.0", notification_sent=False)
    db.update_version(12345, "1.1.0", notification_sent=True)
    history = db.get_check_history(12345, limit=10)
    assert len(history) == 2
    assert history[0].version_found == "1.1.0"
    assert history[0].notification_sent is True
    assert history[1].version_found == "1.0.0"


def test_check_history_limit(db):
    db.add_modpack(12345, "Test Pack")
    for i in range(5):
        db.update_version(12345, f"1.{i}.0")
    history = db.get_check_history(12345, limit=3)
    assert len(history) == 3


def test_check_history_missing_modpack(db):
    assert db.get_check_history(99999) == []


# ---------------------------------------------------------------------------
# toggle_notifications
# ---------------------------------------------------------------------------


def test_toggle_notifications_disable(db):
    db.add_modpack(12345, "Test Pack")
    result = db.toggle_notifications(12345, False)
    assert result is True
    mp = db.get_modpack(12345)
    assert mp.notification_enabled is False


def test_toggle_notifications_re_enable(db):
    db.add_modpack(12345, "Test Pack")
    db.toggle_notifications(12345, False)
    db.toggle_notifications(12345, True)
    assert db.get_modpack(12345).notification_enabled is True


def test_toggle_notifications_missing(db):
    assert db.toggle_notifications(99999, True) is False


# ---------------------------------------------------------------------------
# cascade delete
# ---------------------------------------------------------------------------


def test_remove_modpack_also_removes_history(db):
    db.add_modpack(12345, "Test Pack")
    db.update_version(12345, "1.0.0")
    db.update_version(12345, "1.1.0")
    db.remove_modpack(12345)
    # History should be gone (cascade delete)
    assert db.get_check_history(12345) == []
