from django.core.management.base import BaseCommand
from extractly.models import RawMonitoredLink

class Command(BaseCommand):
    help = "Wyświetla liczbę wszystkich oraz unikalnych linków po source.name"

    def handle(self, *args, **options):
        from django.db.models import Count

        sources = (
            RawMonitoredLink.objects
            .values('source__name')
            .annotate(
                total=Count('id'),
                unique=Count('url', distinct=True)
            )
            .order_by('source__name')
        )

        print(f"{'Źródło':40} {'Wszystkie':10} {'Unikalne':10}")
        print("="*65)
        for row in sources:
            print(f"{row['source__name'][:38]:40} {row['total']:10} {row['unique']:10}")
