from django.core.management.base import BaseCommand
from workouts.importer import import_wrkout_exercises


class Command(BaseCommand):
    help = "Import the exercise catalog from wrkout/exercises.json"

    def add_arguments(self, parser):
        parser.add_argument(
            "--tarball",
            dest="tarball",
            default=None,
            help="Path to a local exercises.json tarball/zip to import from",
        )
        parser.add_argument(
            "--no-images",
            action="store_true",
            help="Do not download exercise images",
        )
        parser.add_argument(
            "--dry-run",
            action="store_true",
            help="Parse the catalog but do not write anything to the database",
        )

    def handle(self, *args, **options):
        result = import_wrkout_exercises(
            tarball_path=options.get("tarball"),
            import_images=not options.get("no_images"),
            dry_run=options.get("dry_run"),
        )
        self.stdout.write(
            self.style.SUCCESS(
                f"Import finished: {result['created']} created, "
                f"{result['updated']} updated, {result['skipped']} skipped, "
                f"{result['image_failures']} image failures"
            )
        )
