# Generated by Codex on 2025-10-24

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


class Migration(migrations.Migration):

    dependencies = [
        ('api', '0171_mcpserverconfig_auth_method'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='MCPServerOAuthCredential',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('client_id', models.CharField(blank=True, max_length=256)),
                ('client_secret_encrypted', models.BinaryField(blank=True, null=True)),
                ('access_token_encrypted', models.BinaryField(blank=True, null=True)),
                ('refresh_token_encrypted', models.BinaryField(blank=True, null=True)),
                ('id_token_encrypted', models.BinaryField(blank=True, null=True)),
                ('token_type', models.CharField(blank=True, max_length=32)),
                ('scope', models.CharField(blank=True, max_length=512)),
                ('expires_at', models.DateTimeField(blank=True, null=True)),
                ('metadata', models.JSONField(blank=True, default=dict)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('organization', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='mcp_oauth_credentials', to='api.organization')),
                ('server_config', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='oauth_credential', to='api.mcpserverconfig')),
                ('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='mcp_oauth_credentials', to=settings.AUTH_USER_MODEL)),
            ],
        ),
        migrations.CreateModel(
            name='MCPServerOAuthSession',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('state', models.CharField(max_length=255, unique=True)),
                ('redirect_uri', models.CharField(blank=True, max_length=512)),
                ('scope', models.CharField(blank=True, max_length=512)),
                ('code_challenge', models.CharField(blank=True, max_length=255)),
                ('code_challenge_method', models.CharField(blank=True, max_length=32)),
                ('code_verifier_encrypted', models.BinaryField(blank=True, null=True)),
                ('token_endpoint', models.CharField(blank=True, max_length=512)),
                ('client_id', models.CharField(blank=True, max_length=256)),
                ('client_secret_encrypted', models.BinaryField(blank=True, null=True)),
                ('metadata', models.JSONField(blank=True, default=dict)),
                ('expires_at', models.DateTimeField()),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('initiated_by', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='mcp_oauth_sessions', to=settings.AUTH_USER_MODEL)),
                ('organization', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='mcp_oauth_sessions', to='api.organization')),
                ('server_config', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='oauth_sessions', to='api.mcpserverconfig')),
                ('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='mcp_oauth_user_sessions', to=settings.AUTH_USER_MODEL)),
            ],
        ),
        migrations.AddIndex(
            model_name='mcpserveroauthcredential',
            index=models.Index(fields=['organization'], name='mcp_oauth_credential_org_idx'),
        ),
        migrations.AddIndex(
            model_name='mcpserveroauthcredential',
            index=models.Index(fields=['user'], name='mcp_oauth_credential_user_idx'),
        ),
        migrations.AddIndex(
            model_name='mcpserveroauthsession',
            index=models.Index(fields=['expires_at'], name='mcp_oauth_session_expiry_idx'),
        ),
    ]
