"""analytics_selfstat tables

Revision ID: 11c44afbea58
Revises: admin_user_plain_password
Create Date: 2026-07-08 16:32:49.351817

"""
from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op


# revision identifiers, used by Alembic.
revision: str = '11c44afbea58'
down_revision: str | Sequence[str] | None = 'admin_user_plain_password'
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    op.create_table(
        'analytics_selfstat',
        sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
        sa.Column('device_id', sa.String(length=64), nullable=False),
        sa.Column('epoch_id', sa.String(length=64), nullable=False),
        sa.Column('app_ver', sa.String(length=32), nullable=True),
        sa.Column('oem', sa.String(length=32), nullable=True),
        sa.Column('os', sa.String(length=32), nullable=True),
        sa.Column('batches_attempted', sa.BigInteger(), nullable=False, server_default='0'),
        sa.Column('batches_ok', sa.BigInteger(), nullable=False, server_default='0'),
        sa.Column('batches_fail', sa.BigInteger(), nullable=False, server_default='0'),
        sa.Column('retries', sa.BigInteger(), nullable=False, server_default='0'),
        sa.Column('queue_depth', sa.Integer(), nullable=False, server_default='0'),
        sa.Column('sent_at', sa.BigInteger(), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True),
                  server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
        sa.PrimaryKeyConstraint('id'),
    )
    with op.batch_alter_table('analytics_selfstat', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_analytics_selfstat_created_at'), ['created_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_analytics_selfstat_device_id'), ['device_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_analytics_selfstat_epoch_id'), ['epoch_id'], unique=False)

    op.create_table(
        'analytics_selfstat_event',
        sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
        sa.Column('snapshot_id', sa.Integer(), nullable=False),
        sa.Column('event', sa.String(length=64), nullable=False),
        sa.Column('attempted', sa.BigInteger(), nullable=False, server_default='0'),
        sa.Column('drop_capture', sa.BigInteger(), nullable=False, server_default='0'),
        sa.Column('delivered', sa.BigInteger(), nullable=False, server_default='0'),
        sa.Column('drop_undelivered', sa.BigInteger(), nullable=False, server_default='0'),
        sa.ForeignKeyConstraint(['snapshot_id'], ['analytics_selfstat.id'], ),
        sa.PrimaryKeyConstraint('id'),
    )
    with op.batch_alter_table('analytics_selfstat_event', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_analytics_selfstat_event_event'), ['event'], unique=False)
        batch_op.create_index(batch_op.f('ix_analytics_selfstat_event_snapshot_id'), ['snapshot_id'], unique=False)


def downgrade() -> None:
    with op.batch_alter_table('analytics_selfstat_event', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_analytics_selfstat_event_snapshot_id'))
        batch_op.drop_index(batch_op.f('ix_analytics_selfstat_event_event'))
    op.drop_table('analytics_selfstat_event')
    with op.batch_alter_table('analytics_selfstat', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_analytics_selfstat_epoch_id'))
        batch_op.drop_index(batch_op.f('ix_analytics_selfstat_device_id'))
        batch_op.drop_index(batch_op.f('ix_analytics_selfstat_created_at'))
    op.drop_table('analytics_selfstat')
