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


class PermissionBits:
    VIEW: int = 1  # 001
    INVITE: int = 2  # 010
    EDIT: int = 4  # 100

    @classmethod
    def encode(cls, view: bool, edit: bool, invite: bool) -> int:
        value = 0
        if view:
            value |= cls.VIEW
        if edit:
            value |= cls.EDIT
        if invite:
            value |= cls.INVITE
        return value

    @classmethod
    def decode(cls, permission: int) -> dict[str, bool]:
        return {
            "view": bool(permission & cls.VIEW),
            "edit": bool(permission & cls.EDIT),
            "invite": bool(permission & cls.INVITE),
        }


class InboxMessageType(str, Enum):
    FRIEND_REQUEST = "friend_request"
    CALENDAR = "calendar"
    SYSTEM = "system"
    GROUP = "group"


class InboxMessageStatus(str, Enum):
    PENDING = "pending"
    ACCEPTED = "accepted"
    REJECTED = "rejected"
    DISMISSED = "dismissed"


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

    id: UUID
    recipient_id: UUID
    sender_id: UUID | None = None
    message_type: InboxMessageType
    schedule_item_id: UUID | None = None
    content: str | None = None
    is_read: bool = False
    status: InboxMessageStatus = InboxMessageStatus.PENDING
    created_at: datetime


class InboxMessageListRequest(BaseModel):
    status: InboxMessageStatus | None = None


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

    permission_view: bool = True
    permission_edit: bool = False
    permission_invite: bool = False
