from __future__ import annotations

from datetime import datetime
from enum import Enum
from typing import ClassVar
from uuid import UUID

from pydantic import BaseModel, ConfigDict, EmailStr, Field


class AttachmentType(str, Enum):
    DOCUMENT = "document"
    REMINDER = "reminder"


class ScheduleItemMetadataAttachment(BaseModel):
    name: str
    type: AttachmentType
    visible_to: list[UUID] = Field(default_factory=list)
    url: str | None = None
    note: str | None = None
    content: str | None = None


class ScheduleItemMetadata(BaseModel):
    color: str | None = None
    location: str | None = None
    notes: str | None = None
    attachments: list[ScheduleItemMetadataAttachment] = Field(default_factory=list)
    version: int = 1


class ScheduleItemStatus(str, Enum):
    ACTIVE = "active"
    COMPLETED = "completed"
    CANCELED = "canceled"
    ARCHIVED = "archived"


class ScheduleItemSourceType(str, Enum):
    MANUAL = "manual"
    IMPORTED = "imported"
    AGENT_GENERATED = "agent_generated"


class ScheduleItemCreateRequest(BaseModel):
    model_config: ClassVar[ConfigDict] = ConfigDict(extra="forbid")

    title: str = Field(min_length=1, max_length=255)
    description: str | None = Field(default=None, max_length=2000)
    start_at: datetime
    end_at: datetime | None = None
    timezone: str = Field(default="UTC", max_length=50)
    metadata: ScheduleItemMetadata | None = None


class ScheduleItemUpdateRequest(BaseModel):
    model_config: ClassVar[ConfigDict] = ConfigDict(extra="forbid")

    title: str | None = Field(default=None, min_length=1, max_length=255)
    description: str | None = Field(default=None, max_length=2000)
    start_at: datetime | None = None
    end_at: datetime | None = None
    timezone: str | None = Field(default=None, max_length=50)
    metadata: ScheduleItemMetadata | None = None
    status: ScheduleItemStatus | None = None


class ScheduleItemResponse(BaseModel):
    model_config: ClassVar[ConfigDict] = ConfigDict(from_attributes=True)

    id: UUID
    title: str
    description: str | None = None
    start_at: datetime
    end_at: datetime | None = None
    timezone: str
    metadata: ScheduleItemMetadata | None = None
    status: ScheduleItemStatus
    source_type: ScheduleItemSourceType
    created_at: datetime
    updated_at: datetime


class ScheduleItemListItem(BaseModel):
    model_config: ClassVar[ConfigDict] = ConfigDict(from_attributes=True)

    id: UUID
    title: str
    start_at: datetime
    end_at: datetime | None = None
    timezone: str
    status: ScheduleItemStatus


class ScheduleItemListRequest(BaseModel):
    start_at: datetime
    end_at: datetime


# Permission bit constants (matching PermissionBits in inbox_messages/schemas.py)
_PERMISSION_VIEW = 1  # 001
_PERMISSION_INVITE = 2  # 010
_PERMISSION_EDIT = 4  # 100


class ScheduleItemShareRequest(BaseModel):
    model_config: ClassVar[ConfigDict] = ConfigDict(extra="forbid")

    email: EmailStr = Field(..., description="Email of user to share with")
    permission_view: bool = Field(True, description="Grant view permission")
    permission_edit: bool = Field(False, description="Grant edit permission")
    permission_invite: bool = Field(False, description="Grant invite permission")

    def _permission_value(self) -> int:
        value = 0
        if self.permission_view:
            value |= _PERMISSION_VIEW
        if self.permission_edit:
            value |= _PERMISSION_EDIT
        if self.permission_invite:
            value |= _PERMISSION_INVITE
        return value


class ScheduleItemShareResponse(BaseModel):
    message: str
