from datetime import datetime
from typing import Optional

from sqlalchemy import Integer, String, DateTime, Index, UniqueConstraint, func
from sqlalchemy.orm import Mapped, mapped_column

from shared.db.base import Base


class Like(Base):
    __tablename__ = "likes"

    __table_args__ = (
        UniqueConstraint("user_id", "target_type", "target_slug",
                         name="uq_likes_user_type_slug"),
        UniqueConstraint("user_id", "target_type", "target_id",
                         name="uq_likes_user_type_id"),
        Index("ix_likes_target", "target_type", "target_slug"),
    )

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)

    user_id: Mapped[int] = mapped_column(Integer, nullable=False, index=True)
    target_type: Mapped[str] = mapped_column(String(32), nullable=False)
    target_slug: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
    target_id: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)

    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), server_default=func.now(), nullable=False,
    )
    updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), server_default=func.now(), nullable=False,
    )
    deleted_at: Mapped[Optional[datetime]] = mapped_column(
        DateTime(timezone=True), nullable=True,
    )
