"""Calendar data converters.

Convert between CalendarEventModel (ORM) and CalendarEventInfo (domain).
"""

from __future__ import annotations

from typing import TYPE_CHECKING
from uuid import UUID

from noteflow.config.constants import RULE_FIELD_DESCRIPTION
from noteflow.domain.ports.calendar import CalendarEventInfo
from noteflow.infrastructure.triggers.calendar import CalendarEvent

if TYPE_CHECKING:
    from noteflow.infrastructure.persistence.models.integrations.integration import (
        CalendarEventModel,
    )


class CalendarEventConverter:
    """Convert between CalendarEventModel and CalendarEventInfo."""

    @staticmethod
    def orm_to_info(model: CalendarEventModel, provider: str = "") -> CalendarEventInfo:
        """Convert ORM model to domain CalendarEventInfo.

        Args:
            model: SQLAlchemy CalendarEventModel instance.
            provider: Provider name (google, outlook).

        Returns:
            Domain CalendarEventInfo value object.
        """
        return CalendarEventInfo(
            id=model.external_id,
            title=model.title,
            start_time=model.start_time,
            end_time=model.end_time,
            attendees=tuple(model.attendees) if model.attendees else (),
            location=model.location,
            description=model.description,
            meeting_url=model.meeting_link,
            is_recurring=False,  # Not stored in current model
            is_all_day=model.is_all_day,
            provider=provider,
            raw=dict(model.raw) if model.raw else None,
        )

    @staticmethod
    def info_to_orm_kwargs(
        event: CalendarEventInfo,
        integration_id: UUID,
        calendar_id: str = "primary",
        calendar_name: str = "Primary",
    ) -> dict[str, object]:
        """Convert CalendarEventInfo to ORM model kwargs.

        Args:
            event: Domain CalendarEventInfo.
            integration_id: Parent integration UUID.
            calendar_id: Calendar identifier from provider.
            calendar_name: Display name of the calendar.

        Returns:
            Kwargs dict for CalendarEventModel construction.
        """
        return {
            "integration_id": integration_id,
            "external_id": event.id,
            "calendar_id": calendar_id,
            "calendar_name": calendar_name,
            "title": event.title,
            RULE_FIELD_DESCRIPTION: event.description,
            "start_time": event.start_time,
            "end_time": event.end_time,
            "location": event.location,
            "attendees": list(event.attendees) if event.attendees else None,
            "is_all_day": event.is_all_day,
            "meeting_link": event.meeting_url,
            "raw": event.raw or {},
        }

    @staticmethod
    def info_to_trigger_event(event: CalendarEventInfo) -> CalendarEvent:
        """Convert CalendarEventInfo to trigger-layer CalendarEvent.

        The trigger-layer CalendarEvent is a lightweight dataclass used
        for signal detection in the trigger system.

        Args:
            event: Domain CalendarEventInfo.

        Returns:
            Trigger-layer CalendarEvent value object.
        """
        return CalendarEvent(
            start=event.start_time,
            end=event.end_time,
            title=event.title,
        )

    @staticmethod
    def orm_to_trigger_event(model: CalendarEventModel) -> CalendarEvent:
        """Convert ORM model to trigger-layer CalendarEvent.

        Args:
            model: SQLAlchemy CalendarEventModel instance.

        Returns:
            Trigger-layer CalendarEvent value object.
        """
        return CalendarEvent(
            start=model.start_time,
            end=model.end_time,
            title=model.title,
        )
