"""
Channel Model

Channels are the top-level organizational unit for communication.
They map to team structure (#backend-cell, #pm-all, etc.).
"""

from datetime import datetime
from uuid import UUID, uuid4

from pydantic import Field

from roboco.models.base import (
    ChannelType,
    RobocoBase,
    TimestampMixin,
)

# =============================================================================
# MAIN CHANNEL MODEL
# =============================================================================


class Channel(TimestampMixin):
    """
    Top-level organizational unit for communication.

    Channels map to team structure and contain groups
    with different access levels.
    """

    # Identity
    id: UUID = Field(default_factory=uuid4, description="Channel ID")
    name: str = Field(
        ...,
        min_length=1,
        max_length=100,
        pattern=r"^#?[a-z0-9-]+$",
        description="Channel name (e.g., #backend-cell)",
    )
    slug: str = Field(
        ...,
        min_length=1,
        max_length=50,
        pattern=r"^[a-z0-9-]+$",
        description="URL-safe identifier",
    )
    type: ChannelType = Field(..., description="Channel type")

    # Description
    description: str | None = Field(
        default=None, description="Channel description/purpose"
    )
    topic: str | None = Field(default=None, description="Current channel topic")

    # Access Control
    members: list[UUID] = Field(
        default_factory=list, description="Agent IDs who can see the channel"
    )
    writers: list[UUID] = Field(
        default_factory=list, description="Agent IDs who can write to the channel"
    )
    silent_observers: list[UUID] = Field(
        default_factory=list, description="Agent IDs with silent read access (Auditor)"
    )

    # Settings
    is_archived: bool = Field(default=False)
    is_private: bool = Field(default=False)
    allow_threads: bool = Field(default=True)
    allow_reactions: bool = Field(default=True)
    message_retention_days: int | None = Field(
        default=90, description="How long to retain messages (None = forever)"
    )
    max_message_length: int = Field(default=10000, ge=1)

    # Statistics
    message_count: int = Field(default=0, ge=0)
    group_count: int = Field(default=0, ge=0)
    last_activity: datetime | None = None

    # NOTE: Channel state mutations should be performed through a ChannelService.
    # See enforcement/channel_access.py for can_read/can_write validation.
    # Methods like add_member, remove_member, archive should be in a service.


# =============================================================================
# CREATE/UPDATE SCHEMAS
# =============================================================================


class ChannelCreate(RobocoBase):
    """Schema for creating a new channel."""

    name: str = Field(..., min_length=1, max_length=100, pattern=r"^#?[a-z0-9-]+$")
    slug: str = Field(..., min_length=1, max_length=50, pattern=r"^[a-z0-9-]+$")
    type: ChannelType
    description: str | None = None
    members: list[UUID] = Field(default_factory=list)
    writers: list[UUID] = Field(default_factory=list)
    silent_observers: list[UUID] = Field(default_factory=list)
    is_private: bool = False


class ChannelUpdate(RobocoBase):
    """Schema for updating a channel."""

    name: str | None = Field(default=None, pattern=r"^#?[a-z0-9-]+$")
    description: str | None = None
    topic: str | None = None
    is_archived: bool | None = None
    allow_threads: bool | None = None
    allow_reactions: bool | None = None
    message_retention_days: int | None = None
    max_message_length: int | None = Field(default=None, ge=1)
