# Generated by Django 3.2.5 on 2022-10-17 07:40

import contextlib
from enum import unique

import django.db.models.deletion
import finflo.validators
from django.conf import settings
from django.db import migrations, models
from finflo.base_enum import Values


# RUN ON MIGRATIONS  

def create_flow_model(apps, schema_editor):
    flowmodel = apps.get_model("finflo.Flowmodel")
    try:
        a = settings.FINFLO['WORK_MODEL']
        db_alias = schema_editor.connection.alias
        for model in a:
            flowmodel.objects.using(db_alias).create(description = model.replace('.','_'))
    except:
        raise LookupError("Couldn't find work_model in settings.py check documentation")



def create_party_type_model(apps, schema_editor):
    partytype = apps.get_model("finflo.PartyType")
    with contextlib.suppress(Exception):
        abstract_models = settings.FINFLO['PARTY_MODEL']
        if abstract_models is not None:
            base_model_from_app = apps.get_model(abstract_models[0])
            for iter in base_model_from_app.objects.all():
                partytype.objects.create(description = iter.name)
    # bypassed the system for without the party_model instance


def create_sign_list(apps, schema_editor):
    states = apps.get_model("finflo.SignList")
    db_alias = schema_editor.connection.alias
    #create a default 3 signs
    states.objects.using(db_alias).bulk_create([states(name = Values.NO_SIGN.value, sign_id = 0 , sub_action_name = Values.NO_SIGN.value),
    states(name = Values.AWAITING_SIGN_A.value, sign_id = 1 , sub_action_name = Values.SIGN_A.value),
    states(name = Values.AWAITING_SIGN_B.value, sign_id = 2 , sub_action_name = Values.SIGN_B.value),
    states(name = Values.AWAITING_SIGN_C.value, sign_id = 3 , sub_action_name = Values.SIGN_C.value)])


def create_initial_draft(apps, schema_editor):
    states = apps.get_model("finflo.States")
    db_alias = schema_editor.connection.alias
    states.objects.using(db_alias).create(description = Values.DRAFT.value)
    

# DO NOT EDIT THE BELOW LINE IN PRODUCTION --
def creates_default_return_Action(apps,schema_editor):
    action = apps.get_model('finflo.Action')
    states = apps.get_model("finflo.States")
    db_alias = schema_editor.connection.alias
    action.objects.using(db_alias).create(description = Values.RETURN.value , to_state = states.objects.get(id = 1 )  )





