"""add_user_activity_logs

Revision ID: c1d91928e145
Revises: 3f0ba5a7a28c
Create Date: 2026-07-13 23:30:31.511697
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa


revision: str = 'c1d91928e145'
down_revision: Union[str, None] = '3f0ba5a7a28c'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('user_activity_logs',
    sa.Column('id', sa.Uuid(), nullable=False),
    sa.Column('user_id', sa.Uuid(), nullable=True),
    sa.Column('tenant_id', sa.Uuid(), nullable=True),
    sa.Column('activity_type', sa.String(length=50), nullable=False),
    sa.Column('target_type', sa.String(length=30), nullable=True),
    sa.Column('target_id', sa.String(length=200), nullable=True),
    sa.Column('detail', sa.JSON(), nullable=True),
    sa.Column('ip_address', sa.String(length=45), nullable=True),
    sa.Column('user_agent', sa.String(length=500), nullable=True),
    sa.Column('session_id', sa.String(length=100), nullable=True),
    sa.Column('duration_seconds', sa.Integer(), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ondelete='SET NULL'),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='SET NULL'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('ix_ual_target', 'user_activity_logs', ['target_type', 'target_id'], unique=False)
    op.create_index('ix_ual_type_created', 'user_activity_logs', ['activity_type', 'created_at'], unique=False)
    op.create_index('ix_ual_user_created', 'user_activity_logs', ['user_id', 'created_at'], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index('ix_ual_user_created', table_name='user_activity_logs')
    op.drop_index('ix_ual_type_created', table_name='user_activity_logs')
    op.drop_index('ix_ual_target', table_name='user_activity_logs')
    op.drop_table('user_activity_logs')
    # ### end Alembic commands ###
