from django.core.management.base import BaseCommand

from vrobbler.apps.scrobbles.utils import deduplicate_scrobbles


class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument(
            "--commit",
            action="store_true",
            help="Commit the deletes",
        )
        parser.add_argument(
            "--media-type",
            default=None,
            help="Only dedupe scrobbles of this media type (e.g. Track)",
        )

    def handle(self, *args, **options):
        commit = options["commit"]
        media_type = options["media_type"]
        if not commit:
            self.stdout.write("No changes will be saved, use --commit to save")

        count = deduplicate_scrobbles(commit=commit, media_type=media_type)

        if commit:
            self.stdout.write(
                self.style.SUCCESS(f"Deleted {count} duplicate scrobbles")
            )
        else:
            self.stdout.write(f"Found {count} duplicate scrobbles")
