"""eProtons Engineering Lab: regression checks for the example renewal rule.

Example material supplied by eProtons for the code walkthrough "What could break if we change this rule?".
The accounts are fictional. Run with:  python -m pytest renewal_regression_checks.py

The proposed change stops treating accounts in grace status as eligible for renewal (line 6 of renewal.py).
These checks protect the behavior that must not change and state the behavior that is expected to change.
"""
from collections import namedtuple
from datetime import date, timedelta

import pytest

Account = namedtuple("Account", "account_id status paid_through manual_override")
TODAY = date(2026, 11, 23)


def is_eligible_for_renewal(account, today):
    """Current rule, as in renewal.py lines 4 to 11."""
    window_start = today - timedelta(days=30)
    if account.status in ("active", "grace"):
        if account.paid_through >= window_start:
            return True
    if account.status == "suspended" and account.manual_override:
        return True
    return False


def is_eligible_for_renewal_proposed(account, today):
    """Proposed rule: line 6 no longer accepts the grace status."""
    window_start = today - timedelta(days=30)
    if account.status == "active":
        if account.paid_through >= window_start:
            return True
    if account.status == "suspended" and account.manual_override:
        return True
    return False


ACCOUNTS = [
    Account("ACC-1001", "active", date(2026, 11, 10), False),
    Account("ACC-1002", "active", date(2026, 10, 1), False),
    Account("ACC-1003", "grace", date(2026, 11, 5), False),
    Account("ACC-1004", "grace", date(2026, 10, 24), False),
    Account("ACC-1005", "grace", date(2026, 9, 15), False),
    Account("ACC-1006", "suspended", date(2026, 8, 30), True),
    Account("ACC-1007", "suspended", date(2026, 11, 1), False),
    Account("ACC-1008", "active", date(2026, 10, 24), False),
    Account("ACC-1009", "closed", date(2026, 11, 20), False),
]
CURRENTLY_ELIGIBLE = {"ACC-1001", "ACC-1003", "ACC-1004", "ACC-1006", "ACC-1008"}
EXPECTED_TO_LOSE_ELIGIBILITY = {"ACC-1003", "ACC-1004"}


@pytest.mark.parametrize("account", ACCOUNTS, ids=lambda a: a.account_id)
def test_current_behavior_is_characterized(account):
    """Characterization: record what the current rule returns for every example account."""
    assert is_eligible_for_renewal(account, TODAY) == (account.account_id in CURRENTLY_ELIGIBLE)


@pytest.mark.parametrize(
    "account", [a for a in ACCOUNTS if a.account_id not in EXPECTED_TO_LOSE_ELIGIBILITY], ids=lambda a: a.account_id
)
def test_unaffected_accounts_keep_their_result(account):
    """Regression: every account outside the agreed change keeps its current result."""
    assert is_eligible_for_renewal_proposed(account, TODAY) == is_eligible_for_renewal(account, TODAY)


@pytest.mark.parametrize(
    "account", [a for a in ACCOUNTS if a.account_id in EXPECTED_TO_LOSE_ELIGIBILITY], ids=lambda a: a.account_id
)
def test_grace_accounts_paid_within_window_lose_eligibility(account):
    """Agreed change: grace accounts paid within the 30-day window are no longer eligible."""
    assert is_eligible_for_renewal(account, TODAY) is True
    assert is_eligible_for_renewal_proposed(account, TODAY) is False


def test_payment_exactly_thirty_days_ago_remains_eligible_for_active_accounts():
    """Boundary: the window includes the thirtieth day (renewal.py line 7 uses >=)."""
    account = Account("BOUNDARY", "active", TODAY - timedelta(days=30), False)
    assert is_eligible_for_renewal_proposed(account, TODAY) is True


def test_manual_override_for_suspended_accounts_is_unchanged():
    """The override path at renewal.py lines 9 and 10 is outside the change."""
    account = Account("OVERRIDE", "suspended", date(2025, 1, 1), True)
    assert is_eligible_for_renewal_proposed(account, TODAY) is True
