# retoor <retoor@molodetz.nl>

from datetime import datetime, timezone
from types import SimpleNamespace
from tests.conftest import BASE_URL
from devplacepy.database import get_table, set_setting
from devplacepy.utils import generate_uid
from devplacepy.services.devii.config import (
    effective_daily_limit,
    FIELD_USER_DAILY_USD,
    FIELD_GUEST_DAILY_USD,
    FIELD_ADMIN_DAILY_USD,
)
from devplacepy.services.devii.store import UsageLedger
LEDGER = "devii_usage_ledger"
def _now_iso_devii_quota():
    return datetime.now(timezone.utc).isoformat()
def _seed_ledger(owner_kind, owner_id, cost, n=1):
    table = get_table(LEDGER)
    for _ in range(n):
        table.insert(
            {
                "owner_kind": owner_kind,
                "owner_id": owner_id,
                "created_at": _now_iso_devii_quota(),
                "cost_usd": cost,
            }
        )
def _seed_gateway_spend(owner_id, cost, n=1, owner_kind="user"):
    table = get_table("gateway_usage_ledger")
    for _ in range(n):
        table.insert(
            {
                "owner_kind": owner_kind,
                "owner_id": owner_id,
                "created_at": _now_iso_devii_quota(),
                "success": 1,
                "cost_usd": cost,
            }
        )
def _ensure_devii():
    from devplacepy.services.manager import service_manager
    from devplacepy.services.devii import DeviiService

    if service_manager.get_service("devii") is None:
        service_manager.register(DeviiService())
    return service_manager.get_service("devii")
def _make_user_devii_quota(role="Member"):
    uid = generate_uid()
    username = f"quota_{uid[-12:]}"
    get_table("users").insert(
        {
            "uid": uid,
            "username": username,
            "terms_version": "1",
            "email": f"{username}@t.dev",
            "api_key": generate_uid(),
            "role": role,
            "is_active": True,
        }
    )
    return uid, username


def test_effective_limit_resolver():
    cfg = {
        FIELD_USER_DAILY_USD: 1.0,
        FIELD_GUEST_DAILY_USD: 0.05,
        FIELD_ADMIN_DAILY_USD: 0.0,
    }
    assert effective_daily_limit(cfg, "user", False) == 1.0
    assert effective_daily_limit(cfg, "guest", False) == 0.05
    assert effective_daily_limit(cfg, "user", True) == 0.0
    cfg[FIELD_ADMIN_DAILY_USD] = 5.0
    assert effective_daily_limit(cfg, "user", True) == 5.0


def test_admin_exempt_by_default(local_db):
    from devplacepy.routers.profile import _ai_quota

    _ensure_devii()
    set_setting("devii_user_daily_usd", "1.0")
    set_setting("devii_admin_daily_usd", "0.0")
    try:
        uid, _ = _make_user_devii_quota()
        _seed_gateway_spend(uid, 5.0)
        member_view = _ai_quota(uid, is_admin=False)
        admin_view = _ai_quota(uid, is_admin=True)
        assert member_view["used_pct"] == 100.0
        assert member_view["unlimited"] is False
        assert admin_view["used_pct"] == 0.0
        assert admin_view["unlimited"] is True
    finally:
        set_setting("devii_user_daily_usd", "1.0")
        set_setting("devii_admin_daily_usd", "0.0")


def test_admin_limit_is_zero_by_default(local_db):
    svc = _ensure_devii()
    set_setting("devii_admin_daily_usd", "0.0")
    try:
        assert svc.daily_limit_for("user", True) == 0.0
        assert svc.daily_limit_for("user", False) == 1.0
    finally:
        set_setting("devii_admin_daily_usd", "0.0")


def test_admin_cap_configurable(local_db):
    from devplacepy.routers.profile import _ai_quota

    _ensure_devii()
    set_setting("devii_admin_daily_usd", "2.0")
    try:
        uid, _ = _make_user_devii_quota()
        _seed_gateway_spend(uid, 1.0)
        quota = _ai_quota(uid, is_admin=True)
        assert quota["used_pct"] == 50.0
    finally:
        set_setting("devii_admin_daily_usd", "0.0")


def test_guest_cap_configurable(local_db):
    svc = _ensure_devii()
    set_setting("devii_guest_daily_usd", "0.10")
    try:
        assert svc.daily_limit_for("guest") == 0.10
    finally:
        set_setting("devii_guest_daily_usd", "0.05")


def test_reset_single_owner(local_db):
    ledger = UsageLedger()
    uid, _ = _make_user_devii_quota()
    _seed_ledger("user", uid, 0.5, n=3)
    assert ledger.spent_24h("user", uid) == 1.5
    removed = ledger.reset("user", uid)
    assert removed == 3
    assert ledger.spent_24h("user", uid) == 0.0


def test_reset_guests_only_leaves_users(local_db):
    ledger = UsageLedger()
    guest_id = f"guest-{generate_uid()[:8]}"
    user_id, _ = _make_user_devii_quota()
    _seed_ledger("guest", guest_id, 0.02, n=2)
    _seed_ledger("user", user_id, 0.5, n=1)
    ledger.reset_owner_kind("guest")
    assert ledger.spent_24h("guest", guest_id) == 0.0
    assert ledger.spent_24h("user", user_id) == 0.5


def test_reset_all(local_db):
    ledger = UsageLedger()
    guest_id = f"guest-{generate_uid()[:8]}"
    user_id, _ = _make_user_devii_quota()
    _seed_ledger("guest", guest_id, 0.02, n=2)
    _seed_ledger("user", user_id, 0.5, n=2)
    ledger.reset_all()
    assert ledger.spent_24h("guest", guest_id) == 0.0
    assert ledger.spent_24h("user", user_id) == 0.0


def test_cli_reset_quota_for_user(local_db):
    from devplacepy.cli import cmd_devii_reset_quota

    uid, username = _make_user_devii_quota()
    _seed_ledger("user", uid, 0.5, n=2)
    cmd_devii_reset_quota(SimpleNamespace(username=username, guests=False, all=False))
    assert get_table(LEDGER).count(owner_kind="user", owner_id=uid) == 0


def test_cli_reset_quota_guests(local_db):
    from devplacepy.cli import cmd_devii_reset_quota

    guest_id = f"guest-{generate_uid()[:8]}"
    user_id, _ = _make_user_devii_quota()
    _seed_ledger("guest", guest_id, 0.02, n=2)
    _seed_ledger("user", user_id, 0.5, n=1)
    cmd_devii_reset_quota(SimpleNamespace(username=None, guests=True, all=False))
    assert get_table(LEDGER).count(owner_kind="guest", owner_id=guest_id) == 0
    assert get_table(LEDGER).count(owner_kind="user", owner_id=user_id) == 1


def test_cli_reset_quota_all(local_db):
    from devplacepy.cli import cmd_devii_reset_quota

    guest_id = f"guest-{generate_uid()[:8]}"
    user_id, _ = _make_user_devii_quota()
    _seed_ledger("guest", guest_id, 0.02, n=1)
    _seed_ledger("user", user_id, 0.5, n=1)
    cmd_devii_reset_quota(SimpleNamespace(username=None, guests=False, all=True))
    assert get_table(LEDGER).count() == 0
