"""add resource accounting and soft delete

Revision ID: 8623aa4bf3a1
Revises: 6846d43389b6
Create Date: 2026-05-29 17:16:18.935039
"""
from typing import Sequence, Union

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

# revision identifiers, used by Alembic.
revision: str = '8623aa4bf3a1'
down_revision: Union[str, None] = '6846d43389b6'
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('generated_resources',
    sa.Column('id', sa.String(length=32), nullable=False),
    sa.Column('user_id', sa.String(length=32), nullable=False),
    sa.Column('resource_type', sa.String(length=16), nullable=False),
    sa.Column('resource_url', sa.Text(), nullable=False),
    sa.Column('remote_url', sa.Text(), nullable=True),
    sa.Column('storage_type', sa.String(length=32), nullable=False),
    sa.Column('storage_path', sa.Text(), nullable=True),
    sa.Column('file_size_bytes', sa.BigInteger(), nullable=False),
    sa.Column('source_model', sa.String(length=64), nullable=False),
    sa.Column('source_model_module', sa.String(length=255), nullable=True),
    sa.Column('source_id', sa.String(length=32), nullable=False),
    sa.Column('engine_id', sa.String(length=32), nullable=True),
    sa.Column('engine_type', sa.String(length=32), nullable=True),
    sa.Column('provider', sa.String(length=64), nullable=True),
    sa.Column('model_name', sa.String(length=128), nullable=True),
    sa.Column('generated_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('resource_month', sa.Date(), nullable=False),
    sa.Column('extra_json', sa.Text(), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('deleted_at', sa.DateTime(timezone=True), nullable=True),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('ix_generated_resources_active_user', 'generated_resources', ['user_id', 'deleted_at'], unique=False)
    op.create_index(op.f('ix_generated_resources_deleted_at'), 'generated_resources', ['deleted_at'], unique=False)
    op.create_index(op.f('ix_generated_resources_engine_id'), 'generated_resources', ['engine_id'], unique=False)
    op.create_index(op.f('ix_generated_resources_generated_at'), 'generated_resources', ['generated_at'], unique=False)
    op.create_index(op.f('ix_generated_resources_resource_month'), 'generated_resources', ['resource_month'], unique=False)
    op.create_index(op.f('ix_generated_resources_resource_type'), 'generated_resources', ['resource_type'], unique=False)
    op.create_index('ix_generated_resources_source', 'generated_resources', ['source_model', 'source_id'], unique=False)
    op.create_index(op.f('ix_generated_resources_source_id'), 'generated_resources', ['source_id'], unique=False)
    op.create_index(op.f('ix_generated_resources_source_model'), 'generated_resources', ['source_model'], unique=False)
    op.create_index(op.f('ix_generated_resources_user_id'), 'generated_resources', ['user_id'], unique=False)
    op.create_index('ix_generated_resources_user_month', 'generated_resources', ['user_id', 'resource_month'], unique=False)
    op.create_table('user_resource_month_stats',
    sa.Column('id', sa.String(length=32), nullable=False),
    sa.Column('user_id', sa.String(length=32), nullable=False),
    sa.Column('stat_month', sa.Date(), nullable=False),
    sa.Column('active_size_bytes', sa.BigInteger(), nullable=False),
    sa.Column('deleted_size_bytes', sa.BigInteger(), nullable=False),
    sa.Column('total_generated_size_bytes', sa.BigInteger(), nullable=False),
    sa.Column('image_size_bytes', sa.BigInteger(), nullable=False),
    sa.Column('video_size_bytes', sa.BigInteger(), nullable=False),
    sa.Column('active_count', sa.Integer(), nullable=False),
    sa.Column('deleted_count', sa.Integer(), nullable=False),
    sa.Column('image_count', sa.Integer(), nullable=False),
    sa.Column('video_count', sa.Integer(), nullable=False),
    sa.Column('last_recalculated_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('user_id', 'stat_month', name='uq_user_resource_month_stats_user_month')
    )
    op.create_index(op.f('ix_user_resource_month_stats_stat_month'), 'user_resource_month_stats', ['stat_month'], unique=False)
    op.create_index(op.f('ix_user_resource_month_stats_user_id'), 'user_resource_month_stats', ['user_id'], unique=False)
    op.create_table('user_resource_total_stats',
    sa.Column('id', sa.String(length=32), nullable=False),
    sa.Column('user_id', sa.String(length=32), nullable=False),
    sa.Column('active_size_bytes', sa.BigInteger(), nullable=False),
    sa.Column('deleted_size_bytes', sa.BigInteger(), nullable=False),
    sa.Column('total_generated_size_bytes', sa.BigInteger(), nullable=False),
    sa.Column('image_size_bytes', sa.BigInteger(), nullable=False),
    sa.Column('video_size_bytes', sa.BigInteger(), nullable=False),
    sa.Column('active_count', sa.Integer(), nullable=False),
    sa.Column('deleted_count', sa.Integer(), nullable=False),
    sa.Column('image_count', sa.Integer(), nullable=False),
    sa.Column('video_count', sa.Integer(), nullable=False),
    sa.Column('last_recalculated_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('user_id', name='uq_user_resource_total_stats_user')
    )
    op.create_index(op.f('ix_user_resource_total_stats_user_id'), 'user_resource_total_stats', ['user_id'], unique=False)
    op.add_column('chat_generation_tasks', sa.Column('deleted_at', sa.DateTime(timezone=True), nullable=True))

    op.create_index(op.f('ix_chat_generation_tasks_deleted_at'), 'chat_generation_tasks', ['deleted_at'], unique=False)
    op.add_column('generation_records', sa.Column('deleted_at', sa.DateTime(timezone=True), nullable=True))
    op.create_index(op.f('ix_generation_records_deleted_at'), 'generation_records', ['deleted_at'], unique=False)
    op.add_column('projects', sa.Column('deleted_at', sa.DateTime(timezone=True), nullable=True))
    op.create_index(op.f('ix_projects_deleted_at'), 'projects', ['deleted_at'], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_projects_deleted_at'), table_name='projects')
    op.drop_column('projects', 'deleted_at')
    op.drop_index(op.f('ix_generation_records_deleted_at'), table_name='generation_records')
    op.drop_column('generation_records', 'deleted_at')
    op.drop_index(op.f('ix_chat_generation_tasks_deleted_at'), table_name='chat_generation_tasks')

    op.drop_column('chat_generation_tasks', 'deleted_at')
    op.drop_index(op.f('ix_user_resource_total_stats_user_id'), table_name='user_resource_total_stats')
    op.drop_table('user_resource_total_stats')
    op.drop_index(op.f('ix_user_resource_month_stats_user_id'), table_name='user_resource_month_stats')
    op.drop_index(op.f('ix_user_resource_month_stats_stat_month'), table_name='user_resource_month_stats')
    op.drop_table('user_resource_month_stats')
    op.drop_index('ix_generated_resources_user_month', table_name='generated_resources')
    op.drop_index(op.f('ix_generated_resources_user_id'), table_name='generated_resources')
    op.drop_index(op.f('ix_generated_resources_source_model'), table_name='generated_resources')
    op.drop_index(op.f('ix_generated_resources_source_id'), table_name='generated_resources')
    op.drop_index('ix_generated_resources_source', table_name='generated_resources')
    op.drop_index(op.f('ix_generated_resources_resource_type'), table_name='generated_resources')
    op.drop_index(op.f('ix_generated_resources_resource_month'), table_name='generated_resources')
    op.drop_index(op.f('ix_generated_resources_generated_at'), table_name='generated_resources')
    op.drop_index(op.f('ix_generated_resources_engine_id'), table_name='generated_resources')
    op.drop_index(op.f('ix_generated_resources_deleted_at'), table_name='generated_resources')
    op.drop_index('ix_generated_resources_active_user', table_name='generated_resources')
    op.drop_table('generated_resources')
    # ### end Alembic commands ###
