"""Add monitor-only hosted app traffic totals to daily usage snapshots."""

import sqlalchemy as sa
from alembic import op

revision = "086"
down_revision = "085"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "project_hosted_app_usage_snapshots",
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("workspace_id", sa.UUID(), nullable=False),
        sa.Column("project_id", sa.UUID(), nullable=False),
        sa.Column("snapshot_date", sa.Date(), nullable=False),
        sa.Column("requests", sa.BigInteger(), nullable=False, server_default="0"),
        sa.Column("egress_bytes", sa.BigInteger(), nullable=False, server_default="0"),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
        sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["workspace_id"], ["workspaces.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "project_id",
            "snapshot_date",
            name="uq_project_hosted_app_usage_snapshot_date",
        ),
    )
    op.create_index(
        "ix_project_hosted_app_usage_workspace_date",
        "project_hosted_app_usage_snapshots",
        ["workspace_id", "snapshot_date"],
    )
    op.add_column(
        "workspace_usage_snapshots",
        sa.Column("hosted_app_requests", sa.BigInteger(), nullable=False, server_default="0"),
    )
    op.add_column(
        "workspace_usage_snapshots",
        sa.Column("hosted_app_egress_bytes", sa.BigInteger(), nullable=False, server_default="0"),
    )


def downgrade() -> None:
    op.drop_column("workspace_usage_snapshots", "hosted_app_egress_bytes")
    op.drop_column("workspace_usage_snapshots", "hosted_app_requests")
    op.drop_index(
        "ix_project_hosted_app_usage_workspace_date",
        table_name="project_hosted_app_usage_snapshots",
    )
    op.drop_table("project_hosted_app_usage_snapshots")
