"""zootech full catalog tables

Revision ID: 20260716_0007
Revises: 20260715_0006
"""

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

revision: str = "20260716_0007"
down_revision: Union[str, None] = "20260715_0006"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def _simple_catalog(name: str, uq: str) -> None:
    op.create_table(
        name,
        sa.Column("enterprise_id", sa.String(length=36), nullable=False),
        sa.Column("id", sa.String(length=36), nullable=False),
        sa.Column("version", sa.Integer(), nullable=False),
        sa.Column("content_hash", sa.String(length=64), nullable=False),
        sa.Column("is_deleted", sa.Boolean(), nullable=False),
        sa.Column("payload_json", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("enterprise_id", "id", name=uq),
    )
    op.create_index(f"idx_{name}_enterprise", name, ["enterprise_id"])


def upgrade() -> None:
    op.create_table(
        "zootech_unloading_group",
        sa.Column("enterprise_id", sa.String(length=36), nullable=False),
        sa.Column("id", sa.String(length=36), nullable=False),
        sa.Column("name", sa.String(length=100), nullable=False),
        sa.Column("recipe_id", sa.String(length=36), nullable=False),
        sa.Column("version", sa.Integer(), nullable=False),
        sa.Column("content_hash", sa.String(length=64), nullable=False),
        sa.Column("is_deleted", sa.Boolean(), nullable=False),
        sa.Column("payload_json", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("enterprise_id", "id", name="uq_zootech_unloading_group_ent_id"),
    )
    op.create_table(
        "zootech_feed_mixer",
        sa.Column("enterprise_id", sa.String(length=36), nullable=False),
        sa.Column("id", sa.String(length=36), nullable=False),
        sa.Column("name", sa.String(length=100), nullable=False),
        sa.Column("version", sa.Integer(), nullable=False),
        sa.Column("content_hash", sa.String(length=64), nullable=False),
        sa.Column("is_deleted", sa.Boolean(), nullable=False),
        sa.Column("payload_json", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("enterprise_id", "id", name="uq_zootech_feed_mixer_ent_id"),
    )
    op.create_table(
        "zootech_feed_dispenser",
        sa.Column("enterprise_id", sa.String(length=36), nullable=False),
        sa.Column("id", sa.String(length=36), nullable=False),
        sa.Column("name", sa.String(length=100), nullable=False),
        sa.Column("version", sa.Integer(), nullable=False),
        sa.Column("content_hash", sa.String(length=64), nullable=False),
        sa.Column("is_deleted", sa.Boolean(), nullable=False),
        sa.Column("payload_json", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("enterprise_id", "id", name="uq_zootech_feed_dispenser_ent_id"),
    )
    op.create_table(
        "zootech_feeding_location",
        sa.Column("enterprise_id", sa.String(length=36), nullable=False),
        sa.Column("id", sa.String(length=36), nullable=False),
        sa.Column("name", sa.String(length=100), nullable=False),
        sa.Column("version", sa.Integer(), nullable=False),
        sa.Column("content_hash", sa.String(length=64), nullable=False),
        sa.Column("is_deleted", sa.Boolean(), nullable=False),
        sa.Column("payload_json", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("enterprise_id", "id", name="uq_zootech_feeding_location_ent_id"),
    )
    op.create_table(
        "zootech_feeding_period",
        sa.Column("enterprise_id", sa.String(length=36), nullable=False),
        sa.Column("id", sa.String(length=36), nullable=False),
        sa.Column("name", sa.String(length=100), nullable=False),
        sa.Column("dispenser_id", sa.String(length=36), nullable=True),
        sa.Column("version", sa.Integer(), nullable=False),
        sa.Column("content_hash", sa.String(length=64), nullable=False),
        sa.Column("is_deleted", sa.Boolean(), nullable=False),
        sa.Column("payload_json", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("enterprise_id", "id", name="uq_zootech_feeding_period_ent_id"),
    )
    op.create_table(
        "zootech_feeding_point",
        sa.Column("enterprise_id", sa.String(length=36), nullable=False),
        sa.Column("id", sa.String(length=36), nullable=False),
        sa.Column("name", sa.String(length=100), nullable=False),
        sa.Column("period_id", sa.String(length=36), nullable=True),
        sa.Column("version", sa.Integer(), nullable=False),
        sa.Column("content_hash", sa.String(length=64), nullable=False),
        sa.Column("is_deleted", sa.Boolean(), nullable=False),
        sa.Column("payload_json", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("enterprise_id", "id", name="uq_zootech_feeding_point_ent_id"),
    )
    op.create_table(
        "zootech_period_recipes",
        sa.Column("enterprise_id", sa.String(length=36), nullable=False),
        sa.Column("period_id", sa.String(length=36), nullable=False),
        sa.Column("recipe_id", sa.String(length=36), nullable=False),
        sa.Column("order", sa.Integer(), nullable=False),
        sa.Column("version", sa.Integer(), nullable=False),
        sa.Column("content_hash", sa.String(length=64), nullable=False),
        sa.Column("is_deleted", sa.Boolean(), nullable=False),
        sa.Column("payload_json", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("period_id", "recipe_id"),
        sa.UniqueConstraint("enterprise_id", "period_id", "recipe_id", name="uq_zootech_period_recipes_ent"),
    )
    op.create_table(
        "zootech_trip",
        sa.Column("enterprise_id", sa.String(length=36), nullable=False),
        sa.Column("id", sa.String(length=36), nullable=False),
        sa.Column("mixer_id", sa.String(length=36), nullable=True),
        sa.Column("recipe_id", sa.String(length=36), nullable=True),
        sa.Column("status", sa.String(length=20), nullable=False),
        sa.Column("version", sa.Integer(), nullable=False),
        sa.Column("content_hash", sa.String(length=64), nullable=False),
        sa.Column("is_deleted", sa.Boolean(), nullable=False),
        sa.Column("payload_json", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("enterprise_id", "id", name="uq_zootech_trip_ent_id"),
    )
    for tbl, uq in (
        ("zootech_daily_trip_skip", "uq_zootech_daily_trip_skip_ent_id"),
        ("zootech_daily_ingredient_skip", "uq_zootech_daily_ingredient_skip_ent_id"),
        ("zootech_daily_unloading_group_skip", "uq_zootech_daily_unloading_group_skip_ent_id"),
        ("zootech_daily_ingredient_replacement", "uq_zootech_daily_ingredient_replacement_ent_id"),
        ("zootech_daily_component_norm_adjustment", "uq_zootech_daily_component_norm_adjustment_ent_id"),
    ):
        _simple_catalog(tbl, uq)


def downgrade() -> None:
    for tbl in (
        "zootech_daily_component_norm_adjustment",
        "zootech_daily_ingredient_replacement",
        "zootech_daily_unloading_group_skip",
        "zootech_daily_ingredient_skip",
        "zootech_daily_trip_skip",
        "zootech_trip",
        "zootech_period_recipes",
        "zootech_feeding_point",
        "zootech_feeding_period",
        "zootech_feeding_location",
        "zootech_feed_dispenser",
        "zootech_feed_mixer",
        "zootech_unloading_group",
    ):
        op.drop_table(tbl)
