# Path: capitolweb/legislators/models.py
# class CongressPerson(models.Model):
#     def __str__(self):
#         return self.official_full
#     bioguide_id = models.CharField(max_length=7, primary_key=True)
#     first = models.CharField(max_length=25)
#     middle = models.CharField(max_length=25)
#     last = models.CharField(max_length=25)
#     suffix = models.CharField(max_length=5)
#     nickname = models.CharField(max_length=25)
#     official_full = models.CharField(max_length=50)
#     birthday = models.DateField(default=date(1776, 7, 4))
#     gender = models.CharField(max_length=1, choices=(('M', "Male"), ('F', 'Female')))
#     religion = models.CharField(max_length=30)
# 
#     @property
#     def image_lg(self):
#         return "https://theunitedstates.io/images/congress/450x550/{}.jpg".format(self.bioguide_id)
# 
#     @property
#     def image_sm(self):
#         return "https://theunitedstates.io/images/congress/225x275/{}.jpg".format(self.bioguide_id)
# 
#     class Meta:
#         ordering = ('official_full',)
# 
# class ExternalId(models.Model):
#     person = models.ForeignKey(CongressPerson, on_delete=models.CASCADE, related_name='external_ids')
#     type = models.CharField(max_length=50)
#     value = models.CharField(max_length=50)
# 
# def get_current_legislators():
#     """
#     Filter legislators based on terms
#     :param state: 2 letter code for state
#     :param party: full name of a party
#     :param term_type: 'sen' or 'rep' for senate or house
#     :return:
#     """
#     results = CongressPerson.objects.filter(terms__start_date__lte=date.today(), terms__end_date__gte=date.today())
#     return results


from .models import CongressPerson, ExternalId, get_current_legislators
from rest_framework.decorators import api_view
from django.http import JsonResponse
from legislators.serializers import CongressPersonSerializer, CongressPersonShortSerializer
import logging
    """
    logger.info("Request: {}".format(person_id))
    person = CongressPerson.objects.prefetch_related('terms').get(pk=person_id)
    serializer = CongressPersonSerializer(person)
    return JsonResponse(serializer.data, safe=False)


@api_view(['GET'])
def find_by_bioguide_id(request, bioguide_id):
    """
    Return a congress person by their bioguide id
    :param request: 
    :param bioguide_id: the bioguide id
    :return:  a single CongressPerson
    """
    logger.info("Request: {}".format(bioguide_id))
    ref = ExternalId.objects.filter(type='bioguide', value=bioguide_id)[0]
    serializer = CongressPersonSerializer(ref.person)
    return JsonResponse(serializer.data, safe=False)


@api_view(['GET'])
def list_current(request):
    """
    Get all of the current CongressPeople
    :param request: optional - state=<2 letter state code>
    :return: A list of CongressPeople
    """
    params = request.query_params
    state = params.get('state', None)

# Path: capitolweb/legislators/models.py
# class CongressPerson(models.Model):
#     def __str__(self):
#         return self.official_full
#     bioguide_id = models.CharField(max_length=7, primary_key=True)
#     first = models.CharField(max_length=25)
#     middle = models.CharField(max_length=25)
#     last = models.CharField(max_length=25)
#     suffix = models.CharField(max_length=5)
#     nickname = models.CharField(max_length=25)
#     official_full = models.CharField(max_length=50)
#     birthday = models.DateField(default=date(1776, 7, 4))
#     gender = models.CharField(max_length=1, choices=(('M', "Male"), ('F', 'Female')))
#     religion = models.CharField(max_length=30)
# 
#     @property
#     def image_lg(self):
#         return "https://theunitedstates.io/images/congress/450x550/{}.jpg".format(self.bioguide_id)
# 
#     @property
#     def image_sm(self):
#         return "https://theunitedstates.io/images/congress/225x275/{}.jpg".format(self.bioguide_id)
# 
#     class Meta:
#         ordering = ('official_full',)
# 
# class ExternalId(models.Model):
#     person = models.ForeignKey(CongressPerson, on_delete=models.CASCADE, related_name='external_ids')
#     type = models.CharField(max_length=50)
#     value = models.CharField(max_length=50)
# 
# def get_current_legislators():
#     """
#     Filter legislators based on terms
#     :param state: 2 letter code for state
#     :param party: full name of a party
#     :param term_type: 'sen' or 'rep' for senate or house
#     :return:
#     """
#     results = CongressPerson.objects.filter(terms__start_date__lte=date.today(), terms__end_date__gte=date.today())
#     return results


# Path: capitolweb/legislators/views.py
from .models import CongressPerson, ExternalId, get_current_legislators
from rest_framework.decorators import api_view
from django.http import JsonResponse
from legislators.serializers import CongressPersonSerializer, CongressPersonShortSerializer
import logging

    """
    logger.info("Request: {}".format(person_id))
    person = CongressPerson.objects.prefetch_related('terms').get(pk=person_id)
    serializer = CongressPersonSerializer(person)
    return JsonResponse(serializer.data, safe=False)


@api_view(['GET'])
def find_by_bioguide_id(request, bioguide_id):
    """
    Return a congress person by their bioguide id
    :param request: 
    :param bioguide_id: the bioguide id
    :return:  a single CongressPerson
    """
    logger.info("Request: {}".format(bioguide_id))
    ref = ExternalId.objects.filter(type='bioguide', value=bioguide_id)[0]
    serializer = CongressPersonSerializer(ref.person)
    return JsonResponse(serializer.data, safe=False)


@api_view(['GET'])
def list_current(request):
    """
    Get all of the current CongressPeople
    :param request: optional - state=<2 letter state code>
    :return: A list of CongressPeople
    """
    params = request.query_params
    state = params.get('state', None)

    people = get_current_legislators()