"""initial_revisions

Revision ID: 1
Revises:
Create Date: 2025-06-27 13:26:25.896838

"""

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from openvair.common.orm_types import PathType

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


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        'interfaces',
        sa.Column('id', sa.UUID(), nullable=False),
        sa.Column('name', sa.String(length=30), nullable=True),
        sa.Column('mac', sa.String(length=20), nullable=True),
        sa.Column('ip', sa.String(length=40), nullable=True),
        sa.Column('netmask', sa.Integer(), nullable=True),
        sa.Column('gateway', sa.String(length=40), nullable=True),
        sa.Column('inf_type', sa.String(length=20), nullable=True),
        sa.Column('mtu', sa.Integer(), nullable=True),
        sa.Column('speed', sa.Integer(), nullable=True),
        sa.Column('power_state', sa.String(length=20), nullable=False),
        sa.Column('status', sa.String(length=20), nullable=False),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_table(
        'interface_extra_specs',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('key', sa.String(length=60), nullable=True),
        sa.Column('value', sa.String(length=155), nullable=True),
        sa.Column('interface_id', sa.UUID(), nullable=True),
        sa.ForeignKeyConstraint(
            ['interface_id'],
            ['interfaces.id'],
        ),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_table(
        'storages',
        sa.Column('id', sa.UUID(), nullable=False),
        sa.Column('name', sa.String(length=60), nullable=True),
        sa.Column('description', sa.String(length=255), nullable=True),
        sa.Column('storage_type', sa.String(length=30), nullable=False),
        sa.Column('initialized', sa.Boolean(), nullable=True),
        sa.Column('status', sa.String(length=30), nullable=False),
        sa.Column('information', sa.Text(), nullable=True),
        sa.Column('size', sa.BigInteger(), nullable=True),
        sa.Column('available', sa.BigInteger(), nullable=True),
        sa.Column('user_id', sa.UUID(), nullable=True),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_table(
        'storage_extra_specs',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('key', sa.String(length=60), nullable=True),
        sa.Column('value', sa.String(length=155), nullable=True),
        sa.Column('storage_id', sa.UUID(), nullable=True),
        sa.ForeignKeyConstraint(
            ['storage_id'],
            ['storages.id'],
        ),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_table(
        'users',
        sa.Column('id', sa.UUID(), nullable=False),
        sa.Column('username', sa.String(length=30), nullable=True),
        sa.Column('email', sa.String(length=255), nullable=True),
        sa.Column('is_superuser', sa.Boolean(), nullable=True),
        sa.Column('hashed_password', sa.String(length=255), nullable=True),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('email'),
        sa.UniqueConstraint('username'),
    )
    op.create_table(
        'volumes',
        sa.Column('id', sa.UUID(), nullable=False),
        sa.Column('name', sa.String(length=40), nullable=True),
        sa.Column('user_id', sa.UUID(), nullable=True),
        sa.Column('format', sa.String(length=10), nullable=True),
        sa.Column('size', sa.BigInteger(), nullable=True),
        sa.Column('used', sa.BigInteger(), nullable=True),
        sa.Column('status', sa.String(length=20), nullable=True),
        sa.Column('information', sa.Text(), nullable=True),
        sa.Column('path', sa.String(length=255), nullable=True),
        sa.Column('template_id', sa.UUID(), nullable=True),
        sa.Column('description', sa.String(length=255), nullable=True),
        sa.Column('storage_id', sa.UUID(), nullable=True),
        sa.Column('storage_type', sa.String(length=30), nullable=True),
        sa.Column('read_only', sa.Boolean(), nullable=True),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_table(
        'volume_attach_vm',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('volume_id', sa.UUID(), nullable=True),
        sa.Column('vm_id', sa.UUID(), nullable=True),
        sa.Column('user_id', sa.UUID(), nullable=True),
        sa.Column('target', sa.String(length=50), nullable=True),
        sa.ForeignKeyConstraint(
            ['volume_id'],
            ['volumes.id'],
        ),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_table(
        'virtual_machines',
        sa.Column('id', sa.UUID(), nullable=False),
        sa.Column('power_state', sa.String(length=30), nullable=True),
        sa.Column('status', sa.String(length=30), nullable=True),
        sa.Column('name', sa.String(length=60), nullable=True),
        sa.Column('description', sa.String(length=255), nullable=True),
        sa.Column('information', sa.Text(), nullable=True),
        sa.Column('user_id', sa.UUID(), nullable=True),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_table(
        'cpu_info',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('cores', sa.Integer(), nullable=True),
        sa.Column('threads', sa.Integer(), nullable=True),
        sa.Column('sockets', sa.Integer(), nullable=True),
        sa.Column('type', sa.String(length=30), nullable=True),
        sa.Column('model', sa.String(length=30), nullable=True),
        sa.Column('vm_id', sa.UUID(), nullable=True),
        sa.Column('vcpu', sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(
            ['vm_id'],
            ['virtual_machines.id'],
        ),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_table(
        'disk',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('name', sa.String(length=255), nullable=True),
        sa.Column('emulation', sa.String(length=30), nullable=True),
        sa.Column('format', sa.String(length=20), nullable=True),
        sa.Column('qos', postgresql.JSON(astext_type=sa.Text()), nullable=True),
        sa.Column('boot_order', sa.Integer(), nullable=True),
        sa.Column('path', sa.String(length=255), nullable=True),
        sa.Column('size', sa.BigInteger(), nullable=True),
        sa.Column('provisioning', sa.String(length=30), nullable=True),
        sa.Column('type', sa.String(length=30), nullable=True),
        sa.Column('order', sa.Integer(), nullable=True),
        sa.Column('vm_id', sa.UUID(), nullable=True),
        sa.Column('disk_id', sa.UUID(), nullable=True),
        sa.Column('read_only', sa.Boolean(), nullable=True),
        sa.ForeignKeyConstraint(
            ['vm_id'],
            ['virtual_machines.id'],
        ),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_table(
        'os',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('os_type', sa.String(length=80), nullable=True),
        sa.Column('os_variant', sa.String(length=255), nullable=True),
        sa.Column('boot_device', sa.String(length=10), nullable=True),
        sa.Column('graphic_driver', sa.String(length=30), nullable=True),
        sa.Column('bios', sa.String(length=30), nullable=True),
        sa.Column('vm_id', sa.UUID(), nullable=True),
        sa.ForeignKeyConstraint(
            ['vm_id'],
            ['virtual_machines.id'],
        ),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_table(
        'protocol_graphic_interface',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('login', sa.String(length=90), nullable=True),
        sa.Column('password', sa.String(length=255), nullable=True),
        sa.Column('connect_type', sa.String(length=30), nullable=True),
        sa.Column('url', sa.String(length=255), nullable=True),
        sa.Column('vm_id', sa.UUID(), nullable=True),
        sa.ForeignKeyConstraint(
            ['vm_id'],
            ['virtual_machines.id'],
        ),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_table(
        'ram',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('size', sa.BigInteger(), nullable=True),
        sa.Column('vm_id', sa.UUID(), nullable=True),
        sa.ForeignKeyConstraint(
            ['vm_id'],
            ['virtual_machines.id'],
        ),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_table(
        'snapshots',
        sa.Column('id', sa.UUID(), nullable=False),
        sa.Column('vm_id', sa.UUID(), nullable=False),
        sa.Column('name', sa.String(length=60), nullable=False),
        sa.Column('parent_id', sa.UUID(), nullable=True),
        sa.Column('description', sa.String(length=255), nullable=True),
        sa.Column('status', sa.String(length=30), nullable=False),
        sa.Column('created_at', sa.DateTime(), nullable=False),
        sa.Column('is_current', sa.Boolean(), nullable=False),
        sa.ForeignKeyConstraint(
            ['parent_id'],
            ['snapshots.id'],
        ),
        sa.ForeignKeyConstraint(
            ['vm_id'],
            ['virtual_machines.id'],
        ),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(
        op.f('ix_snapshots_is_current'),
        'snapshots',
        ['is_current'],
        unique=False,
    )
    op.create_table(
        'virtual_interface',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('interface', sa.String(length=60), nullable=True),
        sa.Column('mac', sa.String(length=20), nullable=True),
        sa.Column('mode', sa.String(length=30), nullable=True),
        sa.Column('portgroup', sa.String(length=30), nullable=True),
        sa.Column('model', sa.String(length=30), nullable=True),
        sa.Column('order', sa.Integer(), nullable=True),
        sa.Column('vm_id', sa.UUID(), nullable=True),
        sa.ForeignKeyConstraint(
            ['vm_id'],
            ['virtual_machines.id'],
        ),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_table(
        'images',
        sa.Column('id', sa.UUID(), nullable=False),
        sa.Column('name', sa.String(length=40), nullable=True),
        sa.Column('user_id', sa.UUID(), nullable=True),
        sa.Column('size', sa.BigInteger(), nullable=True),
        sa.Column('path', sa.String(length=255), nullable=True),
        sa.Column('status', sa.String(length=20), nullable=True),
        sa.Column('information', sa.Text(), nullable=True),
        sa.Column('description', sa.String(length=255), nullable=True),
        sa.Column('storage_id', sa.UUID(), nullable=True),
        sa.Column('storage_type', sa.String(length=30), nullable=True),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_table(
        'image_attach_vm',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('image_id', sa.UUID(), nullable=True),
        sa.Column('vm_id', sa.UUID(), nullable=True),
        sa.Column('user_id', sa.UUID(), nullable=True),
        sa.Column('target', sa.String(length=50), nullable=True),
        sa.ForeignKeyConstraint(
            ['image_id'],
            ['images.id'],
        ),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_table(
        'events',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('module', sa.String(length=40), nullable=True),
        sa.Column('object_id', sa.UUID(), nullable=True),
        sa.Column('user_id', sa.UUID(), nullable=True),
        sa.Column('event', sa.String(length=50), nullable=True),
        sa.Column('timestamp', sa.DateTime(), nullable=True),
        sa.Column('information', sa.Text(), nullable=True),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_table(
        'notifications',
        sa.Column('id', sa.UUID(), nullable=False),
        sa.Column('create_datetime', sa.TIMESTAMP(), nullable=True),
        sa.Column('msg_type', sa.String(length=30), nullable=True),
        sa.Column('subject', sa.String(length=30), nullable=True),
        sa.Column('recipients', sa.ARRAY(sa.String()), nullable=True),
        sa.Column('message', sa.String(length=255), nullable=True),
        sa.Column('status', sa.String(length=255), nullable=True),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_table(
        'virtual_networks',
        sa.Column('id', sa.UUID(), nullable=False),
        sa.Column('network_name', sa.String(), nullable=True),
        sa.Column('forward_mode', sa.String(), nullable=True),
        sa.Column('bridge', sa.String(), nullable=True),
        sa.Column('virtual_port_type', sa.String(), nullable=True),
        sa.Column('state', sa.String(), nullable=True),
        sa.Column('autostart', sa.String(), nullable=True),
        sa.Column('persistent', sa.String(), nullable=True),
        sa.Column('virsh_xml', sa.Text(), nullable=True),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_table(
        'virtual_network_port_groups',
        sa.Column('port_group_name', sa.String(), nullable=False),
        sa.Column('is_trunk', sa.String(), nullable=True),
        sa.Column('tags', sa.ARRAY(sa.Integer()), nullable=True),
        sa.Column('virtual_network_id', sa.UUID(), nullable=False),
        sa.ForeignKeyConstraint(
            ['virtual_network_id'],
            ['virtual_networks.id'],
        ),
        sa.PrimaryKeyConstraint('port_group_name', 'virtual_network_id'),
    )
    op.create_table(
        'iscsi_interfaces',
        sa.Column('id', sa.UUID(), nullable=False),
        sa.Column('inf_type', sa.String(length=20), nullable=True),
        sa.Column('ip', sa.String(length=40), nullable=True),
        sa.Column('port', sa.String(length=20), nullable=True),
        sa.Column('status', sa.String(length=20), nullable=True),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('ip'),
    )
    op.create_table(
        'templates',
        sa.Column('id', sa.UUID(), nullable=False),
        sa.Column('name', sa.String(length=40), nullable=False),
        sa.Column('description', sa.Text(), nullable=True),
        sa.Column('path', PathType(), nullable=False),
        sa.Column('tmp_format', sa.String(length=10), nullable=False),
        sa.Column('size', sa.BigInteger(), nullable=False),
        sa.Column('storage_id', sa.String(length=36), nullable=False),
        sa.Column(
            'status',
            sa.Enum(
                'NEW',
                'CREATING',
                'EDITING',
                'AVAILABLE',
                'ERROR',
                'DELETING',
                name='template_status',
            ),
            nullable=False,
        ),
        sa.Column('information', sa.Text(), nullable=True),
        sa.Column('is_backing', sa.Boolean(), nullable=False),
        sa.Column('created_at', sa.DateTime(), nullable=False),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('name'),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('templates')
    op.drop_table('iscsi_interfaces')
    op.drop_table('virtual_network_port_groups')
    op.drop_table('virtual_networks')
    op.drop_table('notifications')
    op.drop_table('events')
    op.drop_table('image_attach_vm')
    op.drop_table('images')
    op.drop_table('virtual_interface')
    op.drop_index(op.f('ix_snapshots_is_current'), table_name='snapshots')
    op.drop_table('snapshots')
    op.drop_table('ram')
    op.drop_table('protocol_graphic_interface')
    op.drop_table('os')
    op.drop_table('disk')
    op.drop_table('cpu_info')
    op.drop_table('virtual_machines')
    op.drop_table('volume_attach_vm')
    op.drop_table('volumes')
    op.drop_table('users')
    op.drop_table('storage_extra_specs')
    op.drop_table('storages')
    op.drop_table('interface_extra_specs')
    op.drop_table('interfaces')
    # ### end Alembic commands ###
