"""Initial migration: create all tables

Revision ID: 8ef31f2c15b2
Revises: 
Create Date: 2026-01-02 14:40:13.722481

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '8ef31f2c15b2'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('users',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.Column('email', sa.String(length=255), nullable=False),
    sa.Column('username', sa.String(length=50), nullable=False),
    sa.Column('hashed_password', sa.String(length=255), nullable=False),
    sa.Column('is_active', sa.Boolean(), nullable=False),
    sa.Column('is_verified', sa.Boolean(), nullable=False),
    sa.Column('verification_token', sa.String(length=255), nullable=True),
    sa.Column('reset_token', sa.String(length=255), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('users', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_users_email'), ['email'], unique=True)
        batch_op.create_index(batch_op.f('ix_users_username'), ['username'], unique=True)

    op.create_table('stories',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.Column('user_id', sa.String(length=36), nullable=False),
    sa.Column('title', sa.String(length=255), nullable=False),
    sa.Column('genre', sa.String(length=100), nullable=True),
    sa.Column('epoch', sa.String(length=100), nullable=True),
    sa.Column('reality_level', sa.Integer(), nullable=False),
    sa.Column('narrative_perspective', sa.String(length=10), nullable=False),
    sa.Column('protagonist_name', sa.String(length=100), nullable=True),
    sa.Column('protagonist_gender', sa.String(length=10), nullable=True),
    sa.Column('context_answers', sa.JSON(), nullable=True),
    sa.Column('narrative_skeleton', sa.JSON(), nullable=True),
    sa.Column('cover_image_url', sa.Text(), nullable=True),
    sa.Column('game_mode', sa.String(length=20), nullable=False),
    sa.Column('status', sa.String(length=20), nullable=False),
    sa.Column('is_published', sa.Boolean(), nullable=False),
    sa.Column('publication_status', sa.String(length=20), nullable=False),
    sa.Column('publication_description', sa.Text(), nullable=True),
    sa.Column('published_turn_count', sa.Integer(), nullable=False),
    sa.Column('turn_count', sa.Integer(), nullable=False),
    sa.Column('views_count', sa.Integer(), nullable=False),
    sa.Column('likes_count', sa.Integer(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.Column('last_played_at', sa.DateTime(), nullable=False),
    sa.Column('published_at', sa.DateTime(), nullable=True),
    sa.Column('last_chapter_published_at', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('stories', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_stories_genre'), ['genre'], unique=False)
        batch_op.create_index(batch_op.f('ix_stories_is_published'), ['is_published'], unique=False)
        batch_op.create_index(batch_op.f('ix_stories_publication_status'), ['publication_status'], unique=False)
        batch_op.create_index(batch_op.f('ix_stories_status'), ['status'], unique=False)
        batch_op.create_index(batch_op.f('ix_stories_user_id'), ['user_id'], unique=False)

    op.create_table('lorebooks',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.Column('story_id', sa.String(length=36), nullable=False),
    sa.Column('characters', sa.JSON(), nullable=True),
    sa.Column('locations', sa.JSON(), nullable=True),
    sa.Column('items', sa.JSON(), nullable=True),
    sa.Column('key_events', sa.JSON(), nullable=True),
    sa.Column('protagonist_state', sa.JSON(), nullable=True),
    sa.Column('last_updated_turn', sa.Integer(), nullable=False),
    sa.Column('version', sa.Integer(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['story_id'], ['stories.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('lorebooks', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_lorebooks_story_id'), ['story_id'], unique=True)

    op.create_table('story_favorites',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.Column('user_id', sa.String(length=36), nullable=False),
    sa.Column('story_id', sa.String(length=36), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['story_id'], ['stories.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('user_id', 'story_id', name='unique_user_story_favorite')
    )
    with op.batch_alter_table('story_favorites', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_story_favorites_story_id'), ['story_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_story_favorites_user_id'), ['user_id'], unique=False)

    op.create_table('story_likes',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.Column('user_id', sa.String(length=36), nullable=False),
    sa.Column('story_id', sa.String(length=36), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['story_id'], ['stories.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('user_id', 'story_id', name='unique_user_story_like')
    )
    with op.batch_alter_table('story_likes', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_story_likes_story_id'), ['story_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_story_likes_user_id'), ['user_id'], unique=False)

    op.create_table('story_turns',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.Column('story_id', sa.String(length=36), nullable=False),
    sa.Column('turn_number', sa.Integer(), nullable=False),
    sa.Column('narrative_text', sa.Text(), nullable=False),
    sa.Column('player_action', sa.Text(), nullable=True),
    sa.Column('action_type', sa.String(length=20), nullable=True),
    sa.Column('is_technical', sa.Boolean(), nullable=False),
    sa.Column('available_choices', sa.JSON(), nullable=True),
    sa.Column('selected_choice_index', sa.Integer(), nullable=True),
    sa.Column('is_published', sa.Boolean(), nullable=False),
    sa.Column('tokens_used', sa.Integer(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['story_id'], ['stories.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('story_turns', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_story_turns_is_published'), ['is_published'], unique=False)
        batch_op.create_index(batch_op.f('ix_story_turns_is_technical'), ['is_technical'], unique=False)
        batch_op.create_index(batch_op.f('ix_story_turns_story_id'), ['story_id'], unique=False)

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('story_turns', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_story_turns_story_id'))
        batch_op.drop_index(batch_op.f('ix_story_turns_is_technical'))
        batch_op.drop_index(batch_op.f('ix_story_turns_is_published'))

    op.drop_table('story_turns')
    with op.batch_alter_table('story_likes', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_story_likes_user_id'))
        batch_op.drop_index(batch_op.f('ix_story_likes_story_id'))

    op.drop_table('story_likes')
    with op.batch_alter_table('story_favorites', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_story_favorites_user_id'))
        batch_op.drop_index(batch_op.f('ix_story_favorites_story_id'))

    op.drop_table('story_favorites')
    with op.batch_alter_table('lorebooks', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_lorebooks_story_id'))

    op.drop_table('lorebooks')
    with op.batch_alter_table('stories', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_stories_user_id'))
        batch_op.drop_index(batch_op.f('ix_stories_status'))
        batch_op.drop_index(batch_op.f('ix_stories_publication_status'))
        batch_op.drop_index(batch_op.f('ix_stories_is_published'))
        batch_op.drop_index(batch_op.f('ix_stories_genre'))

    op.drop_table('stories')
    with op.batch_alter_table('users', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_users_username'))
        batch_op.drop_index(batch_op.f('ix_users_email'))

    op.drop_table('users')
    # ### end Alembic commands ###
