from flask import jsonify, Blueprint, request, json, make_response
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import datetime
from ..utils.validators import Validation
from ..models.auth_models import Users


v1_auth_blueprint = Blueprint('auth', __name__, url_prefix='/api/v1')

USER = Users()
VALIDATOR = Validation()


@v1_auth_blueprint.route('/signup', methods=['POST'])
def signup():
    """View that controls creation of new users"""
    try:
        data = request.get_json()
    except:
        return jsonify({
            "status": 400,
            "message": "Invalid input"
        }), 400

    firstname = data.get('firstname')
    lastname = data.get('lastname')
    othername = data.get('othername')
    email = data.get('email')
    phone_number = data.get('phone_number')
    username = data.get('username')
    is_admin = data.get('is_admin')
    password = data.get('password')

    if not firstname or not firstname.split():
        return make_response(jsonify({
            "status": 400,
            "message": "Firstname is required"
        })), 400
    if not lastname or not lastname.split():
        return make_response(jsonify({
            "status": 400,
            "message": "Lastname is required"
        })), 400
    if not email or not email.split():
        return make_response(jsonify({
            "status": 400,
            "message": "Email is required"
        })), 400
    if not phone_number:
        return make_response(jsonify({
            "status": 400,
            "message": "Phone number is required"
        })), 400
    if not username or not username.split():
        return make_response(jsonify({
            "status": 400,
            "message": "Username is required"
        })), 400
    if not password or not password.split():
        return make_response(jsonify({
            "status": 400,
            "message": "Password is required"
        })), 400

    if not VALIDATOR.validate_phone_number(phone_number):
        return jsonify({
            "status": 400,
            "message": "Please input valid phone number"
        }), 400

    if VALIDATOR.validate_password(password):
        return jsonify({
            "status": 400,
            "message": "Password not valid"
        }), 400

    if not VALIDATOR.validate_email(email):
        return jsonify({
            "status": 400,
            "message": "Invalid email"
        }), 400

    if VALIDATOR.username_exists(username):
        return jsonify({
            "status": 400,
            "message": "Username exists"
        }), 400

    if VALIDATOR.email_exists(email):
        return jsonify({
            "status": 400,
            "message": "Email exists"
        }), 400

    password = generate_password_hash(
        password, method='pbkdf2:sha256', salt_length=8)

    res = USER.signup(
        firstname, lastname, othername, email, phone_number, username, is_admin, password)
    return jsonify({
        "status": 201,
        "data": [{
            "firstname": firstname,
            "lastname": lastname,
            "othername": othername,
            "email": email,
            "phone_number": phone_number,
            "username": username,
            "is_admin": is_admin
        }]
    }), 201


@v1_auth_blueprint.route('/login', methods=['POST'])
def login():
    """ A view to control users login """
    try:
        data = request.get_json()

    except:
        return make_response(jsonify({
            "status": 400,
            "message": "Wrong input"
        })), 400
    username = data.get('username')
    password = data.get('password')

    if not username:
        return make_response(jsonify({
            "status": 400,
            "message": "Username is required"
        })), 400
    if not password:
        return make_response(jsonify({
            "status": 400,
            "message": "Password is required"
        })), 400

    if not VALIDATOR.username_exists(username):
        return jsonify({
            "status": 404,
            "message": "User does not exist"
        }), 404

    auth_token = user.generate_auth_token(username)
    return make_response(jsonify({
        "status": 200,
        "message": 'Logged in successfuly',
        "token": auth_token
    })), 200

