# 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
def search_by_params(request):
    """    
    Search by query params
    supports the following ?term=val
    - id - the db id
    - name - matches against the official_full
    - last_name - last_name
    - gender
    - religion
        
    additionally supports boolean current to match only current reps

    example:
        http://127.0.0.1:8000/legislators/search/?gender=F&religion=Jewish
        http://127.0.0.1:8000/legislators/search/?gender=F&religion=Jewish&current
        
    
    :param request: 
    :return: list of CongreesPerson objects
    """

    params = request.query_params

    # if we have an id, just return it
    if 'id' in params:
        logger.info("search by id")
        return find_by_id(request, params.get('id'))

    # See if we're going with all or just current
    if 'current' in params:

# 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

def search_by_params(request):
    """    
    Search by query params
    supports the following ?term=val
    - id - the db id
    - name - matches against the official_full
    - last_name - last_name
    - gender
    - religion
        
    additionally supports boolean current to match only current reps

    example:
        http://127.0.0.1:8000/legislators/search/?gender=F&religion=Jewish
        http://127.0.0.1:8000/legislators/search/?gender=F&religion=Jewish&current
        
    
    :param request: 
    :return: list of CongreesPerson objects
    """

    params = request.query_params

    # if we have an id, just return it
    if 'id' in params:
        logger.info("search by id")
        return find_by_id(request, params.get('id'))

    # See if we're going with all or just current
    if 'current' in params:

        people = get_current_legislators()