"""Typed immutable config builders for weekly dispatcher tests."""

from __future__ import annotations

from dataclasses import dataclass
from datetime import datetime

from gateway.config import PlatformConfig
from gateway.platforms.nutrition_weekly_operations_authority import (
    WeeklyOperationsAuthorityReceipt,
)
from gateway.platforms.nutrition_weekly_operations_config import JsonValue
from tests.gateway._nutrition_weekly_reminder_support import ReminderOwnerFixture


@dataclass(frozen=True, slots=True)
class WeeklyConfigState:
    enabled: bool = True
    reminder_time: str = "20:00:00"
    owner_chat_id: str = "owner-dm"
    review_topic_id: int = 59


def _receipt_document(
    receipt: WeeklyOperationsAuthorityReceipt, owner_chat_id: str
) -> dict[str, JsonValue]:
    identity = receipt.registry_identity
    return {
        "schema": "nutricoach-weekly-operations-authority-v2",
        "candidate_digest": receipt.candidate_digest,
        "config_digest": receipt.config_digest,
        "enabled_customer_keys": list(receipt.enabled_customer_keys),
        "owner": {
            "user_id": receipt.owner.user_id,
            "chat_id": owner_chat_id,
            "version": receipt.owner.version,
        },
        "consent_digest": receipt.consent_digest,
        "registry_identity": {
            "schema_version": identity.schema_version,
            "relative_name": identity.relative_name,
            "device": identity.device,
            "inode": identity.inode,
            "mode": identity.mode,
            "owner": identity.owner,
            "links": identity.links,
            "content_digest": identity.content_digest,
            "binding_digest": identity.binding_digest,
        },
        "issued_at": receipt.issued_at.isoformat(),
        "expires_at": receipt.expires_at.isoformat(),
        "feature_epoch": receipt.feature_epoch,
    }


def weekly_platform_config(
    fixture: ReminderOwnerFixture, state: WeeklyConfigState | None = None,
) -> PlatformConfig:
    current = state or WeeklyConfigState()
    snapshot = fixture.owner.tick_snapshot(
        fixture.runtime.spec.customer_key,
        datetime.fromisoformat("2026-08-17T20:00:00+09:00"),
    )
    extra: dict[str, JsonValue] = {
        "nutrition_coaching": {
            "operator_review": {
                "user_id": "owner",
                "chat_id": "review",
                "topic_id": current.review_topic_id,
            },
            "weekly_operations": {
                "enabled": current.enabled,
                "reminder_time": current.reminder_time,
                "missed_cutoff_time": "23:00:00",
                "weekly_weekday": 0,
                "feature_epoch": "weekly-operations-v1",
                "registry_identity_binding_digest": (
                    snapshot.receipt.registry_identity.binding_digest
                ),
            },
            "weekly_operations_authority": _receipt_document(
                snapshot.receipt, current.owner_chat_id
            ),
            "weekly_operations_authority_source": "operator",
        }
    }
    return PlatformConfig(enabled=True, token="test", extra=extra)
