from __future__ import annotations

import uuid
from enum import Enum

from sqlalchemy import String, Text
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column

from core.db.base import Base, TimestampMixin


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 InboxMessage(TimestampMixin, Base):
    __tablename__: str = "inbox_messages"
    __table_args__ = {"extend_existing": True}

    id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
    )
    recipient_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        nullable=False,
    )
    sender_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True),
        nullable=True,
    )
    message_type: Mapped[InboxMessageType] = mapped_column(
        String(20),
        nullable=False,
    )
    friendship_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True),
        nullable=True,
    )
    schedule_item_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True),
        nullable=True,
    )
    group_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True),
        nullable=True,
    )
    content: Mapped[str | None] = mapped_column(
        Text,
        nullable=True,
    )
    is_read: Mapped[bool] = mapped_column(
        String(10),
        nullable=False,
        default=False,
    )
    status: Mapped[InboxMessageStatus] = mapped_column(
        String(20),
        nullable=False,
        default=InboxMessageStatus.PENDING,
    )
    created_by: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True),
        nullable=True,
    )
