# #region Test.DatasetReview.OrchestratorHelpers.Coverage [C:3] [TYPE Module] [SEMANTICS test,dataset,orchestrator,helpers,snapshot,coverage,edge]
# @BRIEF Edge case tests for orchestrator_pkg/_helpers.py uncovered lines.
#   Covers lines 172-173, 181, 184-187, 222, 241-242 in build_execution_snapshot.
# @RELATION BINDS_TO -> [OrchestratorHelpers]

from __future__ import annotations
from pathlib import Path
import sys

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

from unittest.mock import MagicMock
import pytest


class TestBuildExecutionSnapshotCoverage:
    """Targeted edge coverage for build_execution_snapshot."""

    # Line 172-173: template_variable is None for a valid mapping
    def test_mapping_missing_variable_adds_blocker(self):
        """imported_filter exists but template_variable is None → blocker and continue (lines 172-173)."""
        from src.services.dataset_review.orchestrator_pkg._helpers import build_execution_snapshot
        from src.models.dataset_review import ImportedFilter, TemplateVariable, ExecutionMapping

        session = MagicMock()
        filt = MagicMock(spec=ImportedFilter)
        filt.filter_id = "filt-1"
        filt.filter_name = "region"
        filt.raw_value = "us"
        filt.normalized_value = "US"
        filt.display_name = "Region"
        session.imported_filters = [filt]

        tv = MagicMock(spec=TemplateVariable)
        tv.variable_id = "tv-1"
        tv.variable_name = "region"
        session.template_variables = [tv]

        mapping = MagicMock(spec=ExecutionMapping)
        mapping.mapping_id = "map-1"
        mapping.filter_id = "filt-1"
        mapping.variable_id = "tv-nonexistent"  # no template variable with this ID
        mapping.effective_value = "US"
        mapping.raw_input_value = "us"
        mapping.approval_state = MagicMock(value="approved")
        mapping.requires_explicit_approval = False
        session.execution_mappings = [mapping]
        session.semantic_fields = []
        session.dataset_id = 42

        snapshot = build_execution_snapshot(session)
        assert any("missing_variable" in b for b in snapshot["preview_blockers"])

    # Line 181: effective_value is None after extract_effective_filter_value, falls to default_value
    def test_effective_value_falls_to_default_value(self):
        """effective_value is None → falls back to template_variable.default_value (line 181)."""
        from src.services.dataset_review.orchestrator_pkg._helpers import build_execution_snapshot
        from src.models.dataset_review import ImportedFilter, TemplateVariable, ExecutionMapping

        session = MagicMock()
        filt = MagicMock(spec=ImportedFilter)
        filt.filter_id = "filt-1"
        filt.filter_name = "region"
        filt.raw_value = None
        filt.normalized_value = None  # extract_effective_filter_value returns None
        filt.display_name = "Region"
        session.imported_filters = [filt]

        tv = MagicMock(spec=TemplateVariable)
        tv.variable_id = "tv-1"
        tv.variable_name = "region"
        tv.is_required = False
        tv.default_value = "default-us"  # fallback value
        session.template_variables = [tv]

        mapping = MagicMock(spec=ExecutionMapping)
        mapping.mapping_id = "map-1"
        mapping.filter_id = "filt-1"
        mapping.variable_id = "tv-1"
        mapping.effective_value = None  # triggers fallback chain
        mapping.raw_input_value = None
        mapping.approval_state = MagicMock(value="approved")
        mapping.requires_explicit_approval = False
        session.execution_mappings = [mapping]
        session.semantic_fields = []
        session.dataset_id = 42

        snapshot = build_execution_snapshot(session)
        assert snapshot["template_params"]["region"] == "default-us"

    # Lines 184-187: effective_value is None AND is_required → blocker
    def test_missing_required_value_adds_blocker(self):
        """effective_value is None and variable is required → blocker (lines 184-187)."""
        from src.services.dataset_review.orchestrator_pkg._helpers import build_execution_snapshot
        from src.models.dataset_review import ImportedFilter, TemplateVariable, ExecutionMapping

        session = MagicMock()
        filt = MagicMock(spec=ImportedFilter)
        filt.filter_id = "filt-1"
        filt.filter_name = "region"
        filt.raw_value = None
        filt.normalized_value = None
        filt.display_name = "Region"
        session.imported_filters = [filt]

        tv = MagicMock(spec=TemplateVariable)
        tv.variable_id = "tv-1"
        tv.variable_name = "region"
        tv.is_required = True
        tv.default_value = None  # no default
        session.template_variables = [tv]

        mapping = MagicMock(spec=ExecutionMapping)
        mapping.mapping_id = "map-1"
        mapping.filter_id = "filt-1"
        mapping.variable_id = "tv-1"
        mapping.effective_value = None  # no effective value at all
        mapping.raw_input_value = None
        mapping.approval_state = MagicMock(value="approved")
        mapping.requires_explicit_approval = False
        session.execution_mappings = [mapping]
        session.semantic_fields = []
        session.dataset_id = 42

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

    # Line 222: continue when unmapped filter has no effective_value
    def test_unmapped_filter_no_effective_value_skips(self):
        """Unmapped imported_filter with None effective_value → continue (line 222)."""
        from src.services.dataset_review.orchestrator_pkg._helpers import build_execution_snapshot
        from src.models.dataset_review import ImportedFilter, TemplateVariable

        session = MagicMock()
        filt = MagicMock(spec=ImportedFilter)
        filt.filter_id = "filt-1"
        filt.filter_name = "region"
        filt.raw_value = None
        filt.normalized_value = None  # no effective value
        filt.display_name = "Region"
        session.imported_filters = [filt]

        tv = MagicMock(spec=TemplateVariable)
        tv.variable_id = "tv-1"
        tv.variable_name = "region"
        tv.is_required = False
        tv.default_value = None
        session.template_variables = [tv]

        # No mappings → unmapped filter
        session.execution_mappings = []
        session.semantic_fields = []
        session.dataset_id = 42

        snapshot = build_execution_snapshot(session)
        # Filter has no effective_value and no mapping → skipped at line 222
        effective_filter_names = [f.get("filter_name") for f in snapshot["effective_filters"]]
        assert "region" not in effective_filter_names

    # Lines 241-242: unmapped template variable with default_value
    def test_unmapped_variable_with_default_value(self):
        """Unmapped template variable with default_value → added to template_params (lines 241-242)."""
        from src.services.dataset_review.orchestrator_pkg._helpers import build_execution_snapshot
        from src.models.dataset_review import ImportedFilter, TemplateVariable

        session = MagicMock()
        # No imported filters needed for this test
        session.imported_filters = []

        tv = MagicMock(spec=TemplateVariable)
        tv.variable_id = "tv-1"
        tv.variable_name = "region"
        tv.is_required = False
        tv.default_value = "default-us"  # has default
        session.template_variables = [tv]

        session.execution_mappings = []  # no mappings, so variable is unmapped
        session.semantic_fields = []
        session.dataset_id = 42

        snapshot = build_execution_snapshot(session)
        assert snapshot["template_params"]["region"] == "default-us"
# #endregion Test.DatasetReview.OrchestratorHelpers.Coverage
