# Path: models.py
# 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
#     )


import http.client as client
import uuid
from flask import request
from flask_restful import Resource
from models import Item
from utils import generate_response
"""
Items view: this module provides methods to interact
with items resources
"""





SEARCH_FIELDS = ['name', 'description']


class ItemsHandler(Resource):
    """Handler of the collection of items"""

    def get(self):
        """Retrieve every item"""
        data = Item.json_list(Item.select())
        return generate_response(data, client.OK)

    def post(self):
        """
        Insert a new item, the item_uuid identifier is forwarded
        from the one generated from the database
        """
        request_data = request.get_json(force=True)


# Path: models.py
# 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/items.py
import http.client as client
import uuid
from flask import request
from flask_restful import Resource
from models import Item
from utils import generate_response

"""
Items view: this module provides methods to interact
with items resources
"""





SEARCH_FIELDS = ['name', 'description']


class ItemsHandler(Resource):
    """Handler of the collection of items"""

    def get(self):
        """Retrieve every item"""
        data = Item.json_list(Item.select())
        return generate_response(data, client.OK)

    def post(self):
        """
        Insert a new item, the item_uuid identifier is forwarded
        from the one generated from the database
        """
        request_data = request.get_json(force=True)


        errors = Item.validate_input(request_data)