"""v0.6.1

Revision ID: 9be3cd376d02
Revises: 83923d29d59c
Create Date: 2026-05-29 00:24:37.461123

"""
from typing import Sequence, Union

import sqlmodel
from alembic import op
import sqlalchemy as sa

import app

# revision identifiers, used by Alembic.
revision: str = '9be3cd376d02'
down_revision: Union[str, None] = '83923d29d59c'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('book_tag',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('user_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('book_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('tag_id', app.api.models.base.GUID(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('user_id', 'book_id', 'tag_id', name='unique_user_book_tag')
    )
    op.create_table('tag',
    sa.Column('create_time', sa.DateTime(), nullable=False, comment='Created time'),
    sa.Column('update_time', sa.DateTime(), nullable=False, comment='Updated time'),
    sa.Column('id', app.api.models.base.GUID(), nullable=False),
    sa.Column('tenant_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('workspace_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('user_id', app.api.models.base.GUID(), nullable=False),
    sa.Column('name', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
    sa.Column('type', sqlmodel.sql.sqltypes.AutoString(length=64), nullable=False),
    sa.Column('icon', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('color', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('user_id', 'type', 'name', name='unique_user_tag')
    )
    with op.batch_alter_table('book', schema=None) as batch_op:
        batch_op.add_column(sa.Column('subtitle', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=True))
        batch_op.add_column(sa.Column('size', sa.Integer(), nullable=True))
        batch_op.add_column(sa.Column('clc_code', sqlmodel.sql.sqltypes.AutoString(), nullable=True))
        batch_op.add_column(sa.Column('ddc_code', sqlmodel.sql.sqltypes.AutoString(), nullable=True))
        batch_op.alter_column('rating',
               existing_type=sa.INTEGER(),
               type_=sa.Float(),
               existing_nullable=True)
        batch_op.create_index('idx_tenant_isbn', ['tenant_id', 'isbn'], unique=False)
        batch_op.drop_column('is_physical')
        batch_op.drop_column('location')

    with op.batch_alter_table('book_collection', schema=None) as batch_op:
        batch_op.add_column(sa.Column('description', sqlmodel.sql.sqltypes.AutoString(), nullable=True))

    with op.batch_alter_table('user_book', schema=None) as batch_op:
        batch_op.add_column(sa.Column('is_removed', sa.Integer(), server_default=sa.text('(-1)'), nullable=False))
        batch_op.add_column(sa.Column('is_physical', sa.Integer(), server_default=sa.text('(-1)'), nullable=False))
        batch_op.add_column(sa.Column('location', sqlmodel.sql.sqltypes.AutoString(), nullable=True))
        batch_op.add_column(sa.Column('code', sqlmodel.sql.sqltypes.AutoString(), nullable=True))
        batch_op.add_column(sa.Column('is_weread', sa.Integer(), server_default=sa.text('(-1)'), nullable=False))
        batch_op.alter_column('rating',
               existing_type=sa.INTEGER(),
               type_=sa.Float(),
               existing_nullable=True)
        batch_op.drop_column('tags')

    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('user_book', schema=None) as batch_op:
        batch_op.add_column(sa.Column('tags', sa.VARCHAR(), server_default=sa.text("'[]'"), nullable=False))
        batch_op.alter_column('rating',
               existing_type=sa.Float(),
               type_=sa.INTEGER(),
               existing_nullable=True)
        batch_op.drop_column('is_weread')
        batch_op.drop_column('code')
        batch_op.drop_column('location')
        batch_op.drop_column('is_physical')
        batch_op.drop_column('is_removed')

    with op.batch_alter_table('book_collection', schema=None) as batch_op:
        batch_op.drop_column('description')

    with op.batch_alter_table('book', schema=None) as batch_op:
        batch_op.add_column(sa.Column('location', sa.VARCHAR(), nullable=True))
        batch_op.add_column(sa.Column('is_physical', sa.INTEGER(), server_default=sa.text('(-1)'), nullable=False))
        batch_op.drop_index('idx_tenant_isbn')
        batch_op.alter_column('rating',
               existing_type=sa.Float(),
               type_=sa.INTEGER(),
               existing_nullable=True)
        batch_op.drop_column('ddc_code')
        batch_op.drop_column('clc_code')
        batch_op.drop_column('size')
        batch_op.drop_column('subtitle')

    op.drop_table('tag')
    op.drop_table('book_tag')
    # ### end Alembic commands ###
