"""Add step_categories table for step library organization

Revision ID: 007
Revises: 006
Create Date: 2026-02-03

"""
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 = '007'
down_revision: Union[str, None] = '006'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ===========================================
    # Create step_categories table
    # ===========================================
    op.create_table(
        'step_categories',
        sa.Column('id', postgresql.UUID(as_uuid=True), primary_key=True, server_default=sa.text('gen_random_uuid()')),
        sa.Column('name', sa.String(100), nullable=False),
        sa.Column('slug', sa.String(100), nullable=False),
        sa.Column('description', sa.Text(), nullable=True),
        # Team scoping (NULL = global, like TreeCategory)
        sa.Column('team_id', postgresql.UUID(as_uuid=True), sa.ForeignKey('teams.id', ondelete='CASCADE'), nullable=True),
        sa.Column('display_order', sa.Integer(), nullable=False, server_default='0'),
        sa.Column('is_active', sa.Boolean(), nullable=False, server_default='true'),
        sa.Column('created_by', postgresql.UUID(as_uuid=True), sa.ForeignKey('users.id', ondelete='SET NULL'), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('now()')),
        sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('now()')),
        sa.UniqueConstraint('slug', 'team_id', name='uq_step_categories_slug_team')
    )
    op.create_index('ix_step_categories_team_id', 'step_categories', ['team_id'])
    op.create_index('ix_step_categories_slug', 'step_categories', ['slug'])
    op.create_index('ix_step_categories_display_order', 'step_categories', ['display_order'])

    # ===========================================
    # Seed default GLOBAL categories (team_id = NULL)
    # ===========================================
    op.execute("""
        INSERT INTO step_categories (name, slug, description, display_order, team_id) VALUES
        ('Citrix / VDI', 'citrix-vdi', 'Virtual desktop, XenApp, XenDesktop, VDA issues', 1, NULL),
        ('Active Directory', 'active-directory', 'AD, LDAP, Group Policy, authentication', 2, NULL),
        ('Microsoft 365', 'microsoft-365', 'Exchange Online, Teams, SharePoint, OneDrive', 3, NULL),
        ('Networking', 'networking', 'DNS, DHCP, VPN, firewall, connectivity', 4, NULL),
        ('File Services', 'file-services', 'File shares, permissions, DFS, storage', 5, NULL),
        ('Printing', 'printing', 'Print servers, drivers, spooler issues', 6, NULL),
        ('Backup & Recovery', 'backup-recovery', 'Backup software, disaster recovery, restore', 7, NULL),
        ('Security', 'security', 'Antivirus, permissions, security incidents', 8, NULL),
        ('Hardware', 'hardware', 'Servers, workstations, peripherals', 9, NULL),
        ('Other', 'other', 'Miscellaneous steps', 100, NULL)
    """)


def downgrade() -> None:
    op.drop_index('ix_step_categories_display_order', table_name='step_categories')
    op.drop_index('ix_step_categories_slug', table_name='step_categories')
    op.drop_index('ix_step_categories_team_id', table_name='step_categories')
    op.drop_table('step_categories')
