# Generated by Django 3.2.19 on 2023-06-10 12:29

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

import power_query.json
import power_query.models
import power_query.validators
from power_query.utils import dict_hash


def generate_hashes(apps, schema_editor):
    Dataset = apps.get_model("power_query", "Dataset")
    for dataset in Dataset.objects.all():
        arguments = dataset.info.get("arguments", {})
        query_id = dataset.query_id
        dataset.hash = dict_hash({"query": query_id, **arguments})
        dataset.save()


class Migration(migrations.Migration):
    replaces = [
        ("power_query", "0001_migration"),
        ("power_query", "0002_migration"),
        ("power_query", "0003_migration"),
        ("power_query", "0004_migration"),
        ("power_query", "0005_migration"),
        ("power_query", "0006_migration"),
        ("power_query", "0007_migration"),
        ("power_query", "0008_migration"),
        ("power_query", "0009_migration"),
        ("power_query", "0010_migration"),
        ("power_query", "0011_migration"),
        ("power_query", "0012_migration"),
        ("power_query", "0013_migration"),
        ("power_query", "0014_migration"),
    ]

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("contenttypes", "0002_remove_content_type_name"),
    ]

    operations = [
        migrations.CreateModel(
            name="Formatter",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        blank=True, max_length=255, null=True, unique=True
                    ),
                ),
                (
                    "content_type",
                    models.CharField(
                        choices=[
                            ["csv", "text/csv"],
                            ["html", "text/html"],
                            ["json", "application/json"],
                            ["txt", "text/plain"],
                            ["xls", "application/vnd.ms-excel"],
                            ["xml", "application/xml"],
                            ["yaml", "text/yaml"],
                        ],
                        max_length=5,
                    ),
                ),
                ("code", models.TextField(blank=True, null=True)),
            ],
        ),
        migrations.CreateModel(
            name="Query",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("name", models.CharField(max_length=255, unique=True)),
                ("description", models.TextField(blank=True, null=True)),
                ("code", models.TextField(blank=True, default="qs=conn.all()")),
                ("info", models.JSONField(blank=True, default=dict)),
                (
                    "sentry_error_id",
                    models.CharField(blank=True, max_length=400, null=True),
                ),
                (
                    "owner",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="power_queries",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "target",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="contenttypes.contenttype",
                    ),
                ),
                ("active", models.BooleanField(default=True)),
                (
                    "error_message",
                    models.CharField(blank=True, max_length=400, null=True),
                ),
                ("refresh_daily", models.BooleanField(default=False)),
            ],
            options={
                "verbose_name_plural": "Power Queries",
                "ordering": ("name",),
            },
        ),
        migrations.CreateModel(
            name="Parametrizer",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("code", models.SlugField(editable=False, max_length=255, unique=True)),
                ("name", models.CharField(max_length=255, unique=True)),
                (
                    "description",
                    models.TextField(blank=True, max_length=255, null=True),
                ),
                (
                    "value",
                    models.JSONField(
                        default=dict, validators=[power_query.models.validate_queryargs]
                    ),
                ),
                (
                    "system",
                    models.BooleanField(blank=True, default=False, editable=False),
                ),
            ],
            options={
                "verbose_name": "Arguments",
                "verbose_name_plural": "Arguments",
            },
        ),
        migrations.CreateModel(
            name="Report",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        blank=True, max_length=255, null=True, unique=True
                    ),
                ),
                ("refresh_daily", models.BooleanField(default=False)),
                ("last_run", models.DateTimeField(blank=True, null=True)),
                (
                    "formatter",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="power_query.formatter",
                    ),
                ),
                (
                    "owner",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "query",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="power_query.query",
                    ),
                ),
                ("active", models.BooleanField(default=True)),
                (
                    "document_title",
                    models.CharField(blank=True, max_length=255, null=True),
                ),
                (
                    "limit_access_to",
                    models.ManyToManyField(
                        blank=True,
                        related_name="_power_query_report_limit_access_to_+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="Dataset",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("last_run", models.DateTimeField(blank=True, null=True)),
                ("value", models.BinaryField(blank=True, null=True)),
                ("info", models.JSONField(blank=True, default=dict)),
                (
                    "query",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="datasets",
                        to="power_query.query",
                    ),
                ),
                ("description", models.CharField(default="", max_length=100)),
                (
                    "extra",
                    models.BinaryField(
                        blank=True,
                        help_text="Any other attribute to pass to the formatter",
                        null=True,
                    ),
                ),
                ("hash", models.CharField(default="", editable=False, max_length=200)),
            ],
        ),
        migrations.AddField(
            model_name="query",
            name="parametrizer",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                to="power_query.parametrizer",
            ),
        ),
        migrations.CreateModel(
            name="ReportDocument",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("timestamp", models.DateTimeField(auto_now=True)),
                ("title", models.CharField(max_length=300)),
                ("output", models.BinaryField(blank=True, null=True)),
                ("arguments", models.JSONField(default=dict)),
                (
                    "content_type",
                    models.CharField(
                        choices=[
                            ["csv", "text/csv"],
                            ["html", "text/html"],
                            ["json", "application/json"],
                            ["txt", "text/plain"],
                            ["xls", "application/vnd.ms-excel"],
                            ["xml", "application/xml"],
                            ["yaml", "text/yaml"],
                        ],
                        max_length=5,
                    ),
                ),
                (
                    "dataset",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="power_query.dataset",
                    ),
                ),
                (
                    "limit_access_to",
                    models.ManyToManyField(
                        blank=True,
                        related_name="_power_query_reportdocument_limit_access_to_+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "report",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="documents",
                        to="power_query.report",
                    ),
                ),
            ],
            options={
                "unique_together": {("report", "dataset")},
            },
        ),
        migrations.RunPython(generate_hashes, migrations.RunPython.noop),
        migrations.AlterField(
            model_name="dataset",
            name="hash",
            field=models.CharField(editable=False, max_length=200, unique=True),
        ),
        migrations.RemoveField(
            model_name="query",
            name="refresh_daily",
        ),
        migrations.RemoveField(
            model_name="report",
            name="refresh_daily",
        ),
        migrations.AddField(
            model_name="query",
            name="last_run",
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name="formatter",
            name="content_type",
            field=models.CharField(
                choices=[
                    ("csv", "text/csv"),
                    ("html", "text/html"),
                    ("json", "application/json"),
                    ("txt", "text/plain"),
                    ("xls", "application/vnd.ms-excel"),
                    ("xml", "application/xml"),
                    ("yaml", "text/yaml"),
                ],
                max_length=5,
            ),
        ),
        migrations.AlterField(
            model_name="reportdocument",
            name="content_type",
            field=models.CharField(choices=[], max_length=5),
        ),
        migrations.AlterField(
            model_name="query",
            name="info",
            field=models.JSONField(
                blank=True, default=dict, encoder=power_query.json.PQJSONEncoder
            ),
        ),
        migrations.AddField(
            model_name="report",
            name="validity_days",
            field=models.IntegerField(default=365),
        ),
        migrations.AddField(
            model_name="report",
            name="frequence",
            field=models.CharField(
                blank=True,
                default="mon,tue,wed,thu,fri,sat,sun",
                help_text="Refresh every (e.g. 3 - 1/3 - mon - 1/3,Mon)",
                max_length=30,
                null=True,
                validators=[power_query.validators.FrequencyValidator()],
            ),
        ),
        migrations.AlterField(
            model_name="formatter",
            name="content_type",
            field=models.CharField(
                choices=[
                    ("csv", "text/csv"),
                    ("html", "text/html"),
                    ("json", "application/json"),
                    ("txt", "text/plain"),
                    ("xls", "application/vnd.ms-excel"),
                    ("xml", "application/xml"),
                    ("yaml", "text/yaml"),
                ],
                max_length=5,
            ),
        ),
        migrations.AlterField(
            model_name="reportdocument",
            name="content_type",
            field=models.CharField(choices=[], max_length=5),
        ),
        migrations.AddField(
            model_name="parametrizer",
            name="source",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                related_name="+",
                to="power_query.query",
            ),
        ),
        migrations.AddField(
            model_name="query",
            name="celery_task",
            field=models.CharField(blank=True, max_length=36, null=True),
        ),
        migrations.AddField(
            model_name="report",
            name="celery_task",
            field=models.CharField(blank=True, max_length=36, null=True),
        ),
        migrations.AlterField(
            model_name="reportdocument",
            name="arguments",
            field=models.JSONField(
                default=dict, encoder=power_query.json.PQJSONEncoder
            ),
        ),
    ]
