"""Public service contracts for nutrition onboarding."""

from __future__ import annotations

import hashlib
import json

from pydantic import BaseModel, ConfigDict

from checkin_cli.nutrition_onboarding_models import OnboardingState


QUESTION_FIELDS = (
    "date_of_birth",
    "equation_sex_basis",
    "height_cm",
    "weight_kg",
    "activity_category",
    "activity_rationale",
    "goal_type",
    "target_weight_kg",
    "target_date",
    "allergies",
    "intolerances",
    "religious_ethical_exclusions",
    "disliked_foods",
    "dietary_preferences",
    "conditions",
    "medications",
    "pregnancy_breastfeeding",
    "eating_disorder_risk",
    "cooking_access",
    "budget_band",
    "meal_count",
    "schedule_constraints",
)


class DomainModel(BaseModel):
    model_config = ConfigDict(extra="forbid", frozen=True)


class OnboardingAuthority(DomainModel):
    customer_key: str
    customer_user_id: int
    customer_chat_id: int
    customer_topic_id: int
    owner_user_id: int
    owner_chat_id: int
    owner_topic_id: int
    consent_notice_version: str
    consent_granted: bool
    customer_enabled: bool


class MessageEvidence(DomainModel):
    actor_user_id: int
    chat_id: int
    topic_id: int
    message_id: int
    update_id: int


class OnboardingStatus(DomainModel):
    customer_key: str
    state: OnboardingState
    answer_count: int
    next_field: str | None
    baseline_digest: str | None = None
    owner_reviewed: bool = False


def canonical_digest(value: object) -> str:
    return hashlib.sha256(
        json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")).encode()
    ).hexdigest()


def example_answer(field: str) -> object:
    examples: dict[str, object] = {
        "date_of_birth": "1996-08-01",
        "equation_sex_basis": "male",
        "height_cm": "180",
        "weight_kg": "80",
        "activity_category": "moderate",
        "activity_rationale": "주 3회 근력 운동",
        "goal_type": "maintain",
        "target_weight_kg": None,
        "target_date": None,
        "pregnancy_breastfeeding": False,
        "eating_disorder_risk": False,
        "cooking_access": "기본 조리 가능",
        "budget_band": "보통",
        "meal_count": 3,
        "schedule_constraints": "없음",
    }
    structured = {
        "allergies",
        "intolerances",
        "religious_ethical_exclusions",
        "disliked_foods",
        "dietary_preferences",
        "conditions",
        "medications",
    }
    if field in structured:
        return {"status": "none", "items": []}
    if field not in examples:
        raise ValueError(f"unknown question field: {field}")
    return examples[field]
