from __future__ import annotations

from datetime import datetime

from ccma.domain.dates import format_date_for_display
from ccma.domain.models import (
    MEMBERSHIP_STATUS_LABELS,
    PAYMENT_FREQUENCY_LABELS,
    Member,
)


def base_placeholder_values(
    member: Member,
    organization: dict | None = None,
    *,
    created_at: datetime | None = None,
) -> dict[str, str]:
    """Member and organization placeholders shared by document templates and mail
    templates, so the same `{{member.*}}`/`{{organization.*}}` names mean the same
    thing in a dunning letter and in the e-mail that carries it."""
    organization = organization if isinstance(organization, dict) else {}
    timestamp = created_at or datetime.now().astimezone()
    organization_address = " ".join(
        part
        for part in (
            str(organization.get("street", "")),
            str(organization.get("postal_code", "")),
            str(organization.get("city", "")),
        )
        if part
    )
    return {
        "current_date": format_date_for_display(timestamp.date().isoformat()),
        "current_datetime": timestamp.strftime("%d.%m.%Y %H:%M"),
        "member.id": member.member_id,
        "member.number": member.member_number,
        "member.first_name": member.first_name,
        "member.last_name": member.last_name,
        "member.nickname": member.nickname,
        "member.full_name": member.display_name,
        "member.email": member.email,
        "member.phone": member.phone,
        "member.birth_date": format_date_for_display(member.birth_date),
        "member.status": MEMBERSHIP_STATUS_LABELS.get(member.status, member.status),
        "member.accepted_at": format_date_for_display(member.accepted_at),
        "member.started_at": format_date_for_display(member.membership_started_at),
        "member.ended_at": format_date_for_display(member.membership_ended_at),
        "member.application_date": format_date_for_display(member.application_date),
        "member.payment_frequency": PAYMENT_FREQUENCY_LABELS.get(
            member.payment_frequency, member.payment_frequency
        ).capitalize(),
        "member.street": member.street,
        "member.address_addition": member.address_addition,
        "member.postal_code": member.postal_code,
        "member.city": member.city,
        "member.country": member.country,
        "member.address_line": " ".join(
            part for part in (member.street, member.postal_code, member.city) if part
        ),
        "member.account_holder": member.account_holder,
        "member.iban": member.iban,
        "member.bic": member.bic,
        "member.mandate_reference": member.mandate_reference,
        "member.mandate_signed_at": format_date_for_display(member.mandate_signed_at),
        "member.mandate_revoked_at": format_date_for_display(member.mandate_revoked_at),
        "member.mandate_active": "Ja" if member.mandate_active else "Nein",
        "organization.name": str(organization.get("name", "")),
        "organization.street": str(organization.get("street", "")),
        "organization.postal_code": str(organization.get("postal_code", "")),
        "organization.city": str(organization.get("city", "")),
        "organization.country": str(organization.get("country", "")),
        "organization.address_line": organization_address,
        "organization.email": str(organization.get("email", "")),
        "organization.phone": str(organization.get("phone", "")),
        "organization.website": str(organization.get("website", "")),
        "organization.iban": str(organization.get("iban", "")),
        "organization.bic": str(organization.get("bic", "")),
        "organization.creditor_id": str(organization.get("creditor_id", "")),
    }
