"""initial schema

Revision ID: 1e2e34a0cb06
Revises: 
Create Date: 2026-07-15 01:48:41.232692

"""
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 = '1e2e34a0cb06'
down_revision: Union[str, Sequence[str], None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # Required for the `room_bookings` EXCLUDE USING gist constraint, which
    # mixes an equality operator (room_id) with a range overlap operator
    # (period) — btree_gist supplies the GiST operator class for `=` on
    # non-range types such as uuid.
    op.execute("CREATE EXTENSION IF NOT EXISTS btree_gist")

    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('rooms',
    sa.Column('id', sa.UUID(), server_default=sa.text('gen_random_uuid()'), nullable=False),
    sa.Column('name', sa.String(length=255), nullable=False),
    sa.Column('is_pinned', sa.Boolean(), server_default='false', nullable=False),
    sa.Column('permanent_link', sa.String(length=64), nullable=False),
    sa.Column('is_active', sa.Boolean(), server_default='true', nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('permanent_link')
    )
    op.create_table('users',
    sa.Column('id', sa.UUID(), server_default=sa.text('gen_random_uuid()'), nullable=False),
    sa.Column('email', sa.String(length=255), nullable=False),
    sa.Column('name_user', sa.String(length=255), nullable=False),
    sa.Column('password_hash', sa.Text(), nullable=False),
    sa.Column('role', sa.String(length=16), server_default='user', nullable=False),
    sa.Column('email_verified', sa.Boolean(), server_default='false', nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.CheckConstraint("role IN ('admin', 'user')", name='ck_users_role'),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('email')
    )
    op.create_table('room_bookings',
    sa.Column('id', sa.UUID(), server_default=sa.text('gen_random_uuid()'), nullable=False),
    sa.Column('room_id', sa.UUID(), nullable=False),
    sa.Column('organizer_id', sa.UUID(), nullable=False),
    sa.Column('title', sa.String(length=255), nullable=True),
    sa.Column('period', postgresql.TSTZRANGE(), nullable=False),
    sa.Column('is_closed', sa.Boolean(), server_default='false', nullable=False),
    sa.Column('password_hash', sa.Text(), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    postgresql.ExcludeConstraint((sa.column('room_id'), '='), (sa.column('period'), '&&'), using='gist', name='excl_room_bookings_overlap'),
    sa.CheckConstraint('NOT isempty(period)', name='ck_room_bookings_period_not_empty'),
    sa.ForeignKeyConstraint(['organizer_id'], ['users.id'], ondelete='RESTRICT'),
    sa.ForeignKeyConstraint(['room_id'], ['rooms.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('conferences',
    sa.Column('id', sa.UUID(), server_default=sa.text('gen_random_uuid()'), nullable=False),
    sa.Column('room_id', sa.UUID(), nullable=False),
    sa.Column('booking_id', sa.UUID(), nullable=True),
    sa.Column('title', sa.String(length=255), nullable=True),
    sa.Column('t_start', sa.DateTime(timezone=True), nullable=False),
    sa.Column('t_end', sa.DateTime(timezone=True), nullable=True),
    sa.Column('summary_data', sa.Text(), nullable=True),
    sa.Column('pipeline_status', sa.Enum('recording', 'transcribing', 'summarizing', 'notified', 'failed', name='pipeline_status'), server_default='recording', nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['booking_id'], ['room_bookings.id'], ondelete='SET NULL'),
    sa.ForeignKeyConstraint(['room_id'], ['rooms.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('ix_conferences_pipeline_status', 'conferences', ['pipeline_status'], unique=False)
    op.create_index('ix_conferences_room_id_t_start', 'conferences', ['room_id', 't_start'], unique=False)
    op.create_table('chat_messages',
    sa.Column('id', sa.BigInteger(), sa.Identity(always=True), nullable=False),
    sa.Column('conference_id', sa.UUID(), nullable=False),
    sa.Column('user_id', sa.UUID(), nullable=False),
    sa.Column('text', sa.Text(), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['conference_id'], ['conferences.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('ix_chat_messages_conference_id_created_at', 'chat_messages', ['conference_id', 'created_at'], unique=False)
    op.create_table('conference_participants',
    sa.Column('id', sa.UUID(), server_default=sa.text('gen_random_uuid()'), nullable=False),
    sa.Column('conference_id', sa.UUID(), nullable=False),
    sa.Column('user_id', sa.UUID(), nullable=False),
    sa.Column('joined_at', sa.DateTime(timezone=True), nullable=False),
    sa.Column('left_at', sa.DateTime(timezone=True), nullable=True),
    sa.ForeignKeyConstraint(['conference_id'], ['conferences.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('ix_conference_participants_conference_id', 'conference_participants', ['conference_id'], unique=False)
    op.create_table('phrases',
    sa.Column('id', sa.BigInteger(), sa.Identity(always=True), nullable=False),
    sa.Column('user_id', sa.UUID(), nullable=False),
    sa.Column('conference_id', sa.UUID(), nullable=False),
    sa.Column('data', sa.Text(), nullable=False),
    sa.Column('t_start', sa.DateTime(timezone=True), nullable=False),
    sa.Column('t_end', sa.DateTime(timezone=True), nullable=False),
    sa.ForeignKeyConstraint(['conference_id'], ['conferences.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('ix_phrases_conference_id_t_start', 'phrases', ['conference_id', 't_start'], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index('ix_phrases_conference_id_t_start', table_name='phrases')
    op.drop_table('phrases')
    op.drop_index('ix_conference_participants_conference_id', table_name='conference_participants')
    op.drop_table('conference_participants')
    op.drop_index('ix_chat_messages_conference_id_created_at', table_name='chat_messages')
    op.drop_table('chat_messages')
    op.drop_index('ix_conferences_room_id_t_start', table_name='conferences')
    op.drop_index('ix_conferences_pipeline_status', table_name='conferences')
    op.drop_table('conferences')
    op.drop_table('room_bookings')
    op.drop_table('users')
    op.drop_table('rooms')
    # ### end Alembic commands ###
    postgresql.ENUM(name="pipeline_status").drop(op.get_bind(), checkfirst=True)
