"""SQLAlchemy ORM-модель для централизованной таблицы настроек Celery-job'ов."""

from datetime import datetime

from sqlalchemy import Boolean, DateTime, Integer, SmallInteger, Text, text
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped, mapped_column

from app.core.db import Base


class JobSetting(Base):
    __tablename__ = "job_settings"

    job_type: Mapped[str] = mapped_column(Text, primary_key=True)
    enabled: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
    queue_name: Mapped[str] = mapped_column(Text, default="celery", nullable=False)
    cron_schedule: Mapped[str | None] = mapped_column(Text, nullable=True)
    rate_ms: Mapped[int | None] = mapped_column(Integer, nullable=True)
    max_retries: Mapped[int] = mapped_column(SmallInteger, default=2, nullable=False)
    max_concurrency: Mapped[int] = mapped_column(SmallInteger, default=1, nullable=False)
    extra_config: Mapped[dict] = mapped_column(JSONB, default=dict, nullable=False)
    updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), server_default=text("now()"), nullable=False
    )
    updated_by: Mapped[str | None] = mapped_column(Text, nullable=True)
    description: Mapped[str | None] = mapped_column(Text, nullable=True)
