# -*- coding: utf-8 -*-

# This file is part of wger Workout Manager.
#
# wger Workout Manager is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# wger Workout Manager is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License

import os
import sys
import csv
import random
import django
import argparse

from django.db import IntegrityError
from django.utils.text import slugify

sys.path.insert(0, os.path.join('..', '..'))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
django.setup()

# Must happen after calling django.setup()
from django.contrib.auth.models import User
from wger.gym.models import GymUserConfig

parser = argparse.ArgumentParser(description='Dummy data generator')
parser.add_argument("model",
                    help="The kind of entries you want to generate",
                    choices=['users', 'workout', 'gyms', 'logs', 'exercises'])

args = parser.parse_args()


if args.model == 'users':
    print("** Generating users")

    first_names = []
    last_names = []

    with open(os.path.join('csv', 'first_names_germany.csv')) as name_file:
        name_reader = csv.reader(name_file)
        for row in name_reader:
            first_names.append(row)

    with open(os.path.join('csv', 'last_names_german.csv')) as name_file:
        name_reader = csv.reader(name_file)
        for row in name_reader:
            last_names.append(row[0])

    for i in range(1, 101):
        name_data = random.choice(first_names)
        name = name_data[0]
        gender = name_data[1]
        surname = random.choice(last_names)

        username = slugify('{0}, {1}'.format(name[1], surname))
        email = '{0}@example.com'.format(username)
        password = username
        try:
            user = User.objects.create_user(username,
                                            email,
                                            password)
            user.first_name = name
            user.last_name = surname
            user.save()

        # Usernames are not guaranteed to be unique, so just ignore
        except IntegrityError as e:
            continue

        user.userprofile.gym_id = 1
        user.userprofile.gender = '1' if gender == 'm' else 2
        user.userprofile.save()

        config = GymUserConfig()
        config.gym_id = 1
        config.user = user
        config.save()

        # print('Name: {0}, {1}'.format(name, surname))
        # print('Username: {0}'.format(username))
        # print('Email: {0}'.format(email))
        # print('')

    pass
@@ -690,16 +690,28 @@
 ort csv%0A
+import uuid%0A
 import r
@@ -716,16 +716,16 @@
  random%0A
-
 import d
@@ -1125,14 +1125,8 @@
 n='D
-ummy d
 ata 
@@ -1134,16 +1134,50 @@
 enerator
+. Please consult the documentaiton
 ')%0Aparse
@@ -1886,16 +1886,43 @@
 , 101):%0A
+        uid = uuid.uuid4()%0A
         
@@ -2091,32 +2091,36 @@
 lugify('%7B0%7D, %7B1%7D
+ %7B2%7D
 '.format(name%5B1%5D
@@ -2120,20 +2120,142 @@
 name
-%5B1%5D, surname
+,%0A                                                 surname%5B0%5D,%0A                                                 str(uid).split('-')%5B1%5D
 ))%0A 
@@ -2328,24 +2328,25 @@
  = username%0A
+%0A
         try:
@@ -2612,17 +2612,42 @@
       # 
-U
+Even with the uuid part, u
 sernames
@@ -2683,22 +2683,56 @@
 que,
- so just ignor
+%0A        # in this case, just ignore and continu
 e%0A  

Reduce probability of username collisions