# Generated by Django

from django.db import migrations
from django.conf import settings # To access settings.AUTH_USER_MODEL if needed, though get_model is better

def forwards_func(apps, schema_editor):
    # Get the historical versions of the models
    # AUTH_USER_MODEL defaults to 'auth.User' if not set in settings.
    # Based on settings.py, AUTH_USER_MODEL is not explicitly set, so it's 'auth.User'.
    User = apps.get_model('auth', 'User')
    UserQuota = apps.get_model('api', 'UserQuota')
    
    for user in User.objects.all():
        UserQuota.objects.get_or_create(user=user)
        # Defaults for agent_limit, task_quota, and tasks_used will be applied
        # as per the UserQuota model definition.

def backwards_func(apps, schema_editor):
    # This migration creates UserQuota records with default values.
    # Reversing it by deleting these records could be complex if other operations
    # have since modified these specific UserQuota instances.
    # A simple pass is acceptable, or a more sophisticated approach could be taken
    # to only remove quotas created by this migration if identifiable.
    # For simplicity, we'll use pass.
    pass

class Migration(migrations.Migration):

    dependencies = [
        ('api', '0006_rename_agent_browseruseagent_and_more'),
        # Ensure dependency on the 'auth' app's initial migration if User model is from 'auth'
        # and if there's a possibility of running this before auth migrations.
        # However, typically, auth migrations run early.
        # For safety, if settings.AUTH_USER_MODEL is 'auth.User', include:
        # migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        # Or more explicitly if 'auth.User' is guaranteed:
        # ('auth', '0001_initial'), # Or the latest auth migration if User model changed there
        # Given that UserQuota has a ForeignKey to AUTH_USER_MODEL, Django's makemigrations
        # usually handles this correctly. For a data migration, it's good to be explicit if needed.
        # However, the main dependency is the previous 'api' migration.
        # The User model itself should exist from auth app's migrations.
    ]

    operations = [
        migrations.RunPython(forwards_func, backwards_func),
    ]
