# Generated by Django 5.2.13 on 2026-04-24 10:29

import apps.core.storage.paths
import django.contrib.postgres.fields
import django.db.models.deletion
import simple_history.models
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ("core", "0014_alter_customuser_role"),
        ("repository_documents", "0001_initial"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="AlumniProfile",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("current_employer", models.CharField(blank=True, max_length=255)),
                ("current_position", models.CharField(blank=True, max_length=255)),
                (
                    "orcid_id",
                    models.CharField(
                        blank=True,
                        db_index=True,
                        help_text="ORCID iD in 0000-0000-0000-0000 form.",
                        max_length=19,
                    ),
                ),
                ("linkedin_url", models.URLField(blank=True)),
                ("twitter_url", models.URLField(blank=True)),
                ("website_url", models.URLField(blank=True)),
                ("researchgate_url", models.URLField(blank=True)),
                (
                    "photo",
                    models.FileField(
                        blank=True,
                        null=True,
                        upload_to=apps.core.storage.paths.upload_to_alumni_photos,
                    ),
                ),
                (
                    "headline",
                    models.CharField(
                        blank=True,
                        help_text="Short alumni tagline, distinct from the core UserProfile bio.",
                        max_length=200,
                    ),
                ),
                (
                    "expertise_tags",
                    django.contrib.postgres.fields.ArrayField(
                        base_field=models.CharField(max_length=64),
                        blank=True,
                        default=list,
                        help_text="Tags used by the mentorship matcher (e.g. 'agronomy', 'policy').",
                        size=None,
                    ),
                ),
                (
                    "visibility_consent",
                    models.BooleanField(
                        default=False, help_text="Opt-in for public alumni directory (GDPR)."
                    ),
                ),
                ("published_at", models.DateTimeField(blank=True, null=True)),
                (
                    "source",
                    models.CharField(
                        choices=[
                            ("rims_scholarship", "RIMS scholarship"),
                            ("rep_completion", "REP course completion"),
                            ("mixed", "Mixed (RIMS + REP)"),
                            ("manual", "Manual"),
                            ("csv_import", "CSV import"),
                        ],
                        default="manual",
                        max_length=24,
                    ),
                ),
                (
                    "source_refs",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        help_text="Lineage e.g. {'graduation_ids': [12], 'rep_enrolment_ids': [340]}",
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "current_institution",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="alumni",
                        to="core.institution",
                    ),
                ),
                (
                    "user",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="alumni_profile",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "ordering": ["-published_at", "user__last_name"],
            },
        ),
        migrations.CreateModel(
            name="AlumniCertificate",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("title", models.CharField(max_length=255)),
                (
                    "issuer",
                    models.CharField(
                        blank=True,
                        help_text="Issuing body; use 'RUFORUM' for institute recognitions.",
                        max_length=255,
                    ),
                ),
                ("issued_on", models.DateField(blank=True, null=True)),
                (
                    "file",
                    models.FileField(
                        blank=True,
                        null=True,
                        upload_to=apps.core.storage.paths.upload_to_alumni_certificates,
                    ),
                ),
                ("verification_url", models.URLField(blank=True)),
                (
                    "rep_certificate_number",
                    models.CharField(
                        blank=True,
                        db_index=True,
                        help_text="Soft reference to rep.courses.Certificate.certificate_number.",
                        max_length=64,
                    ),
                ),
                (
                    "profile",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="certificates",
                        to="alumni_profiles.alumniprofile",
                    ),
                ),
            ],
            options={
                "ordering": ["-issued_on"],
            },
        ),
        migrations.CreateModel(
            name="Degree",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("programme_name", models.CharField(max_length=255)),
                (
                    "degree_type",
                    models.CharField(
                        choices=[
                            ("phd", "PhD"),
                            ("msc", "MSc"),
                            ("ma", "MA"),
                            ("bsc", "BSc"),
                            ("ba", "BA"),
                            ("diploma", "Diploma"),
                            ("certificate", "Certificate"),
                            ("other", "Other"),
                        ],
                        default="other",
                        max_length=16,
                    ),
                ),
                ("started_on", models.DateField(blank=True, null=True)),
                ("graduated_on", models.DateField()),
                ("thesis_title", models.CharField(blank=True, max_length=500)),
                ("advisor_name", models.CharField(blank=True, max_length=255)),
                ("funder", models.CharField(blank=True, max_length=255)),
                (
                    "institution",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="alumni_degrees",
                        to="core.institution",
                    ),
                ),
                (
                    "profile",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="degrees",
                        to="alumni_profiles.alumniprofile",
                    ),
                ),
            ],
            options={
                "ordering": ["-graduated_on"],
            },
        ),
        migrations.CreateModel(
            name="Employment",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("organisation", models.CharField(max_length=255)),
                ("position", models.CharField(max_length=255)),
                ("sector", models.CharField(blank=True, max_length=128)),
                (
                    "country",
                    models.CharField(blank=True, help_text="ISO 3166-1 alpha-2", max_length=2),
                ),
                ("started_on", models.DateField()),
                ("ended_on", models.DateField(blank=True, null=True)),
                ("description", models.TextField(blank=True)),
                (
                    "profile",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="employments",
                        to="alumni_profiles.alumniprofile",
                    ),
                ),
            ],
            options={
                "ordering": ["-started_on"],
            },
        ),
        migrations.CreateModel(
            name="HistoricalAlumniProfile",
            fields=[
                (
                    "id",
                    models.BigIntegerField(
                        auto_created=True, blank=True, db_index=True, verbose_name="ID"
                    ),
                ),
                ("current_employer", models.CharField(blank=True, max_length=255)),
                ("current_position", models.CharField(blank=True, max_length=255)),
                (
                    "orcid_id",
                    models.CharField(
                        blank=True,
                        db_index=True,
                        help_text="ORCID iD in 0000-0000-0000-0000 form.",
                        max_length=19,
                    ),
                ),
                ("linkedin_url", models.URLField(blank=True)),
                ("twitter_url", models.URLField(blank=True)),
                ("website_url", models.URLField(blank=True)),
                ("researchgate_url", models.URLField(blank=True)),
                ("photo", models.TextField(blank=True, max_length=100, null=True)),
                (
                    "headline",
                    models.CharField(
                        blank=True,
                        help_text="Short alumni tagline, distinct from the core UserProfile bio.",
                        max_length=200,
                    ),
                ),
                (
                    "expertise_tags",
                    django.contrib.postgres.fields.ArrayField(
                        base_field=models.CharField(max_length=64),
                        blank=True,
                        default=list,
                        help_text="Tags used by the mentorship matcher (e.g. 'agronomy', 'policy').",
                        size=None,
                    ),
                ),
                (
                    "visibility_consent",
                    models.BooleanField(
                        default=False, help_text="Opt-in for public alumni directory (GDPR)."
                    ),
                ),
                ("published_at", models.DateTimeField(blank=True, null=True)),
                (
                    "source",
                    models.CharField(
                        choices=[
                            ("rims_scholarship", "RIMS scholarship"),
                            ("rep_completion", "REP course completion"),
                            ("mixed", "Mixed (RIMS + REP)"),
                            ("manual", "Manual"),
                            ("csv_import", "CSV import"),
                        ],
                        default="manual",
                        max_length=24,
                    ),
                ),
                (
                    "source_refs",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        help_text="Lineage e.g. {'graduation_ids': [12], 'rep_enrolment_ids': [340]}",
                    ),
                ),
                ("created_at", models.DateTimeField(blank=True, editable=False)),
                ("updated_at", models.DateTimeField(blank=True, editable=False)),
                ("history_id", models.AutoField(primary_key=True, serialize=False)),
                ("history_date", models.DateTimeField(db_index=True)),
                ("history_change_reason", models.CharField(max_length=100, null=True)),
                (
                    "history_type",
                    models.CharField(
                        choices=[("+", "Created"), ("~", "Changed"), ("-", "Deleted")], max_length=1
                    ),
                ),
                (
                    "current_institution",
                    models.ForeignKey(
                        blank=True,
                        db_constraint=False,
                        null=True,
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="+",
                        to="core.institution",
                    ),
                ),
                (
                    "history_user",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        blank=True,
                        db_constraint=False,
                        null=True,
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "historical alumni profile",
                "verbose_name_plural": "historical alumni profiles",
                "ordering": ("-history_date", "-history_id"),
                "get_latest_by": ("history_date", "history_id"),
            },
            bases=(simple_history.models.HistoricalChanges, models.Model),
        ),
        migrations.CreateModel(
            name="Publication",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("title", models.CharField(max_length=500)),
                ("authors_text", models.TextField(blank=True)),
                (
                    "publication_type",
                    models.CharField(
                        choices=[
                            ("journal", "Journal article"),
                            ("book", "Book"),
                            ("chapter", "Book chapter"),
                            ("conference", "Conference paper"),
                            ("preprint", "Preprint"),
                            ("report", "Report"),
                            ("other", "Other"),
                        ],
                        default="journal",
                        max_length=16,
                    ),
                ),
                ("doi", models.CharField(blank=True, max_length=128)),
                ("url", models.URLField(blank=True)),
                ("year", models.PositiveSmallIntegerField(blank=True, null=True)),
                ("cited_in_repository", models.BooleanField(default=False)),
                (
                    "document",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="alumni_publications",
                        to="repository_documents.document",
                    ),
                ),
                (
                    "profile",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="publications",
                        to="alumni_profiles.alumniprofile",
                    ),
                ),
            ],
            options={
                "ordering": ["-year", "title"],
            },
        ),
        migrations.AddIndex(
            model_name="alumniprofile",
            index=models.Index(
                fields=["visibility_consent", "published_at"], name="alumni_prof_visibil_b973ae_idx"
            ),
        ),
        migrations.AddConstraint(
            model_name="alumniprofile",
            constraint=models.UniqueConstraint(
                condition=models.Q(("orcid_id", ""), _negated=True),
                fields=("orcid_id",),
                name="uniq_alumni_orcid_when_set",
            ),
        ),
        migrations.AddConstraint(
            model_name="alumnicertificate",
            constraint=models.UniqueConstraint(
                condition=models.Q(("rep_certificate_number", ""), _negated=True),
                fields=("profile", "rep_certificate_number"),
                name="uniq_certificate_per_profile_rep_number",
            ),
        ),
        migrations.AddConstraint(
            model_name="degree",
            constraint=models.UniqueConstraint(
                fields=("profile", "programme_name", "graduated_on"),
                name="uniq_degree_per_profile_programme_date",
            ),
        ),
        migrations.AddConstraint(
            model_name="publication",
            constraint=models.UniqueConstraint(
                condition=models.Q(("doi", ""), _negated=True),
                fields=("profile", "doi"),
                name="uniq_publication_per_profile_doi",
            ),
        ),
    ]
