"""Tests for PipelineError domain model."""

from __future__ import annotations

import uuid
from datetime import datetime, timezone

import pytest

from repo_core.domains.error.model import PipelineError


class TestPipelineErrorDomainModel:
    """Verify PipelineError domain model construction and serialization."""

    def test_from_warehouse_dict(self) -> None:
        """Construct from a dict (simulating warehouse row)."""
        row = {
            "id": str(uuid.uuid4()),
            "error_category": "publish",
            "severity": "error",
            "source_service": "tele-crawler",
            "source_topic": "telethon",
            "telegram_message_id": 42,
            "peer_id": 100500,
            "error_message": "Connection refused",
            "error_type": "ConnectionError",
            "is_retryable": True,
            "retry_count": 0,
            "max_retries": 3,
            "occurred_at": datetime.now(timezone.utc).isoformat(),
        }
        p = PipelineError.from_warehouse(row)
        assert p.telegram_message_id == 42
        assert p.peer_id == 100500

    def test_to_warehouse_includes_telegram_message_id(self) -> None:
        """to_warehouse() includes telegram_message_id from _warehouse_include."""
        p = PipelineError(
            id=uuid.uuid4(),
            error_category="publish",
            source_service="s",
            source_topic="t",
            error_message="m",
            error_type="e",
            occurred_at=datetime.now(timezone.utc),
            telegram_message_id=99,
        )
        d = p.to_warehouse()
        assert d["telegram_message_id"] == 99

    def test_no_legacy_fields(self) -> None:
        """original_message_id and original_telegram_id are removed."""
        p = PipelineError(
            id=uuid.uuid4(),
            error_category="publish",
            source_service="s",
            source_topic="t",
            error_message="m",
            error_type="e",
            occurred_at=datetime.now(timezone.utc),
        )
        assert not hasattr(p, "original_message_id")
        assert not hasattr(p, "original_telegram_id")
        assert hasattr(p, "telegram_message_id")

    def test_to_warehouse_excludes_legacy_fields(self) -> None:
        """to_warehouse() output does not contain removed fields."""
        p = PipelineError(
            id=uuid.uuid4(),
            error_category="publish",
            source_service="s",
            source_topic="t",
            error_message="m",
            error_type="e",
            occurred_at=datetime.now(timezone.utc),
        )
        d = p.to_warehouse()
        assert "original_message_id" not in d
        assert "original_telegram_id" not in d
        assert "telegram_message_id" in d
