"""AWOOOI warning-step owner response preflight source."""

from __future__ import annotations

from collections.abc import Mapping
from typing import Any

from src.services.awoooi_onboarding_source_contracts import source_ready_payload

_SCHEMA_VERSION = "awoooi_gitea_onboarding_warning_step_owner_response_preflight_v1"
_REQUIRED_ACKS = (
    "accept_warning_only_scope",
    "confirm_no_push_or_pull_request_trigger",
    "confirm_no_generic_runner_label",
    "confirm_runner_pressure_guard_required",
)


def load_latest_awoooi_gitea_onboarding_warning_step_owner_response_preflight(
    response: Mapping[str, Any] | None = None,
) -> dict[str, Any]:
    """Validate redacted owner response fields without opening workflow gates."""
    response = response or {}
    accepted = [
        key for key in _REQUIRED_ACKS if response.get(key) is True
    ]
    ready = len(accepted) == len(_REQUIRED_ACKS)
    return source_ready_payload(
        schema_version=_SCHEMA_VERSION,
        source_id="warning_step_owner_response_preflight_service",
        status=(
            "ready_for_template_copy_plan"
            if ready
            else "blocked_owner_response_acknowledgements_missing"
        ),
        readback={
            "required_acknowledgement_count": len(_REQUIRED_ACKS),
            "accepted_acknowledgement_count": len(accepted),
            "missing_acknowledgements": [
                key for key in _REQUIRED_ACKS if key not in accepted
            ],
            "ready_for_template_copy_plan": ready,
        },
        next_actions=[
            "collect_redacted_owner_acknowledgements",
            "rerun_preflight_before_copy_execution_plan",
        ],
    )
