"""Shared builders for bounded multi-customer profile tests."""

from __future__ import annotations

from datetime import date, time
from pathlib import Path

from checkin_cli.customer_admin import (
    CustomerDraft,
    commit_multi_customer_admission_migration,
    register_customer,
    set_customer_ai_consent,
)
from checkin_cli.customer_coaching import (
    CONSENT_VERSION,
    AiProcessingConsent,
)
from checkin_cli.multi_customer_admission_migration import (
    propose_admission_migration,
)
from checkin_cli.multi_customer_admission_migration_models import (
    AdmissionMigrationProposal,
)
from tests.test_customer_admin import _write_activation_readiness

TEST_CANDIDATE_DIGEST = "a" * 64


def admission_policy(limit: int) -> dict[str, int | str]:
    """Return the canonical bounded policy payload."""
    return {
        "schema": "nutricoach-multi-customer-v1",
        "candidate_digest": TEST_CANDIDATE_DIGEST,
        "max_enabled_customers": limit,
    }


def add_second_customer(profile_root: Path, registry_path: Path) -> Path:
    """Register one independently addressed, consenting customer."""
    register_customer(
        registry_path,
        CustomerDraft(
            customer_key="client_002",
            display_name="김철수",
            user_id="4",
            chat_id="-100",
            topic_id="40",
            starts_on=date(2026, 8, 1),
            daily_time=time(8, 30),
            weekly_weekday=1,
            monthly_day=2,
            calories_kcal=2200,
            protein_g=140,
            meals=("아침", "점심", "저녁"),
        ),
    )
    set_customer_ai_consent(
        registry_path,
        "client_002",
        AiProcessingConsent(
            granted=True,
            recorded_on=date(2026, 8, 1),
            notice_version=CONSENT_VERSION,
        ),
    )
    data_root = profile_root / "data" / "customers" / "client_002"
    data_root.mkdir(parents=True)
    _write_activation_readiness(profile_root, "client_002")
    return data_root


def admission_proposal(
    registry_path: Path,
    limit: int,
) -> AdmissionMigrationProposal:
    """Build one exact proposal from current registry bytes."""
    _, proposal = propose_admission_migration(
        registry_path.read_bytes(),
        candidate_digest=TEST_CANDIDATE_DIGEST,
        max_enabled_customers=limit,
    )
    return proposal


def configure_capacity(
    profile_root: Path,
    registry_path: Path,
    limit: int,
) -> None:
    """Commit one exact capacity proposal in a disposable profile."""
    proposal = admission_proposal(registry_path, limit)
    assert (
        commit_multi_customer_admission_migration(
            profile_root,
            proposal,
            proposal.approval_phrase,
        )
        == proposal.after_sha256
    )
