from __future__ import annotations

from collections.abc import Mapping
from dataclasses import dataclass
from datetime import date
from typing import Any, Literal

from ccma.domain.models import ContributionData, Member


@dataclass(frozen=True, slots=True)
class RuleContext:
    member: Member
    contributions: ContributionData
    today: date
    settings: Any
    repository_config: Mapping[str, Any]


@dataclass(frozen=True, slots=True)
class RuleAction:
    key: str
    action: Literal["task", "create_claim"]
    member_id: str
    payload: Mapping[str, Any]


def task(
    *,
    rule_id: str,
    member: Member,
    key_suffix: str,
    severity: str,
    title: str,
    detail: str,
    due_date: date | None = None,
    code: str | None = None,
) -> RuleAction:
    payload: dict[str, Any] = {
        "severity": severity,
        "title": title,
        "detail": detail,
        "code": code or rule_id,
    }
    if due_date:
        payload["due_date"] = due_date.isoformat()
    return RuleAction(
        key=f"{rule_id}:{member.member_id}:{key_suffix}",
        action="task",
        member_id=member.member_id,
        payload=payload,
    )


def create_claim(
    *,
    rule_id: str,
    member: Member,
    claim_key: str,
    payload: Mapping[str, Any],
) -> RuleAction:
    return RuleAction(
        key=f"{rule_id}:{member.member_id}:{claim_key}",
        action="create_claim",
        member_id=member.member_id,
        payload={"claim_key": claim_key, **payload},
    )
