# Generated by Django 4.2.11 on 2024-04-22 13:19

import uuid

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


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("sinp_nomenclatures", "0001_initial"),
    ]

    operations = [
        migrations.CreateModel(
            name="Organism",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("timestamp_create", models.DateTimeField(auto_now_add=True)),
                ("timestamp_update", models.DateTimeField(auto_now=True)),
                (
                    "uuid",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        unique=True,
                        verbose_name="Identifiant unique",
                    ),
                ),
                (
                    "administrative_reference",
                    models.CharField(
                        blank=True, max_length=100, verbose_name="SIRET"
                    ),
                ),
                (
                    "label",
                    models.CharField(
                        max_length=500, unique=True, verbose_name="Nom"
                    ),
                ),
                (
                    "short_label",
                    models.CharField(
                        max_length=50, unique=True, verbose_name="Nom court"
                    ),
                ),
                (
                    "geographic_area_details",
                    models.CharField(
                        blank=True,
                        help_text=(
                            "Information précisant "
                            "la zone géographique d'action."
                        ),
                        max_length=500,
                        verbose_name="Détails sur la zone géographique",
                    ),
                ),
                (
                    "address",
                    models.CharField(
                        blank=True, max_length=500, verbose_name="Adresse"
                    ),
                ),
                (
                    "postal_code",
                    models.CharField(
                        blank=True, max_length=20, verbose_name="Code postal"
                    ),
                ),
                (
                    "municipality",
                    models.CharField(
                        blank=True, max_length=250, verbose_name="Commune"
                    ),
                ),
                (
                    "email",
                    models.EmailField(
                        blank=True, max_length=254, verbose_name="Adresse mail"
                    ),
                ),
                (
                    "phone_number",
                    models.CharField(
                        blank=True,
                        max_length=17,
                        validators=[
                            django.core.validators.RegexValidator(
                                message=(
                                    "Les numéros de téléphones doivent "
                                    "être renseignés avec le format :"
                                    "'+999999999'. jusqu'à 15 chiffres "
                                    "sont autorisés"
                                ),
                                regex="^\\+?1?\\d{9,15}$",
                            )
                        ],
                        verbose_name="Numéro de téléphone",
                    ),
                ),
                ("url", models.URLField(blank=True, verbose_name="URL")),
                (
                    "enabled",
                    models.BooleanField(default=True, verbose_name="Actif"),
                ),
                (
                    "extra_data",
                    models.JSONField(
                        blank=True, null=True, verbose_name="Additional datas"
                    ),
                ),
                (
                    "action_scope",
                    models.ForeignKey(
                        help_text="Périmètre d'action de l'organisme",
                        limit_choices_to={"type__mnemonic": "action_scope"},
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="organism_action_scope",
                        to="sinp_nomenclatures.nomenclature",
                        verbose_name="Périmètre d'action",
                    ),
                ),
                (
                    "geographic_area",
                    models.ManyToManyField(
                        blank=True,
                        help_text="Zone d'action géographique de l'organisme",
                        limit_choices_to={"type__mnemonic": "geographic_area"},
                        related_name="organism_geographic_area",
                        to="sinp_nomenclatures.nomenclature",
                        verbose_name="Zone géographique",
                    ),
                ),
            ],
            options={
                "verbose_name_plural": "organismes",
            },
        ),
        migrations.CreateModel(
            name="OrganismMember",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("timestamp_create", models.DateTimeField(auto_now_add=True)),
                ("timestamp_update", models.DateTimeField(auto_now=True)),
                (
                    "member",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Utilisateur",
                    ),
                ),
                (
                    "member_level",
                    models.ForeignKey(
                        limit_choices_to={"type__mnemonic": "member_level"},
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="member_level",
                        to="sinp_nomenclatures.nomenclature",
                        verbose_name="Niveau du membre",
                    ),
                ),
                (
                    "organism",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="sinp_organisms.organism",
                        verbose_name="Organisme",
                    ),
                ),
            ],
            options={
                "verbose_name_plural": "Membre des organismes",
                "unique_together": {("member", "organism", "member_level")},
            },
        ),
        migrations.AddField(
            model_name="organism",
            name="members",
            field=models.ManyToManyField(
                blank=True,
                related_name="organism_member",
                through="sinp_organisms.OrganismMember",
                to=settings.AUTH_USER_MODEL,
                verbose_name="Membres",
            ),
        ),
        migrations.AddField(
            model_name="organism",
            name="parent",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                to="sinp_organisms.organism",
                verbose_name="Organisme parent",
            ),
        ),
        migrations.AddField(
            model_name="organism",
            name="status",
            field=models.ForeignKey(
                help_text=(
                    "Permet d'indiquer si l'organisme est public ou privé"
                ),
                limit_choices_to={"type__mnemonic": "organism_status"},
                on_delete=django.db.models.deletion.DO_NOTHING,
                related_name="organism_status",
                to="sinp_nomenclatures.nomenclature",
                verbose_name="Statut",
            ),
        ),
        migrations.AddField(
            model_name="organism",
            name="type",
            field=models.ForeignKey(
                limit_choices_to={"type__mnemonic": "organism_type"},
                on_delete=django.db.models.deletion.DO_NOTHING,
                related_name="organism_type",
                to="sinp_nomenclatures.nomenclature",
                verbose_name="Type d'organisme",
            ),
        ),
        migrations.AddConstraint(
            model_name="organism",
            constraint=models.UniqueConstraint(
                fields=("uuid",), name="unique_uuid"
            ),
        ),
    ]
