# Generated by Django 2.1.3 on 2018-12-07 15:11
from os import getenv

from django.contrib.auth import get_user_model
from django.db import migrations


def create_preferences(apps, schema_editor):
    PreferenceModule = apps.get_model('pyplan', 'PreferenceModule')
    Preference = apps.get_model('pyplan', 'Preference')

    # STARTPREFERENCES
    pref_mod_1 = PreferenceModule.objects.create(
        code='init', description='Start Preferences')

    # 3|----|Load model on login|----|null|----|2|----|autoload_model|----|company
    Preference.objects.create(module=pref_mod_1, code='autoload_model',
                              description='Load model on login', definition=None)
    # 4|----|Summary widget config|----|null|----|2|----|summary_widgets|----|user
    Preference.objects.create(module=pref_mod_1, code='summary_widgets',
                              description='Summary widget config',
                              definition=[
                                  {"widgetType": "welcome"}
                              ])

    # REGIONALSETTINGS
    pref_mod_2 = PreferenceModule.objects.create(
        code='regional_settings', description='Regional Settings')

    # 16|----|Decimal separator|----|.|----|5|----|decimal_sep|----|company
    decimal_sep = Preference.objects.create(module=pref_mod_2, code='decimal_sep',
                                            description='Decimal separator', definition={"value": "."})
    # 17|----|Thousand separator|----|,|----|5|----|thousand_sep|----|company
    thousand_sep = Preference.objects.create(module=pref_mod_2, code='thousand_sep',
                                             description='Thousand separator', definition={"value": ","})
    # 18|----|Decimal separator on CTRL+C|----|null|----|5|----|copy_decimal_sep|----|user
    Preference.objects.create(module=pref_mod_2, code='copy_decimal_sep',
                              description='Decimal separator on CTRL+C', definition=None)

    # EMAILSETTINGS
    pref_mod_3 = PreferenceModule.objects.create(
        code='email_settings', description='Email Settings')

    # 19|----|Activate Email Sending|----|false|----|6|----|emailQueueActivation|----|company
    email_service_active = Preference.objects.create(module=pref_mod_3, code='email_service_active',
                                                     description='Activate Email Sending', definition={"value": True})
    # 20|----|Email Templates|----|cubeplan|----|6|----|templatePrefix|----|company
    email_template_prefix = Preference.objects.create(module=pref_mod_3, code='email_template_prefix',
                                                      description='Email Templates Prefix', definition={"value": "pyplan"})
    # 21|----|Use SMTP instead of email service app|----|false|----|6|----|mail_smtp|----|company
    email_use_smtp = Preference.objects.create(module=pref_mod_3, code='email_use_smtp',
                                               description='Use SMTP instead of email service app', definition={"value": True})
    email_smtp_host = Preference.objects.create(module=pref_mod_3, code='smtp_host',
                                                description='SMTP Host', definition={"value": 'smtp.gmail.com'})
    email_smtp_port = Preference.objects.create(module=pref_mod_3, code='smtp_port',
                                                description='SMTP port', definition={"value": 587})
    email_smtp_user = Preference.objects.create(module=pref_mod_3, code='smtp_user',
                                                description='Email SMTP user', definition={"value": 'noreply@pyplan.com'})
    email_smtp_password = Preference.objects.create(module=pref_mod_3, code='smtp_password',
                                                    description='Email SMTP password', definition={"value": 'pyplan123'})
    email_smtp_use_tls = Preference.objects.create(module=pref_mod_3, code='smtp_use_tls',
                                                   description='Email SMTP uses tls', definition={"value": True})
    email_smtp_use_ssl = Preference.objects.create(module=pref_mod_3, code='smtp_use_ssl',
                                                   description='Email SMTP uses ssl', definition={"value": False})
    email_smtp_timeout = Preference.objects.create(module=pref_mod_3, code='smtp_timeout',
                                                   description='Email SMTP timeout (in seconds)', definition={"value": 300})
    smtp_ssl_keyfile = Preference.objects.create(module=pref_mod_3, code='smtp_ssl_keyfile',
                                                 description='The ssl keyfile', definition=None)
    smtp_ssl_certfile = Preference.objects.create(module=pref_mod_3, code='smtp_ssl_certfile',
                                                  description='The ssl certfile', definition=None)
    email_default_from_name = Preference.objects.create(module=pref_mod_3, code='email_default_from_name',
                                                        description='The default from name to be displayed', definition={"value": 'Pyplan - No Reply'})
    email_throw_errors = Preference.objects.create(module=pref_mod_3, code='email_throw_errors',
                                                   description='Email show errors', definition={"value": True})

    # INPUTDATAMODULE
    pref_mod_4 = PreferenceModule.objects.create(
        code='input_data_module', description='Input Data Module')
    # 22|----|Input data connect string|----|null|----|7|----|inputdataconnectstring|----|company
    input_data_connection_string = Preference.objects.create(module=pref_mod_4, code='input_data_connection_string',
                                                             description='Input data connection string', definition=None)

    # FILEMANAGERMODULE
    pref_mod_5 = PreferenceModule.objects.create(
        code='filemanager_module', description='Filemanager Module')
    optimize_templates = Preference.objects.create(module=pref_mod_5, code='optimize_templates',
                                                   description='Optimize templates on upload', definition={"value": True})


