"""create users table

Revision ID: e4f337a1c60e
Revises: 
Create Date: 2025-05-24 01:57:11.465196

"""
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 = 'e4f337a1c60e'
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:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        'users',
        sa.Column('id', sa.Integer(), primary_key=True),
        sa.Column('telegram_id', sa.BigInteger(), unique=True, nullable=False),
        sa.Column('username', sa.Text()),
        sa.Column('balance', sa.Numeric(12, 2), server_default='0'),
        sa.Column('is_admin', sa.Boolean(), server_default=sa.text('false')),
        sa.Column('created_at', sa.TIMESTAMP(), server_default=sa.text('now()'))
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('users', sa.Column('ref_income', sa.INTEGER(), server_default=sa.text('0'), autoincrement=False, nullable=False))
    op.add_column('users', sa.Column('invited_id', sa.BIGINT(), autoincrement=False, nullable=True))
    op.add_column('users', sa.Column('active_promo', sa.VARCHAR(length=50), autoincrement=False, nullable=True))
    op.add_column('users', sa.Column('discount_expires_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=True))
    op.add_column('users', sa.Column('tg_id', sa.BIGINT(), autoincrement=True, nullable=False))
    op.add_column('users', sa.Column('discount_uses_left', sa.INTEGER(), autoincrement=False, nullable=True))
    op.add_column('users', sa.Column('promo_discount', sa.INTEGER(), autoincrement=False, nullable=True))
    op.add_column('users', sa.Column('used_promo_codes', postgresql.ARRAY(sa.VARCHAR()), autoincrement=False, nullable=True))
    op.add_column('users', sa.Column('language', postgresql.ENUM('ru', 'en', name='language'), autoincrement=False, nullable=True))
    op.add_column('users', sa.Column('active_discount', sa.DOUBLE_PRECISION(precision=53), autoincrement=False, nullable=True))
    op.create_foreign_key(op.f('users_invited_id_fkey'), 'users', 'users', ['invited_id'], ['tg_id'])
    op.drop_constraint(None, 'users', type_='unique')
    op.alter_column('users', 'created_at',
               existing_type=sa.TIMESTAMP(),
               type_=postgresql.TIMESTAMP(timezone=True),
               nullable=False,
               existing_server_default=sa.text('now()'))
    op.alter_column('users', 'is_admin',
               existing_type=sa.BOOLEAN(),
               nullable=False,
               existing_server_default=sa.text('false'))
    op.alter_column('users', 'balance',
               existing_type=sa.Numeric(precision=12, scale=2),
               type_=sa.INTEGER(),
               nullable=False,
               existing_server_default=sa.text('0'))
    op.alter_column('users', 'username',
               existing_type=sa.Text(),
               type_=sa.VARCHAR(length=32),
               existing_nullable=True)
    op.drop_column('users', 'telegram_id')
    op.drop_column('users', 'id')
    op.create_table('payments',
    sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
    sa.Column('is_payed', sa.BOOLEAN(), server_default=sa.text('false'), autoincrement=False, nullable=False),
    sa.Column('user_id', sa.BIGINT(), autoincrement=False, nullable=False),
    sa.Column('amount', sa.INTEGER(), server_default=sa.text('0'), autoincrement=False, nullable=False),
    sa.Column('created_at', postgresql.TIMESTAMP(timezone=True), server_default=sa.text('now()'), autoincrement=False, nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['users.tg_id'], name=op.f('payments_user_id_fkey')),
    sa.PrimaryKeyConstraint('id', name=op.f('payments_pkey'))
    )
    op.create_table('domains',
    sa.Column('id', sa.INTEGER(), server_default=sa.text("nextval('domains_id_seq'::regclass)"), autoincrement=True, nullable=False),
    sa.Column('name', sa.VARCHAR(), autoincrement=False, nullable=False),
    sa.Column('owner_id', sa.BIGINT(), autoincrement=False, nullable=False),
    sa.Column('expiration', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=False),
    sa.Column('created_at', postgresql.TIMESTAMP(timezone=True), server_default=sa.text('now()'), autoincrement=False, nullable=False),
    sa.ForeignKeyConstraint(['owner_id'], ['users.tg_id'], name='domains_owner_id_fkey'),
    sa.PrimaryKeyConstraint('id', name='domains_pkey'),
    postgresql_ignore_search_path=False
    )
    op.create_table('cloudflare_configs',
    sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
    sa.Column('domain_id', sa.INTEGER(), autoincrement=False, nullable=False),
    sa.Column('zone_id', sa.VARCHAR(length=32), autoincrement=False, nullable=False),
    sa.Column('is_active', sa.BOOLEAN(), autoincrement=False, nullable=False),
    sa.Column('ip_address', sa.VARCHAR(length=45), autoincrement=False, nullable=False),
    sa.Column('created_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=False),
    sa.ForeignKeyConstraint(['domain_id'], ['domains.id'], name=op.f('cloudflare_configs_domain_id_fkey')),
    sa.PrimaryKeyConstraint('id', name=op.f('cloudflare_configs_pkey'))
    )
    op.create_table('promo_codes',
    sa.Column('id', sa.INTEGER(), server_default=sa.text("nextval('promo_codes_id_seq'::regclass)"), autoincrement=True, nullable=False),
    sa.Column('code', sa.VARCHAR(length=50), autoincrement=False, nullable=False),
    sa.Column('promo_type', sa.VARCHAR(length=20), autoincrement=False, nullable=False),
    sa.Column('value', sa.DOUBLE_PRECISION(precision=53), autoincrement=False, nullable=False),
    sa.Column('max_uses', sa.INTEGER(), autoincrement=False, nullable=False),
    sa.Column('current_uses', sa.INTEGER(), server_default=sa.text('0'), autoincrement=False, nullable=False),
    sa.Column('created_at', postgresql.TIMESTAMP(), server_default=sa.text('CURRENT_TIMESTAMP'), autoincrement=False, nullable=False),
    sa.Column('expires_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
    sa.Column('is_active', sa.BOOLEAN(), server_default=sa.text('true'), autoincrement=False, nullable=False),
    sa.Column('meta', postgresql.JSON(astext_type=sa.Text()), autoincrement=False, nullable=True),
    sa.PrimaryKeyConstraint('id', name='promo_codes_pkey'),
    postgresql_ignore_search_path=False
    )
    op.create_index(op.f('ix_promo_codes_code'), 'promo_codes', ['code'], unique=True)
    op.create_table('promo_history',
    sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
    sa.Column('user_id', sa.BIGINT(), autoincrement=False, nullable=False),
    sa.Column('promo_code_id', sa.INTEGER(), autoincrement=False, nullable=False),
    sa.Column('used_at', postgresql.TIMESTAMP(), server_default=sa.text('CURRENT_TIMESTAMP'), autoincrement=False, nullable=False),
    sa.Column('created_at', postgresql.TIMESTAMP(timezone=True), server_default=sa.text('now()'), autoincrement=False, nullable=False),
    sa.ForeignKeyConstraint(['promo_code_id'], ['promo_codes.id'], name=op.f('promo_history_promo_code_id_fkey')),
    sa.PrimaryKeyConstraint('id', name=op.f('promo_history_pkey'))
    )
    op.create_index(op.f('ix_promo_history_user_id'), 'promo_history', ['user_id'], unique=False)
    # ### end Alembic commands ###
