"""PC access protocols: catalog + per-PC links, retire isvnc/iswinrm

Adds an admin-managed protocol catalog (accessprotocols) and a per-PC link
table (computeraccess). Seeds VNC/WinRM/RDP, migrates each PC's isvnc/iswinrm
into computeraccess rows, then drops the two boolean columns.

Revision ID: 7d05_pc_access_protocols
Revises: 7d04_notificationtype_display
Create Date: 2026-07-08

"""
from alembic import op
import sqlalchemy as sa


revision = '7d05_pc_access_protocols'
down_revision = '7d04_notificationtype_display'
branch_labels = None
depends_on = None


def upgrade():
    op.create_table(
        'accessprotocols',
        sa.Column('protocolid', sa.Integer(), primary_key=True),
        sa.Column('name', sa.String(length=50), nullable=False),
        sa.Column('scheme', sa.String(length=20), nullable=False),
        sa.Column('defaultport', sa.Integer(), nullable=True),
        sa.Column('linktemplate', sa.String(length=255), nullable=False),
        sa.Column('isactive', sa.Boolean(), nullable=False, server_default='1'),
        sa.UniqueConstraint('name', name='uq_accessprotocol_name'),
    )
    op.create_table(
        'computeraccess',
        sa.Column('id', sa.Integer(), primary_key=True),
        sa.Column('computerid', sa.Integer(), nullable=False),
        sa.Column('protocolid', sa.Integer(), nullable=False),
        sa.Column('portoverride', sa.Integer(), nullable=True),
        sa.Column('isactive', sa.Boolean(), nullable=False, server_default='1'),
        sa.ForeignKeyConstraint(['computerid'], ['computers.computerid'], ondelete='CASCADE'),
        sa.ForeignKeyConstraint(['protocolid'], ['accessprotocols.protocolid']),
        sa.UniqueConstraint('computerid', 'protocolid', name='uq_computer_protocol'),
    )
    op.create_index('idx_compaccess_computer', 'computeraccess', ['computerid'])

    conn = op.get_bind()
    # Seed the protocol catalog. exec_driver_sql so the ':' / '{}' in the
    # templates are not parsed as bind params.
    conn.exec_driver_sql(
        "INSERT INTO accessprotocols (name, scheme, defaultport, linktemplate, isactive) VALUES "
        "('VNC','vnc',5900,'vnc://{host}:{port}',1),"
        "('WinRM','https',5986,'https://{host}:{port}/wsman',1),"
        "('RDP','rdp',3389,'rdp://{host}:{port}',1)"
    )
    # Migrate the old booleans into per-PC access rows.
    conn.exec_driver_sql(
        "INSERT INTO computeraccess (computerid, protocolid, isactive) "
        "SELECT c.computerid, p.protocolid, 1 FROM computers c "
        "JOIN accessprotocols p ON p.name='VNC' WHERE c.isvnc = 1"
    )
    conn.exec_driver_sql(
        "INSERT INTO computeraccess (computerid, protocolid, isactive) "
        "SELECT c.computerid, p.protocolid, 1 FROM computers c "
        "JOIN accessprotocols p ON p.name='WinRM' WHERE c.iswinrm = 1"
    )

    op.drop_column('computers', 'isvnc')
    op.drop_column('computers', 'iswinrm')


def downgrade():
    op.add_column('computers', sa.Column('iswinrm', sa.Boolean(), nullable=True))
    op.add_column('computers', sa.Column('isvnc', sa.Boolean(), nullable=True))
    op.drop_index('idx_compaccess_computer', table_name='computeraccess')
    op.drop_table('computeraccess')
    op.drop_table('accessprotocols')