class Migration(migrations.Migration):

    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='Flowmodel',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('description', models.CharField(max_length=255)),
            ],
            options={
                'verbose_name_plural': '1. Flowmodel',
                'ordering': ['id'],
            },
        ),
        migrations.RunPython(create_flow_model),
        migrations.CreateModel(
            name='PartyType',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('description', models.CharField(max_length=255, unique=True)),
            ],
            options={
                'verbose_name_plural': '2. PartyType',
                'ordering': ['id'],
            },
        ),
        migrations.RunPython(create_party_type_model),
        migrations.CreateModel(
            name='SignList',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('sign_id', models.IntegerField(validators=[finflo.validators.validate_postive], editable = False, blank=True, null=True)),
                ('name', models.CharField(max_length=255, unique = True)),
                ('sub_action_name', models.CharField(max_length=255 , unique = True)),
            ],
            options={
                'verbose_name_plural': '5. Signatures',
                'ordering': ['id'],
            },
        ),
        migrations.RunPython(create_sign_list),
        migrations.CreateModel(
            name='States',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('description', models.CharField(max_length=255)),
            ],
            options={
                'verbose_name_plural': '6. States',
                'ordering': ['id'],
            },
        ),
        migrations.RunPython(create_initial_draft),
        migrations.CreateModel(
            name='TransitionManager',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('t_id', models.IntegerField()),
                ('type', models.CharField(max_length=255)),
                ('sub_sign', models.IntegerField(default=0, editable = False)),
                ('in_progress', models.BooleanField(default=False)),
            ],
            options={
                'verbose_name_plural': '4. TransitionManager',
                'ordering': ['id'],
            },
        ),
        migrations.CreateModel(
            name='workflowitems',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_date', models.DateTimeField(auto_now_add=True)),
                ('initial_state', models.CharField(default='DRAFT', max_length=50)),
                ('interim_state', models.CharField(default='DRAFT', max_length=50)),
                ('final_state', models.CharField(default='DRAFT', max_length=50)),
                ('next_available_transitions', models.JSONField(blank=True, null=True)),
                ('action', models.CharField(blank=True, default='DRAFT', max_length=25, null=True)),
                ('subaction', models.CharField(blank=True, max_length=55, null=True)),
                ('previous_action', models.CharField(blank=True, max_length=55, null=True)),
                ('model_type', models.CharField(blank=True, max_length=55, null=True)),
                ('comments', models.CharField(blank=True, max_length=500, null=True)),
                ('is_read', models.BooleanField(blank=True, default=True, null=True)),
                ('final_value', models.BooleanField(blank=True, default=False, null=True)),
                ('current_from_party', models.CharField(max_length = 500 , blank = True , null = True )),
                ('current_to_party', models.CharField(max_length = 500 , blank = True , null = True )),
                ('event_user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)),
                ('transitionmanager', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='finflo.transitionmanager')),
            ],
            options={
                'verbose_name_plural': '7. WorkFlowItem',
                'ordering': ['id'],
            },
        ),
        migrations.CreateModel(
            name='workevents',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('action', models.CharField(blank=True, default='DRAFT', max_length=25, null=True)),
                ('subaction', models.CharField(blank=True, default='DRAFT', max_length=55, null=True)),
                ('initial_state', models.CharField(default='DRAFT', max_length=50)),
                ('interim_state', models.CharField(default='DRAFT', max_length=50)),
                ('final_state', models.CharField(default='DRAFT', max_length=50)),
                ('is_read', models.BooleanField(blank=True, default=True, null=True)),
                ('record_datas', models.JSONField(blank=True, null=True)),
                ('final_value', models.BooleanField(blank=True, default=False, null=True)),
                ('comments', models.CharField(blank=True, max_length=500, null=True)),
                ('created_date', models.DateTimeField(auto_now_add=True)),
                ('type', models.CharField(blank=True, max_length=55, null=True)),
                ('event_user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)),
                ('from_party', models.CharField(max_length = 500 , blank = True , null = True )),
                ('to_party', models.CharField(max_length = 500 , blank = True , null = True )),
                ('workflowitems', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='WorkFlowEvents', to='finflo.workflowitems')),
            ],
            options={
                'verbose_name_plural': '8. WorkFlowEvent',
                'ordering': ['id'],
            },
        ),
        migrations.CreateModel(
            name='Action',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('description', models.CharField(blank=True, help_text='e.g., SUBMIT , DELETE', max_length=255, null=True)),
                ('intermediator', models.BooleanField(default=False)),
                ('sign_required', models.IntegerField(blank=True, default=0, editable = False, help_text='IMPORTANT : if 0 means initial_transition ', null=True)),
                ('from_party', models.ForeignKey(blank=True, help_text='this field is optional', null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='from_transition_party_type', to='finflo.partytype')),
                ('from_state', models.ForeignKey(blank=True, help_text='initial from state for transition ', null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='action_from_state', to='finflo.states')),
                ('model', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='finflo.flowmodel')),
                ('stage_required', models.ForeignKey(blank=True, help_text='this field is optional , IMPORTANT : if NO_SIGN means initial_transition', null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='finflo.signlist')),
                ('to_party', models.ForeignKey(blank=True, help_text='this field is optional', null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='to_transition_party_type', to='finflo.partytype')),
                ('to_state', models.ForeignKey(blank=True, help_text='final state for the transition to take place', null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='finflo.states')),
            ],
            options={
                'verbose_name_plural': '3. Action',
                'ordering': ['id'],
                'unique_together': {('description', 'model')},
            },
        ),
        migrations.RunPython(creates_default_return_Action),
    ]
