from __future__ import annotations

import uuid
from enum import Enum

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

from core.db.base import Base, TimestampMixin


class MemoryType(str, Enum):
    USER = "user"
    WORK = "work"


class MemorySource(str, Enum):
    MANUAL = "manual"
    AGENT = "agent"
    IMPORTED = "imported"


class MemoryStatus(str, Enum):
    ACTIVE = "active"
    DISABLED = "disabled"


class Memory(TimestampMixin, Base):
    __tablename__: str = "memories"
    __table_args__ = {"extend_existing": True}

    id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
    )
    owner_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        nullable=False,
    )
    agent_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True),
        nullable=True,
    )
    memory_type: Mapped[MemoryType] = mapped_column(
        String(20),
        nullable=False,
    )
    title: Mapped[str | None] = mapped_column(String(255), nullable=True)
    content: Mapped[dict] = mapped_column(
        JSONB,
        nullable=False,
    )
    source: Mapped[MemorySource] = mapped_column(
        String(20),
        nullable=False,
    )
    status: Mapped[MemoryStatus] = mapped_column(
        String(20),
        nullable=False,
        default=MemoryStatus.ACTIVE,
    )
