# Generated by Django 4.2.21 on 2025-12-19 14:05

from django.db import migrations


def create_postmortem_reminder_task(apps, schema_editor):
    """Create periodic task for post-mortem reminders.

    Runs twice daily at 10:00 AM and 3:00 PM to check for incidents mitigated 5+ days ago.
    """
    CrontabSchedule = apps.get_model("django_celery_beat", "CrontabSchedule")
    PeriodicTask = apps.get_model("django_celery_beat", "PeriodicTask")

    # Create crontab schedule for 10:00 AM and 3:00 PM (Europe/Paris timezone)
    # Crontab format: minute hour day_of_week day_of_month month_of_year
    # hour='10,15' means run at both 10:00 and 15:00
    # Using Europe/Paris timezone handles DST automatically
    schedule, _ = CrontabSchedule.objects.get_or_create(
        minute="0",
        hour="10,15",
        day_of_week="*",
        day_of_month="*",
        month_of_year="*",
        timezone="Europe/Paris",
    )

    # Create the periodic task
    PeriodicTask.objects.get_or_create(
        name="Send post-mortem reminders for mitigated incidents",
        defaults={
            "task": "slack.send_postmortem_reminders",
            "crontab": schedule,
            "enabled": True,
            "description": "Send reminders for incidents mitigated 5+ days ago that still need post-mortem completion (runs at 10 AM and 3 PM)",
        },
    )


def remove_postmortem_reminder_task(apps, schema_editor):
    """Remove the periodic task on migration rollback."""
    PeriodicTask = apps.get_model("django_celery_beat", "PeriodicTask")

    PeriodicTask.objects.filter(
        task="slack.send_postmortem_reminders"
    ).delete()


class Migration(migrations.Migration):

    dependencies = [
        ("slack", "0008_alter_conversation_incident_categories_and_more"),
        ("django_celery_beat", "0019_alter_periodictasks_options"),
    ]

    operations = [
        migrations.RunPython(
            create_postmortem_reminder_task,
            reverse_code=remove_postmortem_reminder_task,
        ),
    ]
