# #region Test.TranslateHelpers [C:2] [TYPE Module] [SEMANTICS test,translate,helpers,run,dict]
# @BRIEF Unit tests for shared translate helper functions.
# @RELATION BINDS_TO -> [TranslateHelpersModule]

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

from unittest.mock import MagicMock
from datetime import datetime, UTC
import pytest


def make_run(**overrides):
    run = MagicMock()
    run.id = "run-1"
    run.job_id = "job-1"
    run.status = "completed"
    run.trigger_type = "manual"
    run.started_at = datetime(2026, 1, 1, 12, 0, 0, tzinfo=UTC)
    run.completed_at = datetime(2026, 1, 1, 14, 0, 0, tzinfo=UTC)
    run.error_message = None
    run.total_records = 100
    run.successful_records = 90
    run.failed_records = 5
    run.skipped_records = 5
    run.cache_hits = 10
    run.insert_status = "completed"
    run.superset_execution_id = "exec-1"
    run.config_snapshot = {"key": "val"}
    run.key_hash = "abc123"
    run.config_hash = "def456"
    run.dict_snapshot_hash = "ghi789"
    run.created_by = "testuser"
    run.created_at = datetime(2026, 1, 1, 12, 0, 0, tzinfo=UTC)
    for k, v in overrides.items():
        setattr(run, k, v)
    return run


class TestRunToResponse:
    """_run_to_response — convert TranslationRun to dict."""

    def test_full_run(self):
        from src.api.routes.translate._helpers import _run_to_response
        run = make_run()
        result = _run_to_response(run)
        assert result["id"] == "run-1"
        assert result["job_id"] == "job-1"
        assert result["status"] == "completed"
        assert result["total_records"] == 100

    def test_null_dates(self):
        from src.api.routes.translate._helpers import _run_to_response
        run = make_run(started_at=None, completed_at=None)
        result = _run_to_response(run)
        assert result["started_at"] is None
        assert result["completed_at"] is None


class TestDictToResponse:
    """_dict_to_response — convert TerminologyDictionary to dict."""

    def test_basic_conversion(self):
        from src.api.routes.translate._helpers import _dict_to_response
        d = MagicMock()
        d.id = "dict-1"
        d.name = "My Dictionary"
        d.description = "Test desc"
        d.is_active = True
        d.created_by = "admin"
        d.created_at = datetime(2026, 1, 1, 12, 0, 0, tzinfo=UTC)
        d.updated_at = datetime(2026, 1, 1, 13, 0, 0, tzinfo=UTC)

        result = _dict_to_response(d, entry_count=42)
        assert result["id"] == "dict-1"
        assert result["name"] == "My Dictionary"
        assert result["entry_count"] == 42
        assert result["is_active"] is True


class TestGetDictionaryEntryCounts:
    """_get_dictionary_entry_counts — query entry counts."""

    def test_returns_counts(self):
        from src.api.routes.translate._helpers import _get_dictionary_entry_counts
        from sqlalchemy import func

        db = MagicMock()
        mock_row_1 = ("dict-1", 10)
        mock_row_2 = ("dict-2", 5)
        db.query.return_value.filter.return_value.group_by.return_value.all.return_value = [
            mock_row_1, mock_row_2,
        ]

        result = _get_dictionary_entry_counts(db, ["dict-1", "dict-2"])
        assert result == {"dict-1": 10, "dict-2": 5}

    def test_empty_input(self):
        from src.api.routes.translate._helpers import _get_dictionary_entry_counts
        db = MagicMock()
        db.query.return_value.filter.return_value.group_by.return_value.all.return_value = []
        result = _get_dictionary_entry_counts(db, [])
        assert result == {}
# #endregion Test.TranslateHelpers
