"""Allow monitoring_type = 'related' for auto-created entities.

When we parse a publication and find a sócio whose NIF starts with 5
(NIPC — legal entity), we want to auto-register that entity as a
company in the companies table so the UI can link to it. These auto-
created companies get monitoring_type='related' to distinguish them
from user-tracked internal/competitor ones.
"""
from alembic import op

revision = "0017"
down_revision = "0016"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.execute(
        """
        ALTER TABLE companies
          DROP CONSTRAINT IF EXISTS companies_monitoring_type_check;
        ALTER TABLE companies
          ADD CONSTRAINT companies_monitoring_type_check
          CHECK (monitoring_type = ANY (ARRAY['internal','competitor','analysis','related']));
        """
    )


def downgrade() -> None:
    op.execute(
        """
        ALTER TABLE companies
          DROP CONSTRAINT IF EXISTS companies_monitoring_type_check;
        ALTER TABLE companies
          ADD CONSTRAINT companies_monitoring_type_check
          CHECK (monitoring_type = ANY (ARRAY['internal','competitor','analysis']));
        """
    )
