# Generated by Django 5.2.8 on 2025-11-28 23:16

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


class Migration(migrations.Migration):

    dependencies = [
        ('schedule', '0012_remove_photos_from_resourcetype'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='ScheduledTask',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(help_text='Human-readable name for this scheduled task', max_length=200)),
                ('description', models.TextField(blank=True, help_text='What this task does')),
                ('plugin_name', models.CharField(db_index=True, help_text='Name of the plugin to execute', max_length=100)),
                ('plugin_config', models.JSONField(blank=True, default=dict, help_text='Configuration dictionary for the plugin')),
                ('schedule_type', models.CharField(choices=[('CRON', 'Cron Expression'), ('INTERVAL', 'Fixed Interval'), ('ONE_TIME', 'One-Time')], default='INTERVAL', max_length=20)),
                ('cron_expression', models.CharField(blank=True, help_text="Cron expression (e.g., '0 0 * * *' for daily at midnight)", max_length=100)),
                ('interval_minutes', models.PositiveIntegerField(blank=True, help_text='Run every N minutes (for INTERVAL schedule type)', null=True)),
                ('run_at', models.DateTimeField(blank=True, help_text='Specific datetime to run (for ONE_TIME schedule type)', null=True)),
                ('status', models.CharField(choices=[('ACTIVE', 'Active'), ('PAUSED', 'Paused'), ('DISABLED', 'Disabled')], db_index=True, default='ACTIVE', max_length=20)),
                ('last_run_at', models.DateTimeField(blank=True, help_text='When this task last executed', null=True)),
                ('last_run_status', models.CharField(blank=True, help_text='Status of last execution (success/failed)', max_length=20)),
                ('last_run_result', models.JSONField(blank=True, help_text='Result data from last execution', null=True)),
                ('next_run_at', models.DateTimeField(blank=True, db_index=True, help_text='When this task will next execute', null=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('celery_task_id', models.CharField(blank=True, help_text='ID of the associated Celery periodic task', max_length=255)),
                ('created_by', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='created_scheduled_tasks', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'ordering': ['-created_at'],
            },
        ),
        migrations.CreateModel(
            name='TaskExecutionLog',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('started_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('completed_at', models.DateTimeField(blank=True, null=True)),
                ('status', models.CharField(choices=[('SUCCESS', 'Success'), ('FAILED', 'Failed'), ('SKIPPED', 'Skipped')], db_index=True, max_length=20)),
                ('result', models.JSONField(blank=True, help_text='Result data returned by the plugin', null=True)),
                ('error_message', models.TextField(blank=True, help_text='Error message if execution failed')),
                ('execution_time_ms', models.PositiveIntegerField(blank=True, help_text='How long the execution took in milliseconds', null=True)),
                ('scheduled_task', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='execution_logs', to='schedule.scheduledtask')),
            ],
            options={
                'ordering': ['-started_at'],
            },
        ),
        migrations.AddIndex(
            model_name='scheduledtask',
            index=models.Index(fields=['status', 'next_run_at'], name='schedule_sc_status_54324c_idx'),
        ),
        migrations.AddIndex(
            model_name='scheduledtask',
            index=models.Index(fields=['plugin_name', 'status'], name='schedule_sc_plugin__561093_idx'),
        ),
        migrations.AddIndex(
            model_name='taskexecutionlog',
            index=models.Index(fields=['scheduled_task', '-started_at'], name='schedule_ta_schedul_c82569_idx'),
        ),
        migrations.AddIndex(
            model_name='taskexecutionlog',
            index=models.Index(fields=['status', '-started_at'], name='schedule_ta_status_96538c_idx'),
        ),
    ]
