"""Create base tables for users, decklists, and rooms

Revision ID: 000
Revises: 
Create Date: 2025-12-31 00:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '000'
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:
    """Create base tables: users, decklists, rooms, and supporting tables."""

    # 1. Users Table (matches User model)
    op.create_table(
        'mtgonline_users',
        sa.Column('id', sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column('username', sa.String(64), unique=True, nullable=False, index=True),
        sa.Column('password_hash', sa.String(128), nullable=False),
        sa.Column('salt', sa.String(128), nullable=False),
        sa.Column('email', sa.String(255), nullable=True, index=True),
        sa.Column('country', sa.String(2), nullable=True),
        sa.Column('real_name', sa.String(128), nullable=True),
        sa.Column('avatar_bmp', sa.Text(), nullable=True),
        sa.Column('privlevel', sa.String(50), nullable=True, server_default='User'),
        sa.Column('is_active', sa.Boolean(), default=True),
        sa.Column('is_banned', sa.Boolean(), default=False),
        sa.Column('ban_reason', sa.Text(), nullable=True),
        sa.Column('ban_ends', sa.DateTime(), nullable=True),
        sa.Column('vip_status', sa.Integer(), default=0),
        sa.Column('vip_expiry', sa.DateTime(), nullable=True),
        sa.Column('creation_date', sa.DateTime(), server_default=sa.func.now()),
        sa.Column('last_login', sa.DateTime(), nullable=True),
    )

    # 2. Decklist Folders Table (matches DecklistFolder model)
    op.create_table(
        'mtgonline_decklist_folders',
        sa.Column('id', sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column('owner_id', sa.Integer(), sa.ForeignKey('mtgonline_users.id'), nullable=False),
        sa.Column('name', sa.String(255), nullable=False),
        sa.Column('parent_id', sa.Integer(), sa.ForeignKey('mtgonline_decklist_folders.id'), nullable=True),
        sa.Column('creation_date', sa.DateTime(), server_default=sa.func.now()),
    )

    # 3. Decklist Files Table (matches DecklistFile model)
    op.create_table(
        'mtgonline_decklist_files',
        sa.Column('id', sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column('folder_id', sa.Integer(), sa.ForeignKey('mtgonline_decklist_folders.id'), nullable=True),
        sa.Column('owner_id', sa.Integer(), sa.ForeignKey('mtgonline_users.id'), nullable=False),
        sa.Column('name', sa.String(255), nullable=False),
        sa.Column('content', sa.Text(), nullable=False),
        sa.Column('format', sa.String(50), nullable=True, server_default='native'),
        sa.Column('status', sa.String(20), nullable=True, server_default='DRAUGHT'),
        sa.Column('creation_date', sa.DateTime(), server_default=sa.func.now()),
    )
    op.create_index('idx_decks_owner', 'mtgonline_decklist_files', ['owner_id'])
    op.create_index('idx_decks_folder', 'mtgonline_decklist_files', ['folder_id'])

    # 4. Rooms Table (matches Room model)
    op.create_table(
        'mtgonline_rooms',
        sa.Column('id', sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column('name', sa.String(100), unique=True, nullable=False),
        sa.Column('description', sa.Text(), nullable=True),
        sa.Column('is_password_protected', sa.Boolean(), default=False),
        sa.Column('password_hash', sa.String(128), nullable=True),
        sa.Column('creation_date', sa.DateTime(), server_default=sa.func.now()),
    )

    # 5. Room Game Types Table (matches RoomGameType model)
    op.create_table(
        'mtgonline_rooms_gametypes',
        sa.Column('id', sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column('room_id', sa.Integer(), sa.ForeignKey('mtgonline_rooms.id'), nullable=False),
        sa.Column('name', sa.String(100), nullable=False),
        sa.Column('description', sa.Text(), nullable=True),
    )

    # 6. Bans Table (matches Ban model)
    op.create_table(
        'mtgonline_bans',
        sa.Column('id', sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column('user_id', sa.Integer(), sa.ForeignKey('mtgonline_users.id'), nullable=False),
        sa.Column('server_id', sa.Integer(), nullable=True),
        sa.Column('reason', sa.Text(), nullable=False),
        sa.Column('moderators', sa.String(255), nullable=True),
        sa.Column('ip_address', sa.String(45), nullable=True),
        sa.Column('expiration_time', sa.DateTime(), nullable=True),
        sa.Column('active', sa.Boolean(), default=True),
        sa.Column('creation_date', sa.DateTime(), server_default=sa.func.now()),
    )
    op.create_index('idx_bans_active', 'mtgonline_bans', ['active'])

    # 7. Game Log Table (matches GameLog model)
    op.create_table(
        'mtgonline_log',
        sa.Column('id', sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column('room_id', sa.Integer(), sa.ForeignKey('mtgonline_rooms.id'), nullable=True),
        sa.Column('player_id', sa.Integer(), sa.ForeignKey('mtgonline_users.id'), nullable=True),
        sa.Column('message', sa.Text(), nullable=False),
        sa.Column('timestamp', sa.DateTime(), server_default=sa.func.now()),
    )
    op.create_index('idx_log_timestamp', 'mtgonline_log', ['timestamp'])

    # 8. Audit Log Table (matches AuditLog model)
    op.create_table(
        'mtgonline_audit',
        sa.Column('id', sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column('admin_id', sa.Integer(), sa.ForeignKey('mtgonline_users.id'), nullable=False),
        sa.Column('action_type', sa.String(50), nullable=False),
        sa.Column('target_user_id', sa.Integer(), sa.ForeignKey('mtgonline_users.id'), nullable=True),
        sa.Column('details', sa.Text(), nullable=True),
        sa.Column('ip_address', sa.String(45), nullable=True),
        sa.Column('timestamp', sa.DateTime(), server_default=sa.func.now()),
    )


def downgrade() -> None:
    """Drop base tables in reverse dependency order."""
    op.drop_table('mtgonline_audit')
    op.drop_table('mtgonline_log')
    op.drop_table('mtgonline_bans')
    op.drop_table('mtgonline_rooms_gametypes')
    op.drop_table('mtgonline_rooms')
    op.drop_table('mtgonline_decklist_files')
    op.drop_table('mtgonline_decklist_folders')
    op.drop_table('mtgonline_users')
