# Generated by Django 5.0.9 on 2024-11-27 15:14

from contextlib import suppress

import django.db.models.deletion
from django.db import migrations, models


def set_activation_date(*args):
    from wbcompliance.models.risk_management import RiskCheck, RiskRule

    for rule in RiskRule.objects.filter(activation_date__isnull=True):
        with suppress(RiskCheck.DoesNotExist):
            rule.activation_date = rule.checks.earliest("evaluation_date").evaluation_date
            rule.save()


class Migration(migrations.Migration):
    dependencies = [
        ("wbcompliance", "0018_alter_rulecheckedobjectrelationship_unique_together"),
    ]

    operations = [
        migrations.CreateModel(
            name="RuleGroup",
            fields=[
                ("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("key", models.CharField(max_length=255, unique=True)),
                ("name", models.CharField(max_length=255)),
            ],
        ),
        migrations.AddField(
            model_name="riskrule",
            name="activation_date",
            field=models.DateField(blank=True, null=True, verbose_name="Activation Date"),
        ),
        migrations.AddField(
            model_name="riskrule",
            name="frequency",
            field=models.CharField(
                blank=True,
                help_text="The Evaluation Frequency in RRULE format",
                max_length=56,
                null=True,
                verbose_name="Evaluation Frequency",
            ),
        ),
        migrations.AddField(
            model_name="rulebackend",
            name="rule_group",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="rules",
                to="wbcompliance.rulegroup",
            ),
        ),
        migrations.RunPython(set_activation_date),
    ]
