# Generated by Django 4.2.14 on 2024-07-16 09:30

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


def populate_event_status_uuid(apps, schema_editor):
    """Populate ProjectEventStatus sodar_uuid fields"""
    ProjectEventStatus = apps.get_model('timeline', 'ProjectEventStatus')
    for status in ProjectEventStatus.objects.all():
        status.sodar_uuid = uuid.uuid4()
        status.save(update_fields=['sodar_uuid'])


def populate_object_ref_uuid(apps, schema_editor):
    """Populate TimelineEventObjectRef sodar_uuid fields"""
    TimelineEventObjectRef = apps.get_model(
        'timeline', 'TimelineEventObjectRef'
    )
    for status in TimelineEventObjectRef.objects.all():
        status.sodar_uuid = uuid.uuid4()
        status.save(update_fields=['sodar_uuid'])


# Functions from the following migrations need manual copying.
# Move them and any dependencies into this file, then update the
# RunPython operations to refer to the local versions:
# timeline.migrations.0010_populate_uuid
# timeline.migrations.0014_populate_uuid


class Migration(migrations.Migration):

    replaces = [
        ('timeline', '0001_initial'),
        ('timeline', '0002_projectevent_user'),
        ('timeline', '0003_rename_uuid'),
        ('timeline', '0004_update_uuid'),
        ('timeline', '0005_update_jsonfield'),
        ('timeline', '0006_update_user_optional'),
        ('timeline', '0007_allow_null_project'),
        ('timeline', '0008_projectevent_plugin'),
        ('timeline', '0009_create_status_uuid'),
        ('timeline', '0010_populate_uuid'),
        ('timeline', '0011_make_uuid_unique'),
        ('timeline', '0012_rename_models'),
        ('timeline', '0013_timelineeventobjectref_sodar_uuid'),
        ('timeline', '0014_populate_uuid'),
        ('timeline', '0015_make_uuid_unique'),
    ]

    initial = True

    dependencies = [
        ('projectroles', '0028_populate_finder_role'),
        ('projectroles', '0001_initial'),
        ('projectroles', '0019_project_public_guest_access'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='ProjectEvent',
            fields=[
                (
                    'id',
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name='ID',
                    ),
                ),
                (
                    'app',
                    models.CharField(
                        help_text='App from which the event was triggered',
                        max_length=255,
                    ),
                ),
                (
                    'event_name',
                    models.CharField(
                        help_text='Event ID string', max_length=255
                    ),
                ),
                (
                    'description',
                    models.TextField(
                        help_text='Description of status change (may include {object label} references)'
                    ),
                ),
                (
                    'extra_data',
                    models.JSONField(
                        default=dict, help_text='Additional event data as JSON'
                    ),
                ),
                (
                    'classified',
                    models.BooleanField(
                        default=False,
                        help_text='Event is classified (only viewable by user levels specified in rules)',
                    ),
                ),
                (
                    'sodar_uuid',
                    models.UUIDField(
                        default=uuid.uuid4,
                        help_text='Event SODAR UUID',
                        unique=True,
                    ),
                ),
                (
                    'project',
                    models.ForeignKey(
                        help_text='Project to which the event belongs (null for no project)',
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name='events',
                        to='projectroles.project',
                    ),
                ),
                (
                    'user',
                    models.ForeignKey(
                        help_text='User who initiated the event (optional)',
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    'plugin',
                    models.CharField(
                        blank=True,
                        help_text='Plugin to which the event is related (optional, if unset plugin with the name of the app is assumed)',
                        max_length=255,
                        null=True,
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name='ProjectEventObjectRef',
            fields=[
                (
                    'id',
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name='ID',
                    ),
                ),
                (
                    'label',
                    models.CharField(
                        help_text='Label for the object related to the event',
                        max_length=255,
                    ),
                ),
                (
                    'name',
                    models.CharField(
                        help_text='Name or title of the object', max_length=255
                    ),
                ),
                (
                    'object_model',
                    models.CharField(
                        help_text='Object model as string', max_length=255
                    ),
                ),
                (
                    'object_uuid',
                    models.UUIDField(
                        blank=True, help_text='Object SODAR UUID', null=True
                    ),
                ),
                (
                    'extra_data',
                    models.JSONField(
                        default=dict,
                        help_text='Additional data related to the object as JSON',
                    ),
                ),
                (
                    'event',
                    models.ForeignKey(
                        help_text='Event to which the object belongs',
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name='event_objects',
                        to='timeline.projectevent',
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name='ProjectEventStatus',
            fields=[
                (
                    'id',
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name='ID',
                    ),
                ),
                (
                    'timestamp',
                    models.DateTimeField(
                        auto_now_add=True,
                        help_text='DateTime of the status change',
                    ),
                ),
                (
                    'status_type',
                    models.CharField(
                        help_text='Type of the status change', max_length=64
                    ),
                ),
                (
                    'description',
                    models.TextField(
                        blank=True,
                        help_text='Description of status change (optional)',
                    ),
                ),
                (
                    'extra_data',
                    models.JSONField(
                        default=dict, help_text='Additional status data as JSON'
                    ),
                ),
                (
                    'event',
                    models.ForeignKey(
                        help_text='Event to which the status change belongs',
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name='status_changes',
                        to='timeline.projectevent',
                    ),
                ),
                (
                    'sodar_uuid',
                    models.UUIDField(
                        default=uuid.uuid4,
                        help_text='Status SODAR UUID',
                        null=True,
                    ),
                ),
            ],
        ),
        migrations.RunPython(
            code=populate_event_status_uuid,
            reverse_code=migrations.RunPython.noop,
        ),
        migrations.AlterField(
            model_name='projecteventstatus',
            name='sodar_uuid',
            field=models.UUIDField(
                default=uuid.uuid4, help_text='Status SODAR UUID', unique=True
            ),
        ),
        migrations.RenameModel(
            old_name='ProjectEvent',
            new_name='TimelineEvent',
        ),
        migrations.RenameModel(
            old_name='ProjectEventObjectRef',
            new_name='TimelineEventObjectRef',
        ),
        migrations.RenameModel(
            old_name='ProjectEventStatus',
            new_name='TimelineEventStatus',
        ),
        migrations.AddField(
            model_name='timelineeventobjectref',
            name='sodar_uuid',
            field=models.UUIDField(
                default=uuid.uuid4,
                help_text='Object reference SODAR UUID',
                null=True,
            ),
        ),
        migrations.RunPython(
            code=populate_object_ref_uuid,
            reverse_code=migrations.RunPython.noop,
        ),
        migrations.AlterField(
            model_name='timelineeventobjectref',
            name='sodar_uuid',
            field=models.UUIDField(
                default=uuid.uuid4,
                help_text='Object reference SODAR UUID',
                unique=True,
            ),
        ),
    ]
