# Path: pupa/models.py
# class SessionDataQualityReport(models.Model):
#     legislative_session = models.ForeignKey(LegislativeSession, on_delete=models.CASCADE)
# 
#     bills_missing_actions = models.PositiveIntegerField()
#     bills_missing_sponsors = models.PositiveIntegerField()
#     bills_missing_versions = models.PositiveIntegerField()
# 
#     votes_missing_voters = models.PositiveIntegerField()
#     votes_missing_bill = models.PositiveIntegerField()
#     votes_missing_yes_count = models.PositiveIntegerField()
#     votes_missing_no_count = models.PositiveIntegerField()
#     votes_with_bad_counts = models.PositiveIntegerField()
# 
#     # these fields store lists of names mapped to numbers of occurances
#     unmatched_sponsor_people = JSONField()
#     unmatched_sponsor_organizations = JSONField()
#     unmatched_voters = JSONField()


from django.db.models import Count, Subquery, OuterRef, Q, F
from opencivicdata.legislative.models import (Bill, VoteEvent, VoteCount, PersonVote,
                                              BillSponsorship)
from ..models import SessionDataQualityReport

    for vote in queryset:
        if vote.yes_count is None:
            report['votes_missing_yes_count'] += 1
            vote.yes_count = 0
        if vote.no_count is None:
            report['votes_missing_no_count'] += 1
            vote.no_count = 0
        if vote.other_count is None:
            vote.other_count = 0
        if (vote.yes_sum != vote.yes_count or
                vote.no_sum != vote.no_count or
                vote.other_sum != vote.other_count):
            report['votes_with_bad_counts'] += 1

    # handle unmatched
    queryset = BillSponsorship.objects.filter(bill__legislative_session_id=session,
                                              entity_type='person', person_id=None
                                              ).values('name').annotate(num=Count('name'))
    report['unmatched_sponsor_people'] = {item['name']: item['num'] for item in queryset}
    queryset = BillSponsorship.objects.filter(bill__legislative_session_id=session,
                                              entity_type='organization', person_id=None
                                              ).values('name').annotate(num=Count('name'))
    report['unmatched_sponsor_organizations'] = {item['name']: item['num'] for item in queryset}
    queryset = PersonVote.objects.filter(vote_event__legislative_session_id=session,
                                         voter__isnull=True).values(name=F('voter_name')).annotate(
                                             num=Count('voter_name')
                                         )
    report['unmatched_voters'] = {item['name']: item['num'] for item in queryset}


# Path: pupa/models.py
# class SessionDataQualityReport(models.Model):
#     legislative_session = models.ForeignKey(LegislativeSession, on_delete=models.CASCADE)
# 
#     bills_missing_actions = models.PositiveIntegerField()
#     bills_missing_sponsors = models.PositiveIntegerField()
#     bills_missing_versions = models.PositiveIntegerField()
# 
#     votes_missing_voters = models.PositiveIntegerField()
#     votes_missing_bill = models.PositiveIntegerField()
#     votes_missing_yes_count = models.PositiveIntegerField()
#     votes_missing_no_count = models.PositiveIntegerField()
#     votes_with_bad_counts = models.PositiveIntegerField()
# 
#     # these fields store lists of names mapped to numbers of occurances
#     unmatched_sponsor_people = JSONField()
#     unmatched_sponsor_organizations = JSONField()
#     unmatched_voters = JSONField()


# Path: pupa/reports/session.py
from django.db.models import Count, Subquery, OuterRef, Q, F
from opencivicdata.legislative.models import (Bill, VoteEvent, VoteCount, PersonVote,
                                              BillSponsorship)
from ..models import SessionDataQualityReport


    for vote in queryset:
        if vote.yes_count is None:
            report['votes_missing_yes_count'] += 1
            vote.yes_count = 0
        if vote.no_count is None:
            report['votes_missing_no_count'] += 1
            vote.no_count = 0
        if vote.other_count is None:
            vote.other_count = 0
        if (vote.yes_sum != vote.yes_count or
                vote.no_sum != vote.no_count or
                vote.other_sum != vote.other_count):
            report['votes_with_bad_counts'] += 1

    # handle unmatched
    queryset = BillSponsorship.objects.filter(bill__legislative_session_id=session,
                                              entity_type='person', person_id=None
                                              ).values('name').annotate(num=Count('name'))
    report['unmatched_sponsor_people'] = {item['name']: item['num'] for item in queryset}
    queryset = BillSponsorship.objects.filter(bill__legislative_session_id=session,
                                              entity_type='organization', person_id=None
                                              ).values('name').annotate(num=Count('name'))
    report['unmatched_sponsor_organizations'] = {item['name']: item['num'] for item in queryset}
    queryset = PersonVote.objects.filter(vote_event__legislative_session_id=session,
                                         voter__isnull=True).values(name=F('voter_name')).annotate(
                                             num=Count('voter_name')
                                         )
    report['unmatched_voters'] = {item['name']: item['num'] for item in queryset}


    return SessionDataQualityReport(legislative_session_id=session, **report)