"""This module holds the command to import users from saba

See also
--------
``mtp.custom.nissan.apps.import_export.saba.UserImporter``

"""

import os

from django.core.management.base import BaseCommand, CommandError

from mtp.custom.nissan.apps.import_export.saba import UserImporter


class Command(BaseCommand):
    """Django command to import users from a csv file issued by the Saba LMS"""

    help = 'Import users from a provided csv file issued by the Saba LMS'

    def add_arguments(self, parser):
        """Add the file argument"""
        parser.add_argument('filepath', type=str)

    def handle(self, *args, **options):
        """Execute the command with the provided arguments"""

        if not os.path.exists(options['filepath']):
            raise CommandError('The provided file does not exists')

        # At the moment, we don't want to create the groups for the users, so
        # we monkey-patch the ``_add_user_to_group`` method.
        def _add_user_to_group(user, group_name, group_type_name, cache_key):
            return

        user_importer = UserImporter(file_path=options['filepath'])
        user_importer._add_user_to_group = _add_user_to_group

        user_importer.import_users()
