"""Coordination inter-session message model."""

from datetime import datetime
from typing import Optional

from sqlalchemy import DateTime, Index, String, Text
from sqlalchemy.orm import Mapped, mapped_column

from .base import Base, TimestampMixin, UUIDMixin


class CoordMessage(Base, UUIDMixin, TimestampMixin):
    """A message sent from one session to another (or broadcast)."""

    __tablename__ = "coord_messages"

    from_session: Mapped[str] = mapped_column(
        String(200),
        nullable=False,
        doc="Sending session, e.g. 'DESKTOP-0O8A1RL/Claude'"
    )

    to_session: Mapped[Optional[str]] = mapped_column(
        String(200),
        doc="Recipient session; NULL means broadcast to all"
    )

    project_key: Mapped[Optional[str]] = mapped_column(
        String(200),
        doc="Optional project context for the message"
    )

    subject: Mapped[str] = mapped_column(
        String(500),
        nullable=False,
        doc="Message subject line"
    )

    body: Mapped[str] = mapped_column(
        Text,
        nullable=False,
        doc="Message body, markdown ok"
    )

    read_at: Mapped[Optional[datetime]] = mapped_column(
        DateTime,
        doc="NULL means unread"
    )

    __table_args__ = (
        Index("idx_coord_messages_to_read", "to_session", "read_at"),
        Index("idx_coord_messages_from", "from_session"),
    )

    def __repr__(self) -> str:
        return f"<CoordMessage(from='{self.from_session}', to='{self.to_session}', subject='{self.subject}')>"
