"""add signed product surface profiles and bounded trial serials

Revision ID: b5e8c1d7a934
Revises: a6c9e2f4b781
Create Date: 2026-08-26 12:00:00
"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


revision: str = "b5e8c1d7a934"
down_revision: Union[str, Sequence[str], None] = "a6c9e2f4b781"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None

TRIAL_MAXIMUM_TOTAL_DURATION_HOURS = 14 * 24


def upgrade() -> None:
    op.create_table(
        "product_surface_profiles",
        sa.Column("id", sa.String(length=128), nullable=False),
        sa.Column("product_id", sa.String(length=128), nullable=False),
        sa.Column("name", sa.String(length=256), nullable=False),
        sa.Column("description", sa.String(length=512), nullable=False),
        sa.Column("status", sa.String(length=32), nullable=False),
        sa.Column("inventory_revision", sa.Integer(), nullable=False),
        sa.Column("inventory_sha256", sa.String(length=64), nullable=False),
        sa.Column("analysis_ids_json", sa.JSON(), nullable=False),
        sa.Column("main_tab_ids_json", sa.JSON(), nullable=False),
        sa.Column("created_by_user_id", sa.String(length=128), nullable=False),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.func.now(),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.func.now(),
            nullable=False,
        ),
        sa.CheckConstraint(
            "status IN ('active', 'archived')",
            name="ck_product_surface_profiles_status",
        ),
        sa.CheckConstraint(
            "inventory_revision > 0",
            name="ck_product_surface_profiles_inventory_revision",
        ),
        sa.ForeignKeyConstraint(["created_by_user_id"], ["users.id"]),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "product_id",
            "name",
            name="uq_product_surface_profiles_product_name",
        ),
    )
    op.create_index(
        "ix_product_surface_profiles_product_status",
        "product_surface_profiles",
        ["product_id", "status"],
        unique=False,
    )
    with op.batch_alter_table("licenses") as batch_op:
        batch_op.add_column(
            sa.Column("surface_profile_id", sa.String(length=128), nullable=True)
        )
        batch_op.create_foreign_key(
            "fk_licenses_surface_profile_id",
            "product_surface_profiles",
            ["surface_profile_id"],
            ["id"],
        )
    with op.batch_alter_table("serial_batches") as batch_op:
        batch_op.add_column(
            sa.Column("surface_profile_id", sa.String(length=128), nullable=True)
        )
        batch_op.add_column(
            sa.Column("trial_duration_hours", sa.Integer(), nullable=True)
        )
        batch_op.create_foreign_key(
            "fk_serial_batches_surface_profile_id",
            "product_surface_profiles",
            ["surface_profile_id"],
            ["id"],
        )
        batch_op.create_check_constraint(
            "ck_serial_batches_trial_duration_hours",
            "trial_duration_hours IS NULL OR "
            "(trial_duration_hours >= 1 AND trial_duration_hours <= "
            f"{TRIAL_MAXIMUM_TOTAL_DURATION_HOURS})",
        )


def downgrade() -> None:
    with op.batch_alter_table("serial_batches") as batch_op:
        batch_op.drop_constraint(
            "ck_serial_batches_trial_duration_hours",
            type_="check",
        )
        batch_op.drop_constraint(
            "fk_serial_batches_surface_profile_id",
            type_="foreignkey",
        )
        batch_op.drop_column("trial_duration_hours")
        batch_op.drop_column("surface_profile_id")
    with op.batch_alter_table("licenses") as batch_op:
        batch_op.drop_constraint(
            "fk_licenses_surface_profile_id",
            type_="foreignkey",
        )
        batch_op.drop_column("surface_profile_id")
    op.drop_index(
        "ix_product_surface_profiles_product_status",
        table_name="product_surface_profiles",
    )
    op.drop_table("product_surface_profiles")
