from flask_restful import Resource,reqparse
from flask_jwt import jwt_required
from flask import request
import sqlite3

class Item(Resource):
    # @jwt_required()
    def get(self,name):
        connection = sqlite3.connect('data.db')
        cursor = connection.cursor()
        query = "SELECT * FROM items WHERE name=?"
        result = cursor.execute(query,(name,))
        row = result.fetchone()
        connection.close()
        if row:
            return {"item":{"name":row[0],"price": row[1]}}
        return {"message":"Item not found"}

    def post(self,name):
        if next(filter(lambda x: x['name'] == name, items),None) is not None:
            return {'message':f"An item with name {name} already exists."}, 400
        data = request.get_json(silent = True)
        item = {'name':name,'price':data['price']}
        items.append(item)
        return item, 201

    def delete(self,name):
        global items
        items = list(filter(lambda x:x["name"] != name, items))
        return {"message":"item deleted"}
    
    def put(self,name):
        parser = reqparse.RequestParser()
        parser.add_argument(
            'price',
            type=float,
            required = True,
            help = 'This field is required',
        )
        data = parser.parse_args()

        item = next(filter(lambda x: x["name"] == name,items),None)
        if item is None:
            item = {"name":name,"price":data['price']}
            items.append(item)
        else:
            item.update(data)
        return item

class ItemList(Resource):
    def get(self):
        return {'items':items} , 200