"""Add Tenable VPR score column for Nessus enrichment

Revision ID: 011
Revises: 010
Create Date: 2026-05-14 17:00:00.000000

Adds ``vulnerabilities.nessus_vpr_score`` — Tenable's proprietary
Vulnerability Priority Rating (0–10) from the Nessus plugin payload.

Stored alongside our own ``priority_score`` so users can compare:
- VulnCheck's contextual priority (CVSS + KEV + EUVD + asset policy + age)
- Tenable's commercial VPR ranking

Idempotent — skips the column add if it already exists.
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


revision = '011'
down_revision = '010'
branch_labels = None
depends_on = None


def upgrade() -> None:
    """Idempotent via Postgres IF NOT EXISTS — survives the case where
    Base.metadata.create_all() created the column on app start before
    alembic could record this migration."""
    op.execute(
        "ALTER TABLE vulnerabilities ADD COLUMN IF NOT EXISTS nessus_vpr_score DOUBLE PRECISION"
    )
    op.execute(
        "CREATE INDEX IF NOT EXISTS ix_vulnerabilities_nessus_vpr_score "
        "ON vulnerabilities (nessus_vpr_score)"
    )


def downgrade() -> None:
    op.execute("DROP INDEX IF EXISTS ix_vulnerabilities_nessus_vpr_score")
    op.execute("ALTER TABLE vulnerabilities DROP COLUMN IF EXISTS nessus_vpr_score")
