"""Introducing new licensing approach

Revision ID: 560d08d91e5b
Revises: 254932fe7fe3
Create Date: 2024-10-02 15:29:05.963865

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '560d08d91e5b'
down_revision = '254932fe7fe3'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('license_tier',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=50), nullable=False),
    sa.Column('version', sa.String(length=50), nullable=False),
    sa.Column('start_date', sa.Date(), nullable=False),
    sa.Column('end_date', sa.Date(), nullable=True),
    sa.Column('basic_fee_d', sa.Float(), nullable=True),
    sa.Column('basic_fee_e', sa.Float(), nullable=True),
    sa.Column('max_storage_tokens', sa.Integer(), nullable=False),
    sa.Column('additional_storage_token_price_d', sa.Numeric(precision=10, scale=4), nullable=False),
    sa.Column('additional_storage_token_price_e', sa.Numeric(precision=10, scale=4), nullable=False),
    sa.Column('included_embedding_tokens', sa.Integer(), nullable=False),
    sa.Column('additional_embedding_token_price_d', sa.Numeric(precision=10, scale=4), nullable=False),
    sa.Column('additional_embedding_token_price_e', sa.Numeric(precision=10, scale=4), nullable=False),
    sa.Column('additional_embedding_bucket', sa.Integer(), nullable=False),
    sa.Column('included_interaction_tokens', sa.Integer(), nullable=False),
    sa.Column('additional_interaction_token_price_d', sa.Numeric(precision=10, scale=4), nullable=False),
    sa.Column('additional_interaction_token_price_e', sa.Numeric(precision=10, scale=4), nullable=False),
    sa.Column('additional_interaction_bucket', sa.Integer(), nullable=False),
    sa.Column('allow_overage', sa.Boolean(), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    schema='public'
    )
    op.create_table('license',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('tenant_id', sa.Integer(), nullable=False),
    sa.Column('tier_id', sa.Integer(), nullable=False),
    sa.Column('start_date', sa.Date(), nullable=False),
    sa.Column('end_date', sa.Date(), nullable=True),
    sa.Column('currency', sa.String(length=20), nullable=False),
    sa.Column('basic_fee', sa.Float(), nullable=False),
    sa.Column('max_storage_tokens', sa.Integer(), nullable=False),
    sa.Column('additional_storage_token_price', sa.Float(), nullable=False),
    sa.Column('included_embedding_tokens', sa.Integer(), nullable=False),
    sa.Column('additional_embedding_token_price', sa.Numeric(precision=10, scale=4), nullable=False),
    sa.Column('additional_embedding_bucket', sa.Integer(), nullable=False),
    sa.Column('included_interaction_tokens', sa.Integer(), nullable=False),
    sa.Column('additional_interaction_token_price', sa.Numeric(precision=10, scale=4), nullable=False),
    sa.Column('additional_interaction_bucket', sa.Integer(), nullable=False),
    sa.Column('allow_overage', sa.Boolean(), nullable=True),
    sa.ForeignKeyConstraint(['tenant_id'], ['public.tenant.id'], ),
    sa.ForeignKeyConstraint(['tier_id'], ['public.license_tier.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='public'
    )
    op.create_table('license_usage',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('license_id', sa.Integer(), nullable=False),
    sa.Column('tenant_id', sa.Integer(), nullable=False),
    sa.Column('storage_tokens_used', sa.Integer(), nullable=True),
    sa.Column('embedding_tokens_used', sa.Integer(), nullable=True),
    sa.Column('interaction_tokens_used', sa.Integer(), nullable=True),
    sa.Column('period_start_date', sa.Date(), nullable=False),
    sa.Column('period_end_date', sa.Date(), nullable=False),
    sa.ForeignKeyConstraint(['license_id'], ['public.license.id'], ),
    sa.ForeignKeyConstraint(['tenant_id'], ['public.tenant.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='public'
    )
    with op.batch_alter_table('business_event_log', schema=None) as batch_op:
        batch_op.add_column(sa.Column('license_usage_id', sa.Integer(), nullable=True))
        batch_op.create_foreign_key(None, 'license_usage', ['license_usage_id'], ['id'], referent_schema='public')

    with op.batch_alter_table('tenant', schema=None) as batch_op:
        batch_op.drop_column('license_end_date')
        batch_op.drop_column('allowed_monthly_interactions')
        batch_op.drop_column('license_start_date')

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('tenant', schema=None) as batch_op:
        batch_op.add_column(sa.Column('license_start_date', sa.DATE(), autoincrement=False, nullable=True))
        batch_op.add_column(sa.Column('allowed_monthly_interactions', sa.INTEGER(), autoincrement=False, nullable=True))
        batch_op.add_column(sa.Column('license_end_date', sa.DATE(), autoincrement=False, nullable=True))

    with op.batch_alter_table('business_event_log', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_column('license_usage_id')

    op.drop_table('license_usage', schema='public')
    op.drop_table('license', schema='public')
    op.drop_table('license_tier', schema='public')
    # ### end Alembic commands ###
