"""Old-system parity: legacy scholarships app repeating child tables
(Employmenthistory, Leadershipposition, Parent, Publication/
ResearchAndPublication, Communityservice, Currentvolunteering,
Groupassociationclub, Householdincomesource, Additionalfundingsource,
Referenceletter, Transcriptfile, Mastercardeducation/Othereducation).

ScholarshipApplicationProfile only has flat boolean flags for these
categories (e.g. held_leadership_position) — these child tables restore the
repeating detail behind those flags, so an applicant can record e.g.
multiple parents or multiple prior jobs.
"""
import datetime
import uuid

import pytest
from django.utils import timezone

from apps.rims.grants.models import (
    ApplicantCommunityService,
    ApplicantEmployment,
    ApplicantFundingSource,
    ApplicantGroupMembership,
    ApplicantIncomeSource,
    ApplicantLeadershipPosition,
    ApplicantParent,
    ApplicantPriorEducation,
    ApplicantPublication,
    ApplicantReference,
    ApplicantTranscript,
    ApplicantVolunteering,
    Application,
    GrantCall,
)


@pytest.fixture
def scholarship_application(applicant_user, institution):
    call = GrantCall.objects.create(
        title="Child records call",
        slug=f"child-records-{uuid.uuid4().hex[:8]}",
        call_type=GrantCall.CallType.SCHOLARSHIP,
        opens_at=timezone.now(),
        closes_at=timezone.now() + datetime.timedelta(days=30),
        status=GrantCall.Status.PUBLISHED,
    )
    return Application.objects.create(call=call, applicant=applicant_user, institution=institution)


@pytest.mark.django_db
def test_multiple_parents_per_application(scholarship_application):
    ApplicantParent.objects.create(
        application=scholarship_application,
        full_name="John Doe",
        relationship=ApplicantParent.Relationship.FATHER,
        status=ApplicantParent.Status.ALIVE,
    )
    ApplicantParent.objects.create(
        application=scholarship_application,
        full_name="Jane Doe",
        relationship=ApplicantParent.Relationship.MOTHER,
        status=ApplicantParent.Status.DECEASED,
        date_of_death=datetime.date(2020, 1, 1),
    )
    assert scholarship_application.parents.count() == 2


@pytest.mark.django_db
def test_multiple_employment_records(scholarship_application):
    ApplicantEmployment.objects.create(
        application=scholarship_application,
        organisation="NGO A",
        job_title="Field Officer",
        employed_from=datetime.date(2019, 1, 1),
        employed_to=datetime.date(2021, 1, 1),
    )
    ApplicantEmployment.objects.create(
        application=scholarship_application,
        organisation="NGO B",
        job_title="Programme Assistant",
    )
    assert scholarship_application.employment_history.count() == 2


@pytest.mark.django_db
def test_all_other_child_records_attach(scholarship_application):
    ApplicantLeadershipPosition.objects.create(
        application=scholarship_application, position="Chair", group_name="Youth Club", start_year=2020
    )
    ApplicantPublication.objects.create(
        application=scholarship_application, title="Soil health in East Africa",
        category=ApplicantPublication.Category.RESEARCH,
    )
    ApplicantCommunityService.objects.create(
        application=scholarship_application, activity="Tree planting", group_name="Green Uganda", start_year=2021
    )
    ApplicantVolunteering.objects.create(application=scholarship_application, involvement="Weekend tutoring")
    ApplicantGroupMembership.objects.create(application=scholarship_application, name="Rotaract Club", role="Member")
    ApplicantIncomeSource.objects.create(
        application=scholarship_application, source="Family farm", amount_range="USD 0-500"
    )
    ApplicantFundingSource.objects.create(
        application=scholarship_application, already_funded=True, source_of_funding="District bursary"
    )
    ApplicantReference.objects.create(
        application=scholarship_application, referee_name="Dr Smith", contact_details="smith@example.com"
    )
    ApplicantTranscript.objects.create(application=scholarship_application, file="transcripts/test.pdf")
    ApplicantPriorEducation.objects.create(
        application=scholarship_application, qualification="A-Level", institution="Test High School",
        year_of_completion=2022,
    )

    assert scholarship_application.leadership_positions.count() == 1
    assert scholarship_application.publications.count() == 1
    assert scholarship_application.community_service.count() == 1
    assert scholarship_application.current_volunteering.count() == 1
    assert scholarship_application.group_memberships.count() == 1
    assert scholarship_application.household_income_sources.count() == 1
    assert scholarship_application.additional_funding_sources.count() == 1
    assert scholarship_application.reference_letters.count() == 1
    assert scholarship_application.transcripts.count() == 1
    assert scholarship_application.prior_education.count() == 1
