import logging

from django.core.management.base import BaseCommand
from django.db.models import Count, Max

logger = logging.getLogger(__name__)

MEDIA_FIELDS = [
    "artist",
    "album",
    "track",
    "tv_series",
    "video",
    "podcast",
    "podcast_episode",
    "board_game",
    "trail",
    "geo_location",
    "food",
    "book",
]


class Command(BaseCommand):
    help = "Clean up duplicate ChartRecords"

    def add_arguments(self, parser):
        parser.add_argument(
            "--dry-run",
            action="store_true",
            help="Show what would be deleted without actually deleting",
        )

    def handle(self, *args, **options):
        from charts.models import ChartRecord

        dry_run = options["dry_run"]

        total_deleted = 0

        for media_field in MEDIA_FIELDS:
            field_id = f"{media_field}_id"

            duplicates = (
                ChartRecord.objects.filter(**{f"{field_id}__isnull": False})
                .values("user_id", "year", "month", "week", "day", field_id)
                .annotate(
                    count=Count("id"),
                    max_id=Max("id"),
                )
                .filter(count__gt=1)
            )

            for dup in duplicates:
                max_id = dup["max_id"]
                to_delete = list(
                    ChartRecord.objects.filter(
                        user_id=dup["user_id"],
                        year=dup["year"],
                        month=dup["month"],
                        week=dup["week"],
                        day=dup["day"],
                        **{field_id: dup[field_id]},
                    )
                    .exclude(id=max_id)
                    .values_list("id", flat=True)
                )

                if dry_run:
                    self.stdout.write(
                        f"Would delete {len(to_delete)} duplicate ChartRecords "
                        f"({media_field}, user={dup['user_id']}, year={dup['year']}, "
                        f"month={dup['month']}, week={dup['week']}, day={dup['day']})"
                    )
                else:
                    deleted, _ = ChartRecord.objects.filter(id__in=to_delete).delete()
                    total_deleted += deleted
                    self.stdout.write(
                        f"Deleted {deleted} duplicate ChartRecords "
                        f"({media_field}, user={dup['user_id']}, year={dup['year']}, "
                        f"month={dup['month']}, week={dup['week']}, day={dup['day']})"
                    )

        if dry_run:
            self.stdout.write(self.style.WARNING("Dry run complete - no changes made"))
        else:
            self.stdout.write(self.style.SUCCESS(f"Total deleted: {total_deleted}"))
