# region test_report_normalizer [TYPE Module]
# @SEMANTICS: tests, reports, normalizer, fallback
# @PURPOSE: Validate unknown task type fallback and partial payload normalization behavior.
# @RELATION BINDS_TO ->[EXT:frontend:normalize_report]
# @LAYER Domain
# @INVARIANT  Unknown plugin types are mapped to canonical unknown task type.

from datetime import UTC, datetime

from src.core.task_manager.models import Task, TaskStatus
from src.services.reports.normalizer import normalize_task_report


# region test_unknown_type_maps_to_unknown_profile [TYPE Function]
# @RELATION  BINDS_TO -> test_report_normalizer
# @PURPOSE: Ensure unknown plugin IDs map to unknown profile with populated summary and error context.
def test_unknown_type_maps_to_unknown_profile():
    task = Task(
        id="unknown-1",
        plugin_id="custom-unmapped-plugin",
        status=TaskStatus.FAILED,
        started_at=datetime.now(UTC),
        finished_at=datetime.now(UTC),
        params={},
        result={"error_message": "Unexpected plugin payload"},
    )

    report = normalize_task_report(task)

    assert report.task_type.value == "unknown"
    assert report.summary
    assert report.error_context is not None


# endregion test_unknown_type_maps_to_unknown_profile


# region test_partial_payload_keeps_report_visible_with_placeholders [TYPE Function]
# @RELATION  BINDS_TO -> test_report_normalizer
# @PURPOSE: Ensure missing result payload still yields visible report details with result placeholder.
def test_partial_payload_keeps_report_visible_with_placeholders():
    task = Task(
        id="partial-1",
        plugin_id="superset-backup",
        status=TaskStatus.SUCCESS,
        started_at=datetime.now(UTC),
        finished_at=datetime.now(UTC),
        params={},
        result=None,
    )

    report = normalize_task_report(task)

    assert report.task_type.value == "backup"
    assert report.details is not None
    assert "result" in report.details


# endregion test_partial_payload_keeps_report_visible_with_placeholders


# region test_clean_release_plugin_maps_to_clean_release_task_type [TYPE Function]
# @RELATION  BINDS_TO -> test_report_normalizer
# @PURPOSE: Ensure clean-release plugin ID maps to clean_release task profile and summary passthrough.
def test_clean_release_plugin_maps_to_clean_release_task_type():
    task = Task(
        id="clean-release-1",
        plugin_id="clean-release-compliance",
        status=TaskStatus.SUCCESS,
        started_at=datetime.now(UTC),
        finished_at=datetime.now(UTC),
        params={"run_id": "run-1"},
        result={"summary": "Clean release compliance passed", "run_id": "run-1"},
    )

    report = normalize_task_report(task)

    assert report.task_type.value == "clean_release"
    assert report.summary == "Clean release compliance passed"


# endregion test_clean_release_plugin_maps_to_clean_release_task_type
# endregion test_report_normalizer
