# Generated by Django 3.2.13 on 2022-04-28 11:33

import django.core.validators
import django.db.models.deletion
import django_inet.models
import oauth2_provider.generators
from django.conf import settings
from django.db import migrations, models


def transfer_oauth_applications(apps, schema_editor):
    """
    transfers current oauth2 applications to the new table
    """

    ApplicationOrig = apps.get_model("oauth2_provider.Application")
    ApplicationNew = apps.get_model("peeringdb_server.OAuthApplication")

    for app in ApplicationOrig.objects.all().order_by("id"):
        copied_app = ApplicationNew.objects.create(
            id=app.id,
            client_id=app.client_id,
            user=app.user,
            redirect_uris=app.redirect_uris,
            client_type=app.client_type,
            authorization_grant_type=app.authorization_grant_type,
            client_secret=app.client_secret,
            name=app.name,
            skip_authorization=app.skip_authorization,
            algorithm=app.algorithm,
        )

        assert copied_app.pk == app.pk


class Migration(migrations.Migration):
    dependencies = [
        ("peeringdb_server", "0084_data_change_watchlist"),
    ]

    operations = [
        migrations.CreateModel(
            name="OAuthApplication",
            fields=[
                ("id", models.BigAutoField(primary_key=True, serialize=False)),
                (
                    "client_id",
                    models.CharField(
                        db_index=True,
                        default=oauth2_provider.generators.generate_client_id,
                        max_length=100,
                        unique=True,
                    ),
                ),
                (
                    "redirect_uris",
                    models.TextField(
                        blank=True, help_text="Allowed URIs list, space separated"
                    ),
                ),
                (
                    "client_type",
                    models.CharField(
                        choices=[
                            ("confidential", "Confidential"),
                            ("public", "Public"),
                        ],
                        max_length=32,
                    ),
                ),
                (
                    "authorization_grant_type",
                    models.CharField(
                        choices=[
                            ("authorization-code", "Authorization code"),
                            ("implicit", "Implicit"),
                            ("password", "Resource owner password-based"),
                            ("client-credentials", "Client credentials"),
                            ("openid-hybrid", "OpenID connect hybrid"),
                        ],
                        max_length=32,
                    ),
                ),
                (
                    "client_secret",
                    models.CharField(
                        blank=True,
                        db_index=True,
                        default=oauth2_provider.generators.generate_client_secret,
                        max_length=255,
                    ),
                ),
                ("name", models.CharField(blank=True, max_length=255)),
                ("skip_authorization", models.BooleanField(default=False)),
                ("created", models.DateTimeField(auto_now_add=True)),
                ("updated", models.DateTimeField(auto_now=True)),
                (
                    "algorithm",
                    models.CharField(
                        blank=True,
                        choices=[
                            ("", "No OIDC support"),
                            ("RS256", "RSA with SHA-2 256"),
                            ("HS256", "HMAC with SHA-2 256"),
                        ],
                        default="",
                        max_length=5,
                    ),
                ),
                (
                    "org",
                    models.ForeignKey(
                        blank=True,
                        help_text="application is owned by this organization",
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="oauth_applications",
                        to="peeringdb_server.organization",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="peeringdb_server_oauthapplication",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "db_table": "peeringdb_oauth_application",
                "verbose_name": "OAuth Application",
                "verbose_name_plural": "OAuth Applications",
                "abstract": False,
            },
        ),
        migrations.RunPython(transfer_oauth_applications, migrations.RunPython.noop),
    ]