[((8, 20, 8, 69), 'flask.Blueprint', 'Blueprint', (), '', False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((95, 15, 96, 56), 'werkzeug.security.generate_password_hash', 'generate_password_hash', (), '', False, 'from werkzeug.security import generate_password_hash, check_password_hash\n'), ((18, 15, 18, 33), 'flask.request.get_json', 'request.get_json', ({}, {}), '()', False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((100, 11, 111, 6), 'flask.jsonify', 'jsonify', ({(100, 19, 111, 5): "{'status': 201, 'data': [{'firstname': firstname, 'lastname': lastname,\n    'othername': othername, 'email': email, 'phone_number': phone_number,\n    'username': username, 'is_admin': is_admin}]}"}, {}), "({'status': 201, 'data': [{'firstname': firstname, 'lastname':\n    lastname, 'othername': othername, 'email': email, 'phone_number':\n    phone_number, 'username': username, 'is_admin': is_admin}]})", False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((118, 15, 118, 33), 'flask.request.get_json', 'request.get_json', ({}, {}), '()', False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((66, 15, 69, 10), 'flask.jsonify', 'jsonify', ({(66, 23, 69, 9): "{'status': 400, 'message': 'Please input valid phone number'}"}, {}), "({'status': 400, 'message': 'Please input valid phone number'})", False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((72, 15, 75, 10), 'flask.jsonify', 'jsonify', ({(72, 23, 75, 9): "{'status': 400, 'message': 'Password not valid'}"}, {}), "({'status': 400, 'message': 'Password not valid'})", False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((78, 15, 81, 10), 'flask.jsonify', 'jsonify', ({(78, 23, 81, 9): "{'status': 400, 'message': 'Invalid email'}"}, {}), "({'status': 400, 'message': 'Invalid email'})", False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((84, 15, 87, 10), 'flask.jsonify', 'jsonify', ({(84, 23, 87, 9): "{'status': 400, 'message': 'Username exists'}"}, {}), "({'status': 400, 'message': 'Username exists'})", False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((90, 15, 93, 10), 'flask.jsonify', 'jsonify', ({(90, 23, 93, 9): "{'status': 400, 'message': 'Email exists'}"}, {}), "({'status': 400, 'message': 'Email exists'})", False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((140, 15, 143, 10), 'flask.jsonify', 'jsonify', ({(140, 23, 143, 9): "{'status': 404, 'message': 'User does not exist'}"}, {}), "({'status': 404, 'message': 'User does not exist'})", False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((146, 25, 150, 6), 'flask.jsonify', 'jsonify', ({(146, 33, 150, 5): "{'status': 200, 'message': 'Logged in successfuly', 'token': auth_token}"}, {}), "({'status': 200, 'message': 'Logged in successfuly', 'token':\n    auth_token})", False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((20, 15, 23, 10), 'flask.jsonify', 'jsonify', ({(20, 23, 23, 9): "{'status': 400, 'message': 'Invalid input'}"}, {}), "({'status': 400, 'message': 'Invalid input'})", False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((35, 29, 38, 10), 'flask.jsonify', 'jsonify', ({(35, 37, 38, 9): "{'status': 400, 'message': 'Firstname is required'}"}, {}), "({'status': 400, 'message': 'Firstname is required'})", False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((40, 29, 43, 10), 'flask.jsonify', 'jsonify', ({(40, 37, 43, 9): "{'status': 400, 'message': 'Lastname is required'}"}, {}), "({'status': 400, 'message': 'Lastname is required'})", False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((45, 29, 48, 10), 'flask.jsonify', 'jsonify', ({(45, 37, 48, 9): "{'status': 400, 'message': 'Email is required'}"}, {}), "({'status': 400, 'message': 'Email is required'})", False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((50, 29, 53, 10), 'flask.jsonify', 'jsonify', ({(50, 37, 53, 9): "{'status': 400, 'message': 'Phone number is required'}"}, {}), "({'status': 400, 'message': 'Phone number is required'})", False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((55, 29, 58, 10), 'flask.jsonify', 'jsonify', ({(55, 37, 58, 9): "{'status': 400, 'message': 'Username is required'}"}, {}), "({'status': 400, 'message': 'Username is required'})", False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((60, 29, 63, 10), 'flask.jsonify', 'jsonify', ({(60, 37, 63, 9): "{'status': 400, 'message': 'Password is required'}"}, {}), "({'status': 400, 'message': 'Password is required'})", False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((129, 29, 132, 10), 'flask.jsonify', 'jsonify', ({(129, 37, 132, 9): "{'status': 400, 'message': 'Username is required'}"}, {}), "({'status': 400, 'message': 'Username is required'})", False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((134, 29, 137, 10), 'flask.jsonify', 'jsonify', ({(134, 37, 137, 9): "{'status': 400, 'message': 'Password is required'}"}, {}), "({'status': 400, 'message': 'Password is required'})", False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((121, 29, 124, 10), 'flask.jsonify', 'jsonify', ({(121, 37, 124, 9): "{'status': 400, 'message': 'Wrong input'}"}, {}), "({'status': 400, 'message': 'Wrong input'})", False, 'from flask import jsonify, Blueprint, request, json, make_response\n')]