from ccma.domain.dates import DateValidationError, validate_member_dates
from ccma.rules.api import RuleContext, task

RULE_ID = "birthdate-check"
ORDER = 10


def evaluate(context: RuleContext):
    member = context.member
    if not member.birth_date.strip():
        return [
            task(
                rule_id=RULE_ID,
                member=member,
                key_suffix="missing",
                severity="warning",
                title=f"{member.display_name}: Geburtsdatum fehlt",
                detail="Das Geburtsdatum muss in der Mitgliederakte ergänzt werden.",
                code="missing_birth_date",
            )
        ]
    try:
        validate_member_dates(
            birth_date=member.birth_date,
            accepted_at=member.accepted_at,
            membership_started_at=member.membership_started_at,
            today=context.today,
        )
    except DateValidationError as exc:
        return [
            task(
                rule_id=RULE_ID,
                member=member,
                key_suffix="invalid",
                severity="error",
                title=f"{member.display_name}: Ungültige Datumsangabe",
                detail=str(exc),
                code="invalid_member_dates",
            )
        ]
    return []
