#!/usr/bin/env python3
"""
Block provider-comparison topics that generate useless content.

These topics produce articles comparing unnamed "generic providers" (because the
competitor blacklist strips names) without mentioning Parketry. Zero SEO value.

Usage:
    python scripts/block_topics.py          # Preview what would be blocked
    python scripts/block_topics.py --apply  # Actually block the topics
"""

import logging
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent.parent))

from database.connection import test_connection
from database.models import TopicRepository, TopicStatus

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger(__name__)

BLOCK_SLUGS = [
    "die-besten-anpr-anbieter-in-deutschland-2025-marktuebersicht",
    "park-control-parkraum-fair-parken-anbietervergleich-2025",
    "anpr-hardware-im-test-hikvision-axis-genetec-im-vergleich",
    "app-basiertes-parken-vs-anpr-welche-loesung-passt-besser",
    "deutsche-vs-internationale-anpr-anbieter-was-ist-besser",
]

SKIP_REASON = "blocked: provider comparison"


def main():
    apply = "--apply" in sys.argv

    if not test_connection():
        logger.error("Database connection failed")
        return 1

    blocked = 0
    skipped = 0

    for slug in BLOCK_SLUGS:
        topic = TopicRepository.get_by_slug(slug)

        if topic is None:
            logger.warning(f"  NOT FOUND: {slug}")
            continue

        if topic.status != "pending":
            logger.info(f"  SKIP ({topic.status}): {slug}")
            skipped += 1
            continue

        if apply:
            TopicRepository.update_status(topic.id, TopicStatus.SKIPPED, skip_reason=SKIP_REASON)
            logger.info(f"  BLOCKED: {topic.title} (score {topic.priority_score})")
        else:
            logger.info(f"  WOULD BLOCK: {topic.title} (score {topic.priority_score})")

        blocked += 1

    logger.info(f"\n{'Blocked' if apply else 'Would block'}: {blocked}, Already non-pending: {skipped}")

    if not apply and blocked > 0:
        logger.info("Run with --apply to execute")

    return 0


if __name__ == "__main__":
    sys.exit(main())
