from dataclasses import dataclass, field
from datetime import UTC, datetime

from dataclasses_ujson.dataclasses_ujson import UJsonMixin  # type: ignore
from server.config import get_app_config
from server.infra.db.db_mapper import mapper_registry
from sqlalchemy import (
    BigInteger,
    Column,
    DateTime,
    ForeignKeyConstraint,
    String,
)


@mapper_registry.mapped
@dataclass
class Vote(UJsonMixin):
    __sa_dataclass_metadata_key__ = "sa"
    __tablename__ = "votes"
    __table_args__ = (
        ForeignKeyConstraint(["attachemnt_id"], ["attachments.id"], "votes_attachemnt_id_fk"),
        ForeignKeyConstraint(["beerd_id"], ["beerds.id"], "votes_beerd_id_fk"),
    )

    id: str = field(metadata={"sa": Column(String(), primary_key=True, nullable=False)})
    attachemnt_id: str = field(metadata={"sa": Column(String(), nullable=False)})
    beerd_id: str = field(metadata={"sa": Column(String(), nullable=False)})
    rate: int = field(metadata={"sa": Column(BigInteger(), nullable=False)})
    created_at: datetime = field(
        default=datetime.now(UTC),
        metadata={"sa": Column(DateTime(timezone=True), nullable=False)},
    )

    def __str__(self):
        return f"{get_app_config().app_public_url}/api/v0/attachment/{self.id}.original.ext"