def create_default_company(apps, schema_editor):
    # We can't import the Company model directly as it may be a newer
    # version than this migration expects. We use the historical version.
    Company = apps.get_model('pyplan', 'Company')
    Company.objects.create(code='pyplan', name='Pyplan')
    # for person in Company.objects.all():
    #     person.name = '%s %s' % (person.first_name, person.last_name)
    #     person.save()


def create_guest_group(apps, schema_editor):
    # - create group
    Group = apps.get_model('auth', 'Group')
    Group.objects.create(name='Guest')


def create_guest_department(apps, schema_editor):
    # - get company
    Company = apps.get_model('pyplan', 'Company')
    company = Company.objects.get(code='pyplan')

    # - create department
    Department = apps.get_model('pyplan', 'Department')
    Department.objects.create(
        code='guest',
        name='Guest',
        company=company,
        engine_definitions={
            'container_params': {
                'name': f'{company.code}guestcontainers'
            }
        }
    )

def create_superuser(apps, schema_editor):
    # - get company
    Company = apps.get_model('pyplan', 'Company')
    company = Company.objects.get(code='pyplan')

    # - create superuser
    UserModel = get_user_model()
    superuser_data = {
        'username': 'user',
        'first_name': 'User',
        'last_name': '',
        'email': 'user@pyplan.com',
        'password': 'Pyplan123!',
        'is_active': True,
    }
    UserModel.objects.create_superuser(**superuser_data)

    # get user
    User = apps.get_model('pyplan', 'User')
    user = User.objects.get(username='user')

    # - create usercompany
    UserCompany = apps.get_model('pyplan', 'UserCompany')
    UserCompany.objects.create(user=user, company=company, active=True)


def create_guestuser(apps, schema_editor):
    # - get company
    Company = apps.get_model('pyplan', 'Company')
    company = Company.objects.get(code='pyplan')

    # - get guest department
    Department = apps.get_model('pyplan', 'Department')
    department = Department.objects.get(company=company, code='guest')

    # - create guest user
    UserModel = get_user_model()
    username = f'{company.code}-guest'
    user_data = {
        'username': username,
        'first_name': f'{company.name} - Guest User'
    }
    UserModel.objects.create(**user_data)

    # get user
    User = apps.get_model('pyplan', 'User')
    user = User.objects.get(username=username)

    # - create usercompany
    UserCompany = apps.get_model('pyplan', 'UserCompany')
    guest_user_company = UserCompany.objects.create(user=user, company=company)

    # get group
    Group = apps.get_model('auth', 'Group')
    group = Group.objects.get(name='Guest')

    # - add guest user to guest group
    user.groups.add(group)

    # - add guest department to guest user company
    guest_user_company.departments.add(department)


