# link_agregator/management/commands/mark_inactive_for_check.py
from django.core.management.base import BaseCommand
from django.utils import timezone
from extractly.models import Ads, AdsManual

class Command(BaseCommand):
    help = "Ustawia check_active=True (i check_active_date=today) dla ogłoszeń z is_active=False."

    def add_arguments(self, parser):
        parser.add_argument(
            "--model",
            choices=["ads", "adsmanual", "all"],
            default="all",
            help="Wybierz model do aktualizacji."
        )
        parser.add_argument(
            "--dry-run",
            action="store_true",
            help="Pokaż ile rekordów zostanie zaktualizowanych, bez zapisu."
        )

    def handle(self, *args, **opts):
        today = timezone.now().date()
        total = 0

        def run_for(model):
            qs = model.objects.filter(is_active=False).exclude(check_active=True)
            count = qs.count()
            if opts["dry_run"]:
                return count
            updated = qs.update(check_active=True, check_active_date=today)
            return updated

        models = []
        if opts["model"] in ("ads", "all"):
            models.append(("Ads", Ads))
        if opts["model"] in ("adsmanual", "all"):
            models.append(("AdsManual", AdsManual))

        for name, M in models:
            num = run_for(M)
            total += num
            self.stdout.write(self.style.SUCCESS(f"{name}: ustawiono check_active dla {num} rekordów"))

        self.stdout.write(self.style.SUCCESS(f"RAZEM: {total}"))
