"""initial logic schema

Revision ID: 0001_initial
Revises:
Create Date: 2026-05-28 10:00:00.000000
"""

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

revision: str = "0001_initial"
down_revision: str | None = None
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    op.create_table(
        "logic_equipment",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column("owner_user_id", postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column("name", sa.String(length=160), nullable=False),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("image_s3_url", sa.Text(), nullable=True),
        sa.Column("image_s3_key", sa.Text(), nullable=True),
        sa.Column("is_builtin", sa.Boolean(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
    )
    op.create_index("ix_logic_equipment_is_builtin", "logic_equipment", ["is_builtin"])
    op.create_index("ix_logic_equipment_name", "logic_equipment", ["name"])
    op.create_index("ix_logic_equipment_owner_user_id", "logic_equipment", ["owner_user_id"])

    op.create_table(
        "logic_exercises",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column("owner_user_id", postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column("equipment_id", postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column("name", sa.String(length=160), nullable=False),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("image_s3_url", sa.Text(), nullable=True),
        sa.Column("image_s3_key", sa.Text(), nullable=True),
        sa.Column("is_builtin", sa.Boolean(), nullable=False),
        sa.Column("default_calories_per_minute", sa.Numeric(8, 2), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.ForeignKeyConstraint(["equipment_id"], ["logic_equipment.id"]),
    )
    op.create_index("ix_logic_exercises_is_builtin", "logic_exercises", ["is_builtin"])
    op.create_index("ix_logic_exercises_name", "logic_exercises", ["name"])
    op.create_index("ix_logic_exercises_owner_user_id", "logic_exercises", ["owner_user_id"])

    op.create_table(
        "logic_workouts",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column("user_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("started_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("finished_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("notes", sa.Text(), nullable=True),
        sa.Column("estimated_calories", sa.Numeric(10, 2), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
    )
    op.create_index("ix_logic_workouts_user_id", "logic_workouts", ["user_id"])

    op.create_table(
        "logic_workout_items",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column("workout_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("exercise_id", postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column("equipment_id", postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column("order_index", sa.Integer(), nullable=False),
        sa.Column("planned_working_weight", sa.Numeric(8, 2), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.CheckConstraint(
            "(exercise_id IS NOT NULL AND equipment_id IS NULL) OR "
            "(exercise_id IS NULL AND equipment_id IS NOT NULL)",
            name="ck_workout_item_exactly_one_entity",
        ),
        sa.ForeignKeyConstraint(["equipment_id"], ["logic_equipment.id"]),
        sa.ForeignKeyConstraint(["exercise_id"], ["logic_exercises.id"]),
        sa.ForeignKeyConstraint(["workout_id"], ["logic_workouts.id"], ondelete="CASCADE"),
    )

    op.create_table(
        "logic_workout_sets",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column("workout_item_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("set_index", sa.Integer(), nullable=False),
        sa.Column("weight", sa.Numeric(8, 2), nullable=False),
        sa.Column("reps", sa.Integer(), nullable=False),
        sa.Column("duration_seconds", sa.Integer(), nullable=True),
        sa.Column("calories", sa.Numeric(8, 2), nullable=True),
        sa.Column("completed_at", sa.DateTime(timezone=True), nullable=False),
        sa.ForeignKeyConstraint(
            ["workout_item_id"], ["logic_workout_items.id"], ondelete="CASCADE"
        ),
    )


def downgrade() -> None:
    op.drop_table("logic_workout_sets")
    op.drop_table("logic_workout_items")
    op.drop_index("ix_logic_workouts_user_id", table_name="logic_workouts")
    op.drop_table("logic_workouts")
    op.drop_index("ix_logic_exercises_owner_user_id", table_name="logic_exercises")
    op.drop_index("ix_logic_exercises_name", table_name="logic_exercises")
    op.drop_index("ix_logic_exercises_is_builtin", table_name="logic_exercises")
    op.drop_table("logic_exercises")
    op.drop_index("ix_logic_equipment_owner_user_id", table_name="logic_equipment")
    op.drop_index("ix_logic_equipment_name", table_name="logic_equipment")
    op.drop_index("ix_logic_equipment_is_builtin", table_name="logic_equipment")
    op.drop_table("logic_equipment")