def create_company_preferences(apps, schema_editor):
    CompanyPreference = apps.get_model('pyplan', 'CompanyPreference')

    # - get company
    Company = apps.get_model('pyplan', 'Company')
    company = Company.objects.get(code='pyplan')

    # get preferences
    Preference = apps.get_model('pyplan', 'Preference')
    summary_widgets = Preference.objects.get(code='summary_widgets')
    decimal_sep = Preference.objects.get(code='decimal_sep')
    thousand_sep = Preference.objects.get(code='thousand_sep')
    email_service_active = Preference.objects.get(code='email_service_active')
    email_use_smtp = Preference.objects.get(code='email_use_smtp')
    email_template_prefix = Preference.objects.get(
        code='email_template_prefix')
    smtp_host = Preference.objects.get(code='smtp_host')
    smtp_port = Preference.objects.get(code='smtp_port')
    smtp_user = Preference.objects.get(code='smtp_user')
    smtp_password = Preference.objects.get(code='smtp_password')
    smtp_use_tls = Preference.objects.get(code='smtp_use_tls')
    smtp_use_ssl = Preference.objects.get(code='smtp_use_ssl')
    smtp_timeout = Preference.objects.get(code='smtp_timeout')
    smtp_ssl_keyfile = Preference.objects.get(code='smtp_ssl_keyfile')
    smtp_ssl_certfile = Preference.objects.get(code='smtp_ssl_certfile')
    email_default_from_name = Preference.objects.get(
        code='email_default_from_name')
    email_throw_errors = Preference.objects.get(code='email_throw_errors')
    input_data_connection_string = Preference.objects.get(
        code='input_data_connection_string')

    CompanyPreference.objects.create(
        company=company,
        preference=summary_widgets,
        definition=[
            {"widgetType": "welcome"}
        ]
    )
    CompanyPreference.objects.create(
        company=company,
        preference=decimal_sep,
        definition={"value": "."}
    )
    CompanyPreference.objects.create(
        company=company,
        preference=thousand_sep,
        definition={"value": ","}
    )

    # Email Preferences
    CompanyPreference.objects.create(
        company=company,
        preference=email_service_active,
        definition={"value": True}
    )
    CompanyPreference.objects.create(
        company=company,
        preference=email_use_smtp,
        definition={"value": True}
    )
    CompanyPreference.objects.create(
        company=company,
        preference=email_template_prefix,
        definition={"value": "pyplan"}
    )
    CompanyPreference.objects.create(
        company=company,
        preference=smtp_host,
        definition={"value": "smtp.gmail.com"}
    )
    CompanyPreference.objects.create(
        company=company,
        preference=smtp_port,
        definition={"value": 587}
    )
    CompanyPreference.objects.create(
        company=company,
        preference=smtp_user,
        definition={"value": "noreply@pyplan.com"}
    )
    CompanyPreference.objects.create(
        company=company,
        preference=smtp_password,
        definition={"value": "pyplan123"}
    )
    CompanyPreference.objects.create(
        company=company,
        preference=smtp_use_tls,
        definition={"value": True}
    )
    CompanyPreference.objects.create(
        company=company,
        preference=smtp_use_ssl,
        definition={"value": False}
    )
    CompanyPreference.objects.create(
        company=company,
        preference=smtp_timeout,
        definition={"value": 300}
    )
    CompanyPreference.objects.create(
        company=company,
        preference=smtp_ssl_keyfile,
        definition=None
    )
    CompanyPreference.objects.create(
        company=company,
        preference=smtp_ssl_certfile,
        definition=None
    )
    CompanyPreference.objects.create(
        company=company,
        preference=email_default_from_name,
        definition={"value": "Pyplan - No Reply"}
    )
    CompanyPreference.objects.create(
        company=company,
        preference=email_throw_errors,
        definition={"value": True}
    )

    CompanyPreference.objects.create(
        company=company,
        preference=input_data_connection_string,
        definition={
            "value": "Server=DESKTOP-M2HN9PF\SQLEXPRESS;Database=Cubeplan;User Id=cubeplan_usr;Password=cub_p4ss_1234;"
        }
    )


class Migration(migrations.Migration):

    dependencies = [
        ('pyplan', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(create_preferences),
        migrations.RunPython(create_default_company),
        migrations.RunPython(create_guest_group),
        migrations.RunPython(create_guest_department),
        migrations.RunPython(create_superuser),
        migrations.RunPython(create_guestuser),
        migrations.RunPython(create_company_preferences),
    ]
