import re

from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import User


class Command(BaseCommand):
    help = 'Add a procat2 user'

    def add_arguments(self, parser):
        parser.add_argument('first_name', type=str)
        parser.add_argument('last_name', type=str)
        parser.add_argument('email', type=str)

    def handle(self, *args, **options):
        fname = options['first_name']
        lname = options['last_name']
        email = options['email']
        email = re.sub('[<>]', '', email)

        uname = str(lname[:5] + fname[:3]).lower()
        pw = str(fname[0] + lname[0] + 'visual').lower()

        user = User.objects.create_user(uname, password=pw)
        user.first_name = fname
        user.last_name = lname
        user.email = email
        user.is_superuser = False
        user.is_staff = False
        user.is_active = True
        user.save()

        self.stdout.write(self.style.SUCCESS(f'Added user "{uname}" with password "{pw}" ({email})'))
