# Generated by Django 5.2.8 on 2025-12-02 16:27

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


class Migration(migrations.Migration):

    dependencies = [
        ("django_cfg_accounts", "0007_twilioresponse"),
    ]

    operations = [
        migrations.CreateModel(
            name="OAuthState",
            fields=[
                (
                    "state",
                    models.CharField(
                        help_text="Random state token",
                        max_length=64,
                        primary_key=True,
                        serialize=False,
                        unique=True,
                    ),
                ),
                (
                    "provider",
                    models.CharField(
                        choices=[("github", "GitHub")], help_text="OAuth provider", max_length=20
                    ),
                ),
                (
                    "redirect_uri",
                    models.URLField(help_text="Redirect URI for callback", max_length=500),
                ),
                (
                    "source_url",
                    models.URLField(
                        blank=True, help_text="Optional source URL for tracking", max_length=500
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, help_text="When this state was created"
                    ),
                ),
                ("expires_at", models.DateTimeField(help_text="When this state expires")),
            ],
            options={
                "verbose_name": "OAuth State",
                "verbose_name_plural": "OAuth States",
                "db_table": "django_cfg_oauth_state",
                "indexes": [
                    models.Index(fields=["expires_at"], name="django_cfg__expires_c4f92e_idx")
                ],
            },
        ),
        migrations.CreateModel(
            name="OAuthConnection",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "provider",
                    models.CharField(
                        choices=[("github", "GitHub")],
                        db_index=True,
                        help_text="OAuth provider name (github, google, etc.)",
                        max_length=20,
                    ),
                ),
                (
                    "provider_user_id",
                    models.CharField(help_text="User ID from the OAuth provider", max_length=100),
                ),
                (
                    "provider_email",
                    models.EmailField(
                        blank=True,
                        help_text="Email from OAuth provider (may differ from user.email)",
                        max_length=254,
                    ),
                ),
                (
                    "provider_username",
                    models.CharField(
                        blank=True,
                        help_text="Username on the OAuth provider platform",
                        max_length=100,
                    ),
                ),
                (
                    "provider_avatar_url",
                    models.URLField(
                        blank=True, help_text="Avatar URL from OAuth provider", max_length=500
                    ),
                ),
                (
                    "provider_name",
                    models.CharField(
                        blank=True, help_text="Display name from OAuth provider", max_length=200
                    ),
                ),
                ("access_token", models.TextField(help_text="OAuth access token")),
                (
                    "refresh_token",
                    models.TextField(blank=True, help_text="OAuth refresh token (if available)"),
                ),
                (
                    "token_expires_at",
                    models.DateTimeField(
                        blank=True, help_text="When the access token expires", null=True
                    ),
                ),
                (
                    "scopes",
                    models.JSONField(blank=True, default=list, help_text="OAuth scopes granted"),
                ),
                (
                    "raw_data",
                    models.JSONField(
                        blank=True, default=dict, help_text="Raw user data from OAuth provider"
                    ),
                ),
                (
                    "connected_at",
                    models.DateTimeField(
                        auto_now_add=True, help_text="When this OAuth connection was created"
                    ),
                ),
                (
                    "updated_at",
                    models.DateTimeField(
                        auto_now=True, help_text="When this OAuth connection was last updated"
                    ),
                ),
                (
                    "last_login_at",
                    models.DateTimeField(
                        default=django.utils.timezone.now,
                        help_text="Last time this OAuth connection was used for login",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        help_text="The Django user this OAuth account is linked to",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="oauth_connections",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "OAuth Connection",
                "verbose_name_plural": "OAuth Connections",
                "db_table": "django_cfg_oauth_connection",
                "ordering": ["-connected_at"],
                "indexes": [
                    models.Index(
                        fields=["provider", "provider_user_id"],
                        name="django_cfg__provide_a29614_idx",
                    ),
                    models.Index(
                        fields=["user", "provider"], name="django_cfg__user_id_e39dae_idx"
                    ),
                    models.Index(fields=["provider_email"], name="django_cfg__provide_696724_idx"),
                ],
                "unique_together": {("provider", "provider_user_id")},
            },
        ),
    ]
