# Generated by Django 3.2.5 on 2022-08-25 07:28
from enum import Enum
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
from finflo.validators import validate_postive



# DEFAULT ENUM CLASS FOR ACTION AND STATES 
class Values(Enum):
    NO_SIGN = "NO_SIGN"
    DRAFT = "DRAFT"
    RETURN = "RETURN"




# RUN ON MIGRATIONS ONLY

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 i in a:
            flowmodel.objects.using(db_alias).create(description = i)
    except:
        raise LookupError("Couldn't find work_model in settings.py check documentation")



def create_sign_list(apps, schema_editor):
    states = apps.get_model("finflo.SignList")
    db_alias = schema_editor.connection.alias
    states.objects.using(db_alias).create(name = Values.NO_SIGN, sign_id = 1 , sub_name = Values.NO_SIGN)


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)
    


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 , to_state = states.objects.get(id = 1 ) )





class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('accounts', '0001_initial'),
        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',
            },
        ),
        migrations.RunPython(create_flow_model),
        migrations.CreateModel(
            name='SignList',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('sign_id', models.IntegerField()),
                ('name', models.CharField(default=None, max_length=255)),
                ('sub_name', models.CharField(default=None, max_length=255)),
            ],
        ),
        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': '2. States',
            },
        ),
        migrations.RunPython(create_initial_draft),
        migrations.CreateModel(
            name='Temp',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ],
        ),
        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)),
            ],
        ),
        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.CharField(max_length=500)),
                ('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.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='wf_from_party', to='accounts.parties')),
                ('current_to_party', models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, to='accounts.parties')),
                ('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': '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)),
                ('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.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='we_from_party', to='accounts.parties')),
                ('to_party', models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, to='accounts.parties')),
                ('workflowitems', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='WorkFlowEvents', to='finflo.workflowitems')),
            ],
            options={
                'verbose_name_plural': '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(default=0, help_text='IMPORTANT : if 0 means initial_transition ', validators=[validate_postive])),
                ('from_party', models.ForeignKey(blank=True, help_text='this field is optional', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='from_transition_party', to='accounts.parties')),
                ('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(help_text='IMPORTANT : if 0 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.SET_NULL, related_name='to_last_party', to='accounts.parties')),
                ('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',
                'unique_together': {('description', 'model')},
            },
        ),
        migrations.RunPython(creates_default_return_Action),
    ]
