# -*- coding: utf-8 -*-
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand

from portal.models import Profile

User = get_user_model()

accounts = [
    {
        "username": "simon-test",
        "password": "Test123",
        "first_name": "Simon",
        "last_name": "Caminada",
        "street": "Telligraben 3",
        "zip": 5420,
        "place": "Ehrendingen",
        "email": "simon@baker-street.ch"
    },
    {
        "username": "peter.Möström",
        "password": "möström",
        "first_name": "Peter",
        "last_name": "Möström",
        "street": "",
        "zip": "",
        "place": "",
        "email": ""
    },
    {
        "username": "fail test",
        "password": "testtest",
        "first_name": "lakjsdf",
        "last_name": "lkasdjf asdgf",
        "street": "adsg",
        "zip": "",
        "place": "",
        "email": "asdgad"
    }
]


class Command(BaseCommand):

    def handle(self, *args, **options):
        new = 0
        old = 0

        for account in accounts:
            print('---')
            username = account.pop('username')
            try:
                user = User.objects.get(username=username)
                profile = user.profile

                old = old + 1

                print('User already exists:')
            except User.DoesNotExist:
                user = User.objects.create_user(username=username, password=account.pop('password'))

                account['zip'] = account['zip'] if account['zip'] else None
                profile = Profile.objects.create(user=user, **account)

                new = new + 1

                print('New user created:')
            print(user)
            print(profile)

        print('=====')
        print('New users: {} / Old users: {}'.format(new, old))
