# Copyright 2026 EcoFuture Technology Services LLC and contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Generated by Django 5.0.1 on 2024-08-23 23:26

import bazis.contrib.bg.models
import bazis.core.utils.orm
import django.core.files.storage.filesystem
import django.db.models.deletion
import model_clone.mixin
import uuid
from django.conf import settings
from django.db import migrations, models
from django.utils.translation import gettext_lazy as _



class Migration(migrations.Migration):

    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='TaskHandler',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('pid', models.CharField(max_length=255, unique=True, verbose_name='PID')),
                ('host', models.CharField(max_length=255, verbose_name='Host')),
                ('port', models.PositiveIntegerField(verbose_name='Port')),
            ],
            options={
                'verbose_name': 'Handler',
                'verbose_name_plural': 'Handlers',
                'abstract': False,
            },
            bases=(model_clone.mixin.CloneMixin, models.Model),
        ),
        migrations.CreateModel(
            name='TaskCron',
            fields=[
                ('dt_created', models.DateTimeField(auto_now_add=True, db_index=True, verbose_name='Creation time')),
                ('dt_updated', models.DateTimeField(auto_now=True, db_index=True, verbose_name='Update time')),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=255, verbose_name='Name')),
                ('cls_path', models.CharField(db_index=True, max_length=255, verbose_name='Task class')),
                ('args', models.JSONField(blank=True, null=True)),
                ('kwargs', models.JSONField(blank=True, null=True)),
                ('envs', models.JSONField(blank=True, null=True)),
                ('error', models.TextField(blank=True, null=True, verbose_name='Error')),
                ('period', models.CharField(max_length=100, verbose_name='Period value')),
                ('dt_run', models.DateTimeField(blank=True, null=True, verbose_name='Next run time')),
                ('is_enable', models.BooleanField(db_index=True, default=True, verbose_name='Enabled')),
                ('author', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='author_%(class)s', to=settings.AUTH_USER_MODEL)),
                ('author_updated', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='author_updated_%(class)s', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'verbose_name': 'Periodic task',
                'verbose_name_plural': 'Periodic tasks',
                'abstract': False,
            },
            bases=(model_clone.mixin.CloneMixin, models.Model),
        ),
        migrations.CreateModel(
            name='Task',
            fields=[
                ('dt_created', models.DateTimeField(auto_now_add=True, db_index=True, verbose_name='Creation time')),
                ('dt_updated', models.DateTimeField(auto_now=True, db_index=True, verbose_name='Update time')),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=255, verbose_name='Name')),
                ('cls_path', models.CharField(db_index=True, max_length=255, verbose_name='Task class')),
                ('args', models.JSONField(blank=True, null=True)),
                ('kwargs', models.JSONField(blank=True, null=True)),
                ('envs', models.JSONField(blank=True, null=True)),
                ('error', models.TextField(blank=True, null=True, verbose_name='Error')),
                ('pid', models.CharField(db_index=True, max_length=36, verbose_name='PID')),
                ('state', models.CharField(choices=[('draft', 'Draft'), ('waiting', 'Waiting'), ('starting', 'Starting'), ('running', 'Running'), ('done', 'Completed')], db_index=True, default='draft', max_length=35, verbose_name='State')),
                ('phase', models.CharField(max_length=255, verbose_name='Phase')),
                ('phases_history', models.JSONField(blank=True, encoder=bazis.core.utils.orm.JsonFieldEncoder, null=True, verbose_name='History')),
                ('interrupt', models.BooleanField(db_index=True, default=False, verbose_name='Interrupted manually')),
                ('dt_start', models.DateTimeField(blank=True, db_index=True, null=True, verbose_name='Start time')),
                ('dt_finish', models.DateTimeField(blank=True, db_index=True, null=True, verbose_name='Finish time')),
                ('file', models.FileField(blank=True, null=True, storage=django.core.files.storage.filesystem.FileSystemStorage, upload_to=bazis.contrib.bg.models.task_file_name, verbose_name='File')),
                ('file_params', models.JSONField(blank=True, encoder=bazis.core.utils.orm.JsonFieldEncoder, null=True)),
                ('log', models.TextField(blank=True, null=True, verbose_name='Log')),
                ('expected', models.JSONField(blank=True, encoder=bazis.core.utils.orm.JsonFieldEncoder, null=True, verbose_name='Expected')),
                ('performed', models.JSONField(blank=True, encoder=bazis.core.utils.orm.JsonFieldEncoder, null=True, verbose_name='Completed')),
                ('result', models.JSONField(blank=True, encoder=bazis.core.utils.orm.JsonFieldEncoder, null=True, verbose_name='Result')),
                ('author', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='author_%(class)s', to=settings.AUTH_USER_MODEL)),
                ('author_updated', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='author_updated_%(class)s', to=settings.AUTH_USER_MODEL)),
                ('task_cron', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='bg.taskcron', verbose_name='Periodic task')),
                ('handler', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='bg.taskhandler')),
            ],
            options={
                'verbose_name': 'Background task',
                'verbose_name_plural': 'Background tasks',
                'ordering': ['-id'],
                'abstract': False,
                'index_together': {('cls_path', 'state'), ('state', 'dt_finish')},
            },
            bases=(model_clone.mixin.CloneMixin, models.Model),
        ),
    ]
