# Path: auth.py
# class Auth:
#     def login_required(*args, **kwargs):
#     def current_user(self):
#     def init_app(app):
# def load_user(user_id):
# def load_user_from_request(request):
# 
# Path: models.py
# class Favorite(BaseModel):
#     """ Many to many table to relate an item with a user."""
#     uuid = UUIDField(unique=True)
#     user = ForeignKeyField(User, related_name="favorites")
#     item = ForeignKeyField(Item, related_name="favorites")
#     _schema = FavoriteSchema
# 
# class Item(BaseModel):
#     """
#     Item describes a product for the e-commerce platform.
# 
#     Attributes:
#         uuid (UUID): Item UUID
#         name (str): Name for the product
#         price (decimal.Decimal): Price for a single product
#         description (str): Product description
#         availability (int): Quantity of items available
#         category (str): Category group of the item
# 
#     """
#     uuid = UUIDField(unique=True)
#     name = CharField()
#     price = DecimalField(auto_round=True)
#     description = TextField()
#     availability = IntegerField()
#     category = TextField()
#     _schema = ItemSchema
#     _search_attributes = ['name', 'category', 'description']
# 
#     def __str__(self):
#         return '{}, {}, {}, {}'.format(
#             self.uuid,
#             self.name,
#             self.price,
#             self.description)
# 
#     def is_favorite(self, item):
#         for f in self.favorites:
#             if f.item_id == item.id:
#                 return True
#         return False
# 
# Path: utils.py
# def generate_response(data, status, mimetype='application/vnd.api+json'):
#     """
#     Given a resource model that extends from `BaseModel` generate a Reponse
#     object to be returned from the application endpoints'
#     """
#     return Response(
#         response=data,
#         status=status,
#         mimetype=mimetype
#     )


from auth import auth
from flask import request
from flask_restful import Resource
from models import Favorite, Item
from http.client import (CREATED, NOT_FOUND, OK, BAD_REQUEST)
from utils import generate_response
    @auth.login_required
    def get(self):
        data = Favorite.json_list(auth.current_user.favorites)

        return generate_response(data, OK)

    @auth.login_required
    def post(self):
        user = auth.current_user

        res = request.get_json(force=True)
        errors = Favorite.validate_input(res)
        if errors:
            return errors, BAD_REQUEST

        data = res['data']['attributes']

        try:
            item = Item.get(Item.uuid == data['item_uuid'])
        except Item.DoesNotExist:
            return {"message": "Item {} doesn't exist as Favorite.".format(
                    data['item_uuid'])}, NOT_FOUND

        has_already = Item.is_favorite(user, item)
        if has_already:
            return {"message": "Item {} was already been inserted as Favorite.".format(
                    data['item_uuid'])}, OK

        favorite = user.add_favorite(item)


# Path: auth.py
# class Auth:
#     def login_required(*args, **kwargs):
#     def current_user(self):
#     def init_app(app):
# def load_user(user_id):
# def load_user_from_request(request):
# 
# Path: models.py
# class Favorite(BaseModel):
#     """ Many to many table to relate an item with a user."""
#     uuid = UUIDField(unique=True)
#     user = ForeignKeyField(User, related_name="favorites")
#     item = ForeignKeyField(Item, related_name="favorites")
#     _schema = FavoriteSchema
# 
# class Item(BaseModel):
#     """
#     Item describes a product for the e-commerce platform.
# 
#     Attributes:
#         uuid (UUID): Item UUID
#         name (str): Name for the product
#         price (decimal.Decimal): Price for a single product
#         description (str): Product description
#         availability (int): Quantity of items available
#         category (str): Category group of the item
# 
#     """
#     uuid = UUIDField(unique=True)
#     name = CharField()
#     price = DecimalField(auto_round=True)
#     description = TextField()
#     availability = IntegerField()
#     category = TextField()
#     _schema = ItemSchema
#     _search_attributes = ['name', 'category', 'description']
# 
#     def __str__(self):
#         return '{}, {}, {}, {}'.format(
#             self.uuid,
#             self.name,
#             self.price,
#             self.description)
# 
#     def is_favorite(self, item):
#         for f in self.favorites:
#             if f.item_id == item.id:
#                 return True
#         return False
# 
# Path: utils.py
# def generate_response(data, status, mimetype='application/vnd.api+json'):
#     """
#     Given a resource model that extends from `BaseModel` generate a Reponse
#     object to be returned from the application endpoints'
#     """
#     return Response(
#         response=data,
#         status=status,
#         mimetype=mimetype
#     )


# Path: views/favorites.py
from auth import auth
from flask import request
from flask_restful import Resource
from models import Favorite, Item
from http.client import (CREATED, NOT_FOUND, OK, BAD_REQUEST)
from utils import generate_response

    @auth.login_required
    def get(self):
        data = Favorite.json_list(auth.current_user.favorites)

        return generate_response(data, OK)

    @auth.login_required
    def post(self):
        user = auth.current_user

        res = request.get_json(force=True)
        errors = Favorite.validate_input(res)
        if errors:
            return errors, BAD_REQUEST

        data = res['data']['attributes']

        try:
            item = Item.get(Item.uuid == data['item_uuid'])
        except Item.DoesNotExist:
            return {"message": "Item {} doesn't exist as Favorite.".format(
                    data['item_uuid'])}, NOT_FOUND

        has_already = Item.is_favorite(user, item)
        if has_already:
            return {"message": "Item {} was already been inserted as Favorite.".format(
                    data['item_uuid'])}, OK

        favorite = user.add_favorite(item)


        return generate_response(favorite.json(), CREATED)