#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.12"
# dependencies = ["pydantic"]
# ///

"""In-memory rendered-provider-text privacy qualification for Todo10."""

from __future__ import annotations

from typing import ClassVar, Final

from pydantic import BaseModel, ConfigDict

REMINDER_TEXT: Final = "체크인이 확인되지 않았습니다. 오늘 아침 체크인을 제출해 주세요."
TOPIC59_LABELS: Final = (
    "[운영 현황]", "고객 식별자: ", "KST 날짜: ", "제출 상태: ",
    "완료 필드 수: ", "초안 상태: ", "승인 상태: ", "발송 상태: ",
    "이벤트 ID: ", "상태 행 ID: ",
)
SENTINELS: Final = {
    "raw_name": "PRIVACY_RAW_NAME_SENTINEL",
    "nutrition": "PRIVACY_NUTRITION_SENTINEL",
    "health": "PRIVACY_HEALTH_SENTINEL",
    "free_text": "PRIVACY_FREE_TEXT_SENTINEL",
    "token": "network-disabled",
    "credential": "123456:GoldenPathNoNetworkToken",
}
SCHEMAS: Final = {
    "topic59_send": "topic59-day-card-v1",
    "topic59_edit": "topic59-day-card-v1",
    "reminder": "weekly-reminder-static-v1",
    "owner_dm": "owner-weekly-aggregate-v1",
    "generic_schedule": "customer-schedule-v1",
}


class PrivacyScan(BaseModel):
    model_config: ClassVar[ConfigDict] = ConfigDict(frozen=True, extra="forbid")

    schema_name: str
    scanned_categories: int
    leak_categories: tuple[str, ...]

    @property
    def leak_count(self) -> int:
        return len(self.leak_categories)


def scan_provider_text(kind: str, text: str) -> PrivacyScan:
    """Inspect text while resident in memory; return no rendered content."""
    leaks = [category for category, sentinel in SENTINELS.items() if sentinel in text]
    schema = SCHEMAS.get(kind)
    if schema is None:
        leaks.append("schema")
        schema = "unrecognized"
    elif kind.startswith("topic59"):
        lines = text.splitlines()
        if len(lines) != len(TOPIC59_LABELS) or any(
            not line.startswith(label) for line, label in zip(lines, TOPIC59_LABELS, strict=True)
        ):
            leaks.append("schema")
    elif kind == "reminder" and text != REMINDER_TEXT:
        leaks.append("schema")
    return PrivacyScan(
        schema_name=schema, scanned_categories=len(SENTINELS) + 1,
        leak_categories=tuple(sorted(set(leaks))),
    )