"""Rename enum values from uppercase to lowercase and add missing 'deferred'

Revision ID: 006
Revises: 005
Create Date: 2026-02-01 13:00:00.000000

Strategy: Drop old enum types, recreate with correct lowercase values,
and convert existing columns. This is the only reliable way to handle
enum changes in PostgreSQL within Alembic transactions.
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    # ========================================
    # VulnerabilityStatus
    # ========================================
    # 1. Convert column to VARCHAR temporarily
    op.execute("ALTER TABLE vulnerabilities ALTER COLUMN status TYPE VARCHAR(50) USING status::text")

    # 2. Lowercase all existing values
    op.execute("UPDATE vulnerabilities SET status = LOWER(status)")

    # 3. Drop old enum type
    op.execute("DROP TYPE IF EXISTS vulnerabilitystatus")

    # 4. Create new enum with all lowercase values (including 'deferred' which was missing)
    op.execute("""
        CREATE TYPE vulnerabilitystatus AS ENUM (
            'open', 'patched', 'pending_verification', 'patch_failed',
            'accepted_risk', 'false_positive', 'deferred'
        )
    """)

    # 5. Convert column back to enum
    op.execute("ALTER TABLE vulnerabilities ALTER COLUMN status TYPE vulnerabilitystatus USING status::vulnerabilitystatus")

    # ========================================
    # VulnerabilitySeverity
    # ========================================
    # 1. Convert column to VARCHAR temporarily
    op.execute("ALTER TABLE vulnerabilities ALTER COLUMN severity TYPE VARCHAR(50) USING severity::text")

    # 2. Lowercase all existing values
    op.execute("UPDATE vulnerabilities SET severity = LOWER(severity)")

    # 3. Drop old enum type
    op.execute("DROP TYPE IF EXISTS vulnerabilityseverity")

    # 4. Create new enum with lowercase values
    op.execute("""
        CREATE TYPE vulnerabilityseverity AS ENUM (
            'critical', 'high', 'medium', 'low', 'none'
        )
    """)

    # 5. Convert column back to enum
    op.execute("ALTER TABLE vulnerabilities ALTER COLUMN severity TYPE vulnerabilityseverity USING severity::vulnerabilityseverity")


def downgrade() -> None:
    # VulnerabilityStatus -> UPPERCASE
    op.execute("ALTER TABLE vulnerabilities ALTER COLUMN status TYPE VARCHAR(50) USING status::text")
    op.execute("UPDATE vulnerabilities SET status = UPPER(status)")
    op.execute("DROP TYPE IF EXISTS vulnerabilitystatus")
    op.execute("""
        CREATE TYPE vulnerabilitystatus AS ENUM (
            'OPEN', 'PATCHED', 'PENDING_VERIFICATION', 'PATCH_FAILED',
            'ACCEPTED_RISK', 'FALSE_POSITIVE'
        )
    """)
    op.execute("DELETE FROM vulnerabilities WHERE status = 'DEFERRED'")
    op.execute("ALTER TABLE vulnerabilities ALTER COLUMN status TYPE vulnerabilitystatus USING status::vulnerabilitystatus")

    # VulnerabilitySeverity -> UPPERCASE
    op.execute("ALTER TABLE vulnerabilities ALTER COLUMN severity TYPE VARCHAR(50) USING severity::text")
    op.execute("UPDATE vulnerabilities SET severity = UPPER(severity)")
    op.execute("DROP TYPE IF EXISTS vulnerabilityseverity")
    op.execute("""
        CREATE TYPE vulnerabilityseverity AS ENUM (
            'CRITICAL', 'HIGH', 'MEDIUM', 'LOW', 'NONE'
        )
    """)
    op.execute("ALTER TABLE vulnerabilities ALTER COLUMN severity TYPE vulnerabilityseverity USING severity::vulnerabilityseverity")
