from django.core.management.base import BaseCommand

from vrobbler.apps.scrobbles.importers import webdav


class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument(
            "--restart",
            action="store_true",
            help="Restart failed imports",
        )
        parser.add_argument(
            "--update-retroarch-hash",
            action="store_true",
            help="Update retroarch files_hash to new ETag-based scheme "
            "without re-importing (migration helper)",
        )
        parser.add_argument(
            "--update-koreader-etag",
            action="store_true",
            help="Store current WebDAV ETag on last KoReader import "
            "without re-importing (migration helper)",
        )
        parser.add_argument(
            "--include-processed",
            action="store_true",
            help="Also import files already moved to processed/ subdirectories "
            "(may produce duplicate scrobbles; use with care on production)",
        )

    def handle(self, *args, **options):
        restart = False
        if options["restart"]:
            restart = True
        update_hash = options.get("update_retroarch_hash", False)
        update_etag = options.get("update_koreader_etag", False)
        include_processed = options.get("include_processed", False)
        webdav.import_from_webdav_for_all_users(
            restart=restart,
            update_retroarch_hash=update_hash,
            update_koreader_etag=update_etag,
            include_processed=include_processed,
        )
