"""Initial

Revision ID: e6f25df0df90
Revises: 
Create Date: 2026-01-11 11:51:54.002127

"""
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 = 'e6f25df0df90'
down_revision: Union[str, None] = None
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('clip_states',
    sa.Column('clip_id', sa.Text(), nullable=False),
    sa.Column('data', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
    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.PrimaryKeyConstraint('clip_id')
    )
    op.create_index('idx_clip_states_camera', 'clip_states', [sa.literal_column("jsonb_extract_path_text(data, 'camera_name')")], unique=False)
    op.create_index('idx_clip_states_status', 'clip_states', [sa.literal_column("jsonb_extract_path_text(data, 'status')")], unique=False)
    op.create_table('logs',
    sa.Column('id', sa.BigInteger(), autoincrement=True, nullable=False),
    sa.Column('ts', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('payload', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('logs_ts_idx', 'logs', [sa.literal_column('ts DESC')], unique=False)
    op.create_table('clip_events',
    sa.Column('id', sa.BigInteger(), autoincrement=True, nullable=False),
    sa.Column('clip_id', sa.Text(), nullable=False),
    sa.Column('timestamp', sa.DateTime(timezone=True), nullable=False),
    sa.Column('event_type', sa.Text(), nullable=False),
    sa.Column('event_data', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
    sa.ForeignKeyConstraint(['clip_id'], ['clip_states.clip_id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_clip_events_clip_id', 'clip_events', ['clip_id'], unique=False)
    op.create_index('idx_clip_events_clip_id_id', 'clip_events', ['clip_id', 'id'], unique=False)
    op.create_index('idx_clip_events_timestamp', 'clip_events', ['timestamp'], unique=False)
    op.create_index('idx_clip_events_type', 'clip_events', ['event_type'], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index('idx_clip_events_type', table_name='clip_events')
    op.drop_index('idx_clip_events_timestamp', table_name='clip_events')
    op.drop_index('idx_clip_events_clip_id_id', table_name='clip_events')
    op.drop_index('idx_clip_events_clip_id', table_name='clip_events')
    op.drop_table('clip_events')
    op.drop_index('logs_ts_idx', table_name='logs')
    op.drop_table('logs')
    op.drop_index('idx_clip_states_status', table_name='clip_states')
    op.drop_index('idx_clip_states_camera', table_name='clip_states')
    op.drop_table('clip_states')
    # ### end Alembic commands ###
