import uuid
from sqlalchemy import Boolean, ForeignKey, Index, String, Text, UniqueConstraint
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column
from infrastructure.database.base import Base, UUIDPrimaryKeyMixin, TimestampMixin


class TenantLocale(Base, UUIDPrimaryKeyMixin, TimestampMixin):
    __tablename__ = "tenant_locales"

    tenant_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        ForeignKey("tenants.id", ondelete="CASCADE"),
        nullable=False,
        index=True,
    )
    locale: Mapped[str] = mapped_column(String(10), nullable=False)
    is_enabled: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)

    __table_args__ = (
        UniqueConstraint("tenant_id", "locale", name="uq_tenant_locale"),
    )


class Translation(Base, UUIDPrimaryKeyMixin, TimestampMixin):
    __tablename__ = "translations"

    tenant_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True),
        ForeignKey("tenants.id", ondelete="CASCADE"),
        nullable=True,
        index=True,
    )
    locale: Mapped[str] = mapped_column(String(10), nullable=False, index=True)
    namespace: Mapped[str] = mapped_column(String(50), nullable=False, default="bot", index=True)
    key: Mapped[str] = mapped_column(String(100), nullable=False, index=True)
    value: Mapped[str] = mapped_column(Text, nullable=False)
    is_approved: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)

    __table_args__ = (
        Index("ix_translations_lookup", "tenant_id", "locale", "namespace", "key"),
    )
