# #region Test.DatasetReview.HelpersEdge [C:2] [TYPE Module] [SEMANTICS test,dataset,helpers,execution,snapshot,edge]
# @BRIEF Edge-case coverage for orchestrator_pkg/_helpers.py uncovered lines.

from pathlib import Path
import sys

sys.path.insert(0, str(Path(__file__).parent.parent.parent / "src"))

from unittest.mock import MagicMock, patch
import pytest


class TestBuildExecutionSnapshotEdge:
    """Edge cases for build_execution_snapshot."""

    def test_mapping_missing_template_variable(self):
        """Execution mapping with no matching template variable adds blocker (lines 172-173)."""
        from src.services.dataset_review.orchestrator_pkg._helpers import build_execution_snapshot
        from src.models.dataset_review import ApprovalState

        session = MagicMock()
        session.imported_filters = []
        session.execution_mappings = []
        session.template_variables = []
        session.semantic_fields = []
        session.dataset_id = 42

        # Need a mapping that references a template variable that doesn't exist
        mapping = MagicMock()
        mapping.filter_id = "fl-1"
        mapping.variable_id = "tv-nonexistent"
        mapping.mapping_id = "m-1"
        mapping.effective_value = "SomeValue"
        mapping.approval_state = ApprovalState.PENDING
        mapping.requires_explicit_approval = False
        mapping.raw_input_value = None

        imported_filter = MagicMock()
        imported_filter.filter_id = "fl-1"
        imported_filter.filter_name = "region"
        imported_filter.normalized_value = "US"
        imported_filter.raw_value = "us"

        session.imported_filters = [imported_filter]
        session.execution_mappings = [mapping]
        session.template_variables = []  # no matching variable

        snapshot = build_execution_snapshot(session)
        assert "m-1:missing_variable" in str(snapshot["preview_blockers"])

    def test_effective_value_falls_back_to_template_default(self):
        """When effective_value is None, fall back to template default (line 181)."""
        from src.services.dataset_review.orchestrator_pkg._helpers import build_execution_snapshot
        from src.models.dataset_review import ApprovalState

        session = MagicMock()
        session.dataset_id = 42

        mapping = MagicMock()
        mapping.filter_id = "fl-1"
        mapping.variable_id = "tv-1"
        mapping.mapping_id = "m-1"
        mapping.effective_value = None
        mapping.approval_state = ApprovalState.PENDING
        mapping.requires_explicit_approval = False
        mapping.raw_input_value = None

        imported_filter = MagicMock()
        imported_filter.filter_id = "fl-1"
        imported_filter.filter_name = "region"
        imported_filter.normalized_value = None
        imported_filter.raw_value = None

        template_variable = MagicMock()
        template_variable.variable_id = "tv-1"
        template_variable.variable_name = "region"
        template_variable.default_value = "default_region"
        template_variable.is_required = True

        session.imported_filters = [imported_filter]
        session.execution_mappings = [mapping]
        session.template_variables = [template_variable]
        session.semantic_fields = []

        snapshot = build_execution_snapshot(session)
        assert snapshot["template_params"].get("region") == "default_region"

    def test_missing_required_value_blocker(self):
        """Required template variable without effective value adds blocker (lines 184-187)."""
        from src.services.dataset_review.orchestrator_pkg._helpers import build_execution_snapshot
        from src.models.dataset_review import ApprovalState

        session = MagicMock()
        session.dataset_id = 42

        mapping = MagicMock()
        mapping.filter_id = "fl-1"
        mapping.variable_id = "tv-1"
        mapping.mapping_id = "m-1"
        mapping.effective_value = None
        mapping.approval_state = ApprovalState.PENDING
        mapping.requires_explicit_approval = False
        mapping.raw_input_value = None

        imported_filter = MagicMock()
        imported_filter.filter_id = "fl-1"
        imported_filter.filter_name = "region"
        imported_filter.normalized_value = None
        imported_filter.raw_value = None

        template_variable = MagicMock()
        template_variable.variable_id = "tv-1"
        template_variable.variable_name = "region"
        template_variable.default_value = None
        template_variable.is_required = True  # required but no value

        session.imported_filters = [imported_filter]
        session.execution_mappings = [mapping]
        session.template_variables = [template_variable]
        session.semantic_fields = []

        snapshot = build_execution_snapshot(session)
        blockers = snapshot["preview_blockers"]
        assert any("missing_required_value" in str(b) for b in blockers)

    def test_unmapped_filter_without_effective_value_skipped(self):
        """Unmapped filter without effective value is skipped (line 222)."""
        from src.services.dataset_review.orchestrator_pkg._helpers import build_execution_snapshot

        session = MagicMock()
        session.dataset_id = 42

        # Filter with no effective value and no mapping
        imported_filter = MagicMock()
        imported_filter.filter_id = "fl-1"
        imported_filter.filter_name = "region"
        imported_filter.normalized_value = None
        imported_filter.raw_value = None

        session.imported_filters = [imported_filter]
        session.execution_mappings = []
        session.template_variables = []
        session.semantic_fields = []

        snapshot = build_execution_snapshot(session)
        assert len(snapshot["effective_filters"]) == 0

    def test_unmapped_required_variable_blocker(self):
        """Unmapped required template variable adds blocker (lines 241-242)."""
        from src.services.dataset_review.orchestrator_pkg._helpers import build_execution_snapshot

        session = MagicMock()
        session.dataset_id = 42

        template_variable = MagicMock()
        template_variable.variable_id = "tv-1"
        template_variable.variable_name = "region"
        template_variable.default_value = None
        template_variable.is_required = True

        session.imported_filters = []
        session.execution_mappings = []
        session.template_variables = [template_variable]
        session.semantic_fields = []

        snapshot = build_execution_snapshot(session)
        blockers = snapshot["preview_blockers"]
        assert any("unmapped" in str(b) for b in blockers)
# #endregion Test.DatasetReview.HelpersEdge
