from __future__ import annotations

import json
from datetime import date, datetime
from pathlib import Path

from checkin_cli.weekly_operations_summary import build_weekly_operations_summary
from checkin_cli.weekly_operations import CustomerKey
from tests._weekly_operations_summary_support import (
    KST,
    approved_synthetic_week,
)


def test_weekly_operations_summary_uses_all_seven_calendar_days(
    tmp_path: Path,
) -> None:
    # Given: one registered canonical authority and a confirmed two-week sidecar.
    fixture = approved_synthetic_week(tmp_path)
    try:
        # When: the Monday-to-Sunday aggregate is built for the approved synthetic week.
        summary = build_weekly_operations_summary(
            fixture.canonical.source,
            fixture.sidecar.store,
            date(2026, 8, 17),
        )

        # Then: it reports only the approved aggregate values and comparison.
        assert summary.starts_on == date(2026, 8, 17)
        assert summary.ends_on == date(2026, 8, 23)
        assert summary.window_start_kst == datetime(2026, 8, 17, 0, 0, tzinfo=KST)
        assert summary.window_end_kst == datetime(2026, 8, 23, 23, 59, 59, tzinfo=KST)
        assert summary.submitted_count == 4
        assert summary.late_count == 1
        assert summary.missed_count == 2
        assert (summary.completed_days, summary.calendar_days) == (5, 7)
        assert summary.adherence_percent == 71.43
        assert summary.weight_trend.value == "increasing"
        assert summary.reminder_sent_count == 4
        assert summary.reminder_incident_count == 0
        assert summary.prior_week_comparison is not None
        assert summary.prior_week_comparison.completed_days_delta == 2
        assert summary.prior_week_comparison.adherence_percent_delta == 28.57
        serialized = summary.to_json()
        assert json.loads(serialized)["completed_days"] == 5
        assert "RAW-PAYLOAD-PII-SENTINEL" not in serialized
        assert "81.555" not in serialized
        assert CustomerKey("pilot_customer_001") not in serialized
        assert "RAW-PAYLOAD-PII-SENTINEL" not in repr(summary)
    finally:
        fixture.close()
