# Generated by Django 5.2.8 on 2025-11-30 00:49

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


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0009_add_feature_limits'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='OAuthCredential',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('provider', models.CharField(choices=[('google', 'Google'), ('microsoft', 'Microsoft')], help_text='OAuth provider (google, microsoft)', max_length=20)),
                ('purpose', models.CharField(choices=[('email', 'Email Access (IMAP/SMTP)'), ('calendar', 'Calendar Sync'), ('drive', 'File Storage')], help_text='What this credential is used for', max_length=20)),
                ('email', models.EmailField(help_text='Email address associated with this OAuth credential', max_length=254)),
                ('access_token', models.TextField(help_text='OAuth access token')),
                ('refresh_token', models.TextField(blank=True, default='', help_text='OAuth refresh token (for token renewal)')),
                ('token_expiry', 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 by the user')),
                ('is_valid', models.BooleanField(default=True, help_text='Whether this credential is currently valid')),
                ('last_used_at', models.DateTimeField(blank=True, help_text='When this credential was last used', null=True)),
                ('last_error', models.TextField(blank=True, default='', help_text='Last error message if token refresh failed')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('authorized_by', models.ForeignKey(blank=True, help_text='User who authorized this OAuth connection.', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='authorized_oauth_credentials', to=settings.AUTH_USER_MODEL)),
                ('tenant', models.ForeignKey(blank=True, help_text='Tenant this credential belongs to. Null for platform-level credentials.', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='service_oauth_credentials', to='core.tenant')),
            ],
            options={
                'ordering': ['-created_at'],
                'indexes': [models.Index(fields=['tenant', 'provider', 'purpose'], name='core_oauthc_tenant__1c2893_idx'), models.Index(fields=['email'], name='core_oauthc_email_dab500_idx')],
                'unique_together': {('tenant', 'email', 'purpose')},
            },
        ),
    ]
