"""Integration domain ↔ ORM converters."""

from __future__ import annotations

from typing import TYPE_CHECKING

from noteflow.domain.entities.integration import (
    Integration,
    IntegrationStatus,
    IntegrationType,
    SyncRun,
    SyncRunStatus,
)

if TYPE_CHECKING:
    from noteflow.infrastructure.persistence.models.integrations import (
        IntegrationModel,
        IntegrationSyncRunModel,
    )


class IntegrationConverter:
    """Convert between Integration domain objects and ORM models."""

    @staticmethod
    def orm_to_domain(model: IntegrationModel) -> Integration:
        """Convert ORM model to domain entity.

        Args:
            model: SQLAlchemy IntegrationModel instance.

        Returns:
            Domain Integration entity.
        """
        return Integration(
            id=model.id,
            workspace_id=model.workspace_id,
            name=model.name,
            type=IntegrationType(model.type),
            status=IntegrationStatus(model.status),
            config=dict(model.config) if model.config else {},
            last_sync=model.last_sync,
            error_message=model.error_message,
            created_at=model.created_at,
            updated_at=model.updated_at,
        )

    @staticmethod
    def to_orm_kwargs(entity: Integration) -> dict[str, object]:
        """Convert domain entity to ORM model kwargs.

        Returns a dict of kwargs rather than instantiating IntegrationModel
        directly to avoid circular imports and allow the repository to
        handle ORM construction.

        Args:
            entity: Domain Integration.

        Returns:
            Kwargs dict for IntegrationModel construction.
        """
        return {
            "id": entity.id,
            "workspace_id": entity.workspace_id,
            "name": entity.name,
            "type": entity.type.value,
            "status": entity.status.value,
            "config": entity.config,
            "last_sync": entity.last_sync,
            "error_message": entity.error_message,
            "created_at": entity.created_at,
            "updated_at": entity.updated_at,
        }


class SyncRunConverter:
    """Convert between SyncRun domain objects and ORM models."""

    @staticmethod
    def orm_to_domain(model: IntegrationSyncRunModel) -> SyncRun:
        """Convert ORM model to domain entity.

        Args:
            model: SQLAlchemy IntegrationSyncRunModel instance.

        Returns:
            Domain SyncRun entity.
        """
        status = SyncRunStatus(model.status)
        stats = dict(model.stats) if model.stats else {}
        return SyncRun(
            id=model.id,
            integration_id=model.integration_id,
            status=status,
            started_at=model.started_at,
            ended_at=model.ended_at,
            duration_ms=model.duration_ms,
            error_message=model.error_message,
            stats=stats,
        )

    @staticmethod
    def to_orm_kwargs(entity: SyncRun) -> dict[str, object]:
        """Convert domain entity to ORM model kwargs.

        Args:
            entity: Domain SyncRun.

        Returns:
            Kwargs dict for IntegrationSyncRunModel construction.
        """
        return {
            "id": entity.id,
            "integration_id": entity.integration_id,
            "status": entity.status.value,
            "started_at": entity.started_at,
            "ended_at": entity.ended_at,
            "duration_ms": entity.duration_ms,
            "error_message": entity.error_message,
            "stats": entity.stats,
        }
