# Generated by Django 2.2.19 on 2021-03-24 22:07

from django.db import migrations


def add_validated_repo(apps, schema_editor):
    """Created validated repos"""

    AnsibleRepository = apps.get_model('ansible', 'AnsibleRepository')
    AnsibleDistribution = apps.get_model('ansible', 'AnsibleDistribution')
    RepositoryVersion = apps.get_model('core', 'RepositoryVersion')

    repo_data = {
        "name": "validated",
        "description": "Validated collections",
        "pulp_type": "ansible.ansible",
        "next_version": 1,
    }

    repository, _ = AnsibleRepository.objects.get_or_create(
        name=repo_data["name"],
        defaults=repo_data
    )

    if not RepositoryVersion.objects.filter(repository__name=repository.name):
        RepositoryVersion.objects.create(
            repository=repository,
            number=0,
            complete=True
        )

    AnsibleDistribution.objects.get_or_create(
        base_path=repository.name,
        defaults={
            "name": repository.name,
            "base_path": repository.name,
            "repository": repository,
            "pulp_type": "ansible.ansible"
        }
    )


class Migration(migrations.Migration):

    dependencies = [
        ('galaxy', '0031_legacynamespace_legacyrole'),
    ]

    operations = [
        migrations.RunPython(
            code=add_validated_repo,
            reverse_code=migrations.RunPython.noop
        )
    ]
