"""Additional zootech catalog tables (SERVER_MASTER_TABLES parity)."""

from __future__ import annotations

from datetime import UTC, datetime
from uuid import uuid4

from sqlalchemy import Boolean, DateTime, Float, Integer, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column

from app.db.base import Base


class _CatalogMixin:
    enterprise_id: Mapped[str] = mapped_column(String(36), nullable=False, index=True)
    version: Mapped[int] = mapped_column(Integer, nullable=False, default=1)
    content_hash: Mapped[str] = mapped_column(String(64), nullable=False, default="")
    is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
    payload_json: Mapped[str | None] = mapped_column(Text, nullable=True)
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), nullable=False, default=lambda: datetime.now(UTC)
    )
    updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        nullable=False,
        default=lambda: datetime.now(UTC),
        onupdate=lambda: datetime.now(UTC),
    )


class ZootechUnloadingGroup(_CatalogMixin, Base):
    __tablename__ = "zootech_unloading_group"
    __table_args__ = (UniqueConstraint("enterprise_id", "id", name="uq_zootech_unloading_group_ent_id"),)

    id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid4()))
    name: Mapped[str] = mapped_column(String(100), nullable=False, default="")
    recipe_id: Mapped[str] = mapped_column(String(36), nullable=False, index=True)


class ZootechFeedMixer(_CatalogMixin, Base):
    __tablename__ = "zootech_feed_mixer"
    __table_args__ = (UniqueConstraint("enterprise_id", "id", name="uq_zootech_feed_mixer_ent_id"),)

    id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid4()))
    name: Mapped[str] = mapped_column(String(100), nullable=False, default="")


class ZootechFeedDispenser(_CatalogMixin, Base):
    __tablename__ = "zootech_feed_dispenser"
    __table_args__ = (UniqueConstraint("enterprise_id", "id", name="uq_zootech_feed_dispenser_ent_id"),)

    id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid4()))
    name: Mapped[str] = mapped_column(String(100), nullable=False, default="")


class ZootechFeedingLocation(_CatalogMixin, Base):
    __tablename__ = "zootech_feeding_location"
    __table_args__ = (UniqueConstraint("enterprise_id", "id", name="uq_zootech_feeding_location_ent_id"),)

    id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid4()))
    name: Mapped[str] = mapped_column(String(100), nullable=False, default="")


class ZootechFeedingPeriod(_CatalogMixin, Base):
    __tablename__ = "zootech_feeding_period"
    __table_args__ = (UniqueConstraint("enterprise_id", "id", name="uq_zootech_feeding_period_ent_id"),)

    id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid4()))
    name: Mapped[str] = mapped_column(String(100), nullable=False, default="")
    dispenser_id: Mapped[str | None] = mapped_column(String(36), nullable=True)


class ZootechFeedingPoint(_CatalogMixin, Base):
    __tablename__ = "zootech_feeding_point"
    __table_args__ = (UniqueConstraint("enterprise_id", "id", name="uq_zootech_feeding_point_ent_id"),)

    id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid4()))
    name: Mapped[str] = mapped_column(String(100), nullable=False, default="")
    period_id: Mapped[str | None] = mapped_column(String(36), nullable=True)


class ZootechPeriodRecipe(_CatalogMixin, Base):
    __tablename__ = "zootech_period_recipes"
    __table_args__ = (
        UniqueConstraint("enterprise_id", "period_id", "recipe_id", name="uq_zootech_period_recipes_ent"),
    )

    period_id: Mapped[str] = mapped_column(String(36), primary_key=True)
    recipe_id: Mapped[str] = mapped_column(String(36), primary_key=True)
    order: Mapped[int] = mapped_column(Integer, nullable=False, default=0)


class ZootechTrip(_CatalogMixin, Base):
    __tablename__ = "zootech_trip"
    __table_args__ = (UniqueConstraint("enterprise_id", "id", name="uq_zootech_trip_ent_id"),)

    id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid4()))
    mixer_id: Mapped[str | None] = mapped_column(String(36), nullable=True)
    recipe_id: Mapped[str | None] = mapped_column(String(36), nullable=True)
    status: Mapped[str] = mapped_column(String(20), nullable=False, default="pending")


class ZootechDailyTripSkip(_CatalogMixin, Base):
    __tablename__ = "zootech_daily_trip_skip"
    __table_args__ = (UniqueConstraint("enterprise_id", "id", name="uq_zootech_daily_trip_skip_ent_id"),)

    id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid4()))


class ZootechDailyIngredientSkip(_CatalogMixin, Base):
    __tablename__ = "zootech_daily_ingredient_skip"
    __table_args__ = (UniqueConstraint("enterprise_id", "id", name="uq_zootech_daily_ingredient_skip_ent_id"),)

    id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid4()))


class ZootechDailyUnloadingGroupSkip(_CatalogMixin, Base):
    __tablename__ = "zootech_daily_unloading_group_skip"
    __table_args__ = (
        UniqueConstraint("enterprise_id", "id", name="uq_zootech_daily_unloading_group_skip_ent_id"),
    )

    id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid4()))


class ZootechDailyIngredientReplacement(_CatalogMixin, Base):
    __tablename__ = "zootech_daily_ingredient_replacement"
    __table_args__ = (
        UniqueConstraint("enterprise_id", "id", name="uq_zootech_daily_ingredient_replacement_ent_id"),
    )

    id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid4()))


class ZootechDailyComponentNormAdjustment(_CatalogMixin, Base):
    __tablename__ = "zootech_daily_component_norm_adjustment"
    __table_args__ = (
        UniqueConstraint("enterprise_id", "id", name="uq_zootech_daily_component_norm_adjustment_ent_id"),
    )

    id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid4()))


CATALOG_TABLE_ORDER = [
    "component",
    "recipe",
    "ingredient",
    "unloading_group",
    "feed_mixer",
    "feed_dispenser",
    "feeding_location",
    "feeding_period",
    "feeding_point",
    "period_recipes",
    "daily_trip_skip",
    "daily_ingredient_skip",
    "daily_unloading_group_skip",
    "daily_ingredient_replacement",
    "daily_component_norm_adjustment",
    "trip",
]
