from flask_restx import Resource, Namespace, reqparse
from app.functions.roll_dices import roll_dices


def str_to_bool(val):
    return str(val).lower() == 'true'


namespace = Namespace('dice', description='Dice Operations')

# Define the parser and request args:
parser = reqparse.RequestParser()
parser.add_argument('quantity', type=int, required=True, help='Quantity of dice to roll')
parser.add_argument('geometry', type=int, required=True, help='Number of faces on the dice')
parser.add_argument('discard_lowest', type=str_to_bool, required=True, help='Whether to discard lowest roll')


@namespace.route('/')  # resolves to: /dice
class RollDice(Resource):
    @namespace.expect(parser)
    def get(self):
        args = parser.parse_args()
        quantity = args['quantity']
        geometry = args['geometry']
        discard_lowest = args['discard_lowest']
        if quantity < 1 or quantity > 100:
            return {'message': 'Quantity must be between 1 and 100'}, 400
        if geometry < 3 or geometry > 100:
            return {'message': 'Geometry must be between 3 and 100'}, 400
        return roll_dices(quantity, geometry, discard_lowest), 200
