# Generated by Django 4.2.11 on 2024-06-07 12:04

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


def migrate_pages(apps, schema_editor):
    ProjectPage = apps.get_model("db", "ProjectPage")
    Page = apps.get_model("db", "Page")
    ProjectPage.objects.bulk_create(
        [
            ProjectPage(
                workspace_id=page.get("workspace_id"),
                project_id=page.get("project_id"),
                page_id=page.get("id"),
                created_by_id=page.get("created_by_id"),
                updated_by_id=page.get("updated_by_id"),
            )
            for page in Page.objects.values(
                "workspace_id",
                "project_id",
                "id",
                "created_by_id",
                "updated_by_id",
            )
        ],
        batch_size=1000,
    )


class Migration(migrations.Migration):

    dependencies = [
        ("db", "0067_issue_estimate"),
    ]

    operations = [
        migrations.AddField(
            model_name="page",
            name="is_global",
            field=models.BooleanField(default=False),
        ),
        migrations.CreateModel(
            name="ProjectPage",
            fields=[
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, verbose_name="Created At"
                    ),
                ),
                (
                    "updated_at",
                    models.DateTimeField(
                        auto_now=True, verbose_name="Last Modified At"
                    ),
                ),
                (
                    "id",
                    models.UUIDField(
                        db_index=True,
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                        unique=True,
                    ),
                ),
                (
                    "created_by",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="%(class)s_created_by",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Created By",
                    ),
                ),
                (
                    "page",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="project_pages",
                        to="db.page",
                    ),
                ),
                (
                    "project",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="project_pages",
                        to="db.project",
                    ),
                ),
                (
                    "updated_by",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="%(class)s_updated_by",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Last Modified By",
                    ),
                ),
                (
                    "workspace",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="project_pages",
                        to="db.workspace",
                    ),
                ),
            ],
            options={
                "verbose_name": "Project Page",
                "verbose_name_plural": "Project Pages",
                "db_table": "project_pages",
                "ordering": ("-created_at",),
                "unique_together": {("project", "page")},
            },
        ),
        migrations.CreateModel(
            name="TeamPage",
            fields=[
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, verbose_name="Created At"
                    ),
                ),
                (
                    "updated_at",
                    models.DateTimeField(
                        auto_now=True, verbose_name="Last Modified At"
                    ),
                ),
                (
                    "id",
                    models.UUIDField(
                        db_index=True,
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                        unique=True,
                    ),
                ),
                (
                    "created_by",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="%(class)s_created_by",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Created By",
                    ),
                ),
                (
                    "page",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="team_pages",
                        to="db.page",
                    ),
                ),
                (
                    "team",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="team_pages",
                        to="db.team",
                    ),
                ),
                (
                    "updated_by",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="%(class)s_updated_by",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Last Modified By",
                    ),
                ),
                (
                    "workspace",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="team_pages",
                        to="db.workspace",
                    ),
                ),
            ],
            options={
                "verbose_name": "Team Page",
                "verbose_name_plural": "Team Pages",
                "db_table": "team_pages",
                "ordering": ("-created_at",),
                "unique_together": {("team", "page")},
            },
        ),
        migrations.AddField(
            model_name="page",
            name="projects",
            field=models.ManyToManyField(
                related_name="pages", through="db.ProjectPage", to="db.project"
            ),
        ),
        migrations.AddField(
            model_name="page",
            name="teams",
            field=models.ManyToManyField(
                related_name="pages", through="db.TeamPage", to="db.team"
            ),
        ),
        migrations.RunPython(migrate_pages),
        migrations.RemoveField(
            model_name="page",
            name="project",
        ),
        migrations.AlterField(
            model_name="page",
            name="workspace",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="pages",
                to="db.workspace",
            ),
        ),
        migrations.RemoveField(
            model_name="pagelabel",
            name="project",
        ),
        migrations.RemoveField(
            model_name="pagelog",
            name="project",
        ),
        migrations.AlterField(
            model_name="pagelabel",
            name="workspace",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="workspace_page_label",
                to="db.workspace",
            ),
        ),
        migrations.AlterField(
            model_name="pagelog",
            name="workspace",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="workspace_page_log",
                to="db.workspace",
            ),
        ),
    ]
