# Generated by Django 4.2.13 on 2024-06-29 03:41

import django.db.models.deletion
import django.db.models.functions.text
import django.utils.timezone
from django.conf import settings
from django.db import migrations, models
from tqdm import tqdm

from catalog.common import jsondata


def move_masto_email(apps, schema_editor):
    User = apps.get_model("users", "User")
    MastodonAccount = apps.get_model("mastodon", "MastodonAccount")
    EmailAccount = apps.get_model("mastodon", "EmailAccount")
    m = 0
    e = 0
    qs = User.objects.filter(username__isnull=True)
    print(f"Deleting {qs.count()} nameless users.")
    qs.delete()
    for user in tqdm(User.objects.filter(is_active=True)):
        if user.mastodon_username:
            MastodonAccount.objects.update_or_create(
                handle=f"{user.mastodon_username}@{user.mastodon_site}",
                defaults={
                    "user": user,
                    "uid": user.mastodon_id,
                    "domain": user.mastodon_site,
                    "created": user.date_joined,
                    "last_refresh": user.mastodon_last_refresh,
                    "last_reachable": user.mastodon_last_reachable,
                    "followers": user.mastodon_followers,
                    "following": user.mastodon_following,
                    "blocks": user.mastodon_blocks,
                    "mutes": user.mastodon_mutes,
                    "domain_blocks": user.mastodon_domain_blocks,
                    "account_data": user.mastodon_account,
                    "access_data": {
                        "access_token": jsondata.encrypt_str(user.mastodon_token)
                    },
                },
            )
            m += 1
        if user.email:
            EmailAccount.objects.update_or_create(
                handle=user.email,
                defaults={
                    "user": user,
                    "uid": user.email.split("@")[0],
                    "domain": user.email.split("@")[1],
                    "created": user.date_joined,
                },
            )
            e += 1
    print(f"{m} Mastodon, {e} Email migrated.")


class Migration(migrations.Migration):
    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("mastodon", "0004_alter_mastodonapplication_api_domain_and_more"),
    ]

    operations = [
        migrations.CreateModel(
            name="SocialAccount",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "type",
                    models.CharField(
                        choices=[
                            ("mastodon.blueskyaccount", "bluesky account"),
                            ("mastodon.emailaccount", "email account"),
                            ("mastodon.mastodonaccount", "mastodon account"),
                            ("mastodon.threadsaccount", "threads account"),
                        ],
                        db_index=True,
                        max_length=255,
                    ),
                ),
                ("domain", models.CharField(max_length=255)),
                ("uid", models.CharField(max_length=255)),
                ("handle", models.CharField(max_length=1000)),
                ("access_data", models.JSONField(default=dict)),
                ("account_data", models.JSONField(default=dict)),
                ("preference_data", models.JSONField(default=dict)),
                ("followers", models.JSONField(default=list)),
                ("following", models.JSONField(default=list)),
                ("mutes", models.JSONField(default=list)),
                ("blocks", models.JSONField(default=list)),
                ("domain_blocks", models.JSONField(default=list)),
                ("created", models.DateTimeField(default=django.utils.timezone.now)),
                ("modified", models.DateTimeField(auto_now=True)),
                ("last_refresh", models.DateTimeField(default=None, null=True)),
                ("last_reachable", models.DateTimeField(default=None, null=True)),
                (
                    "user",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="social_accounts",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="BlueskyAccount",
            fields=[],
            options={
                "proxy": True,
                "indexes": [],
                "constraints": [],
            },
            bases=("mastodon.socialaccount",),
        ),
        migrations.CreateModel(
            name="EmailAccount",
            fields=[],
            options={
                "proxy": True,
                "indexes": [],
                "constraints": [],
            },
            bases=("mastodon.socialaccount",),
        ),
        migrations.CreateModel(
            name="MastodonAccount",
            fields=[],
            options={
                "proxy": True,
                "indexes": [],
                "constraints": [],
            },
            bases=("mastodon.socialaccount",),
        ),
        migrations.CreateModel(
            name="ThreadsAccount",
            fields=[],
            options={
                "proxy": True,
                "indexes": [],
                "constraints": [],
            },
            bases=("mastodon.socialaccount",),
        ),
        migrations.AddIndex(
            model_name="socialaccount",
            index=models.Index(
                fields=["type", "handle"], name="index_social_type_handle"
            ),
        ),
        migrations.AddIndex(
            model_name="socialaccount",
            index=models.Index(
                fields=["type", "domain", "uid"], name="index_social_type_domain_uid"
            ),
        ),
        migrations.AddConstraint(
            model_name="socialaccount",
            constraint=models.UniqueConstraint(
                django.db.models.functions.text.Lower("domain"),
                django.db.models.functions.text.Lower("uid"),
                name="unique_social_domain_uid",
            ),
        ),
        migrations.AddConstraint(
            model_name="socialaccount",
            constraint=models.UniqueConstraint(
                models.F("type"),
                django.db.models.functions.text.Lower("handle"),
                name="unique_social_type_handle",
            ),
        ),
        migrations.RunPython(move_masto_email),
    ]
