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

[((274, 323), 'flask.Blueprint', 'Blueprint', (['"""auth"""', '__name__'], {'url_prefix': '"""/api/v1"""'}), "('auth', __name__, url_prefix='/api/v1')\n", (283, 323), False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((2776, 2847), 'werkzeug.security.generate_password_hash', 'generate_password_hash', (['password'], {'method': '"""pbkdf2:sha256"""', 'salt_length': '(8)'}), "(password, method='pbkdf2:sha256', salt_length=8)\n", (2798, 2847), False, 'from werkzeug.security import generate_password_hash, check_password_hash\n'), ((510, 528), 'flask.request.get_json', 'request.get_json', ([], {}), '()\n', (526, 528), False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((2983, 3187), 'flask.jsonify', 'jsonify', (["{'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}]})\n", (2990, 3187), False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((3435, 3453), 'flask.request.get_json', 'request.get_json', ([], {}), '()\n', (3451, 3453), False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((2040, 2110), 'flask.jsonify', 'jsonify', (["{'status': 400, 'message': 'Please input valid phone number'}"], {}), "({'status': 400, 'message': 'Please input valid phone number'})\n", (2047, 2110), False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((2212, 2269), 'flask.jsonify', 'jsonify', (["{'status': 400, 'message': 'Password not valid'}"], {}), "({'status': 400, 'message': 'Password not valid'})\n", (2219, 2269), False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((2369, 2421), 'flask.jsonify', 'jsonify', (["{'status': 400, 'message': 'Invalid email'}"], {}), "({'status': 400, 'message': 'Invalid email'})\n", (2376, 2421), False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((2521, 2575), 'flask.jsonify', 'jsonify', (["{'status': 400, 'message': 'Username exists'}"], {}), "({'status': 400, 'message': 'Username exists'})\n", (2528, 2575), False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((2669, 2720), 'flask.jsonify', 'jsonify', (["{'status': 400, 'message': 'Email exists'}"], {}), "({'status': 400, 'message': 'Email exists'})\n", (2676, 2720), False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((4024, 4082), 'flask.jsonify', 'jsonify', (["{'status': 404, 'message': 'User does not exist'}"], {}), "({'status': 404, 'message': 'User does not exist'})\n", (4031, 4082), False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((4200, 4285), 'flask.jsonify', 'jsonify', (["{'status': 200, 'message': 'Logged in successfuly', 'token': auth_token}"], {}), "({'status': 200, 'message': 'Logged in successfuly', 'token':\n    auth_token})\n", (4207, 4285), False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((556, 608), 'flask.jsonify', 'jsonify', (["{'status': 400, 'message': 'Invalid input'}"], {}), "({'status': 400, 'message': 'Invalid input'})\n", (563, 608), False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((1020, 1080), 'flask.jsonify', 'jsonify', (["{'status': 400, 'message': 'Firstname is required'}"], {}), "({'status': 400, 'message': 'Firstname is required'})\n", (1027, 1080), False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((1195, 1254), 'flask.jsonify', 'jsonify', (["{'status': 400, 'message': 'Lastname is required'}"], {}), "({'status': 400, 'message': 'Lastname is required'})\n", (1202, 1254), False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((1363, 1419), 'flask.jsonify', 'jsonify', (["{'status': 400, 'message': 'Email is required'}"], {}), "({'status': 400, 'message': 'Email is required'})\n", (1370, 1419), False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((1514, 1577), 'flask.jsonify', 'jsonify', (["{'status': 400, 'message': 'Phone number is required'}"], {}), "({'status': 400, 'message': 'Phone number is required'})\n", (1521, 1577), False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((1692, 1751), 'flask.jsonify', 'jsonify', (["{'status': 400, 'message': 'Username is required'}"], {}), "({'status': 400, 'message': 'Username is required'})\n", (1699, 1751), False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((1866, 1925), 'flask.jsonify', 'jsonify', (["{'status': 400, 'message': 'Password is required'}"], {}), "({'status': 400, 'message': 'Password is required'})\n", (1873, 1925), False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((3710, 3769), 'flask.jsonify', 'jsonify', (["{'status': 400, 'message': 'Username is required'}"], {}), "({'status': 400, 'message': 'Username is required'})\n", (3717, 3769), False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((3860, 3919), 'flask.jsonify', 'jsonify', (["{'status': 400, 'message': 'Password is required'}"], {}), "({'status': 400, 'message': 'Password is required'})\n", (3867, 3919), False, 'from flask import jsonify, Blueprint, request, json, make_response\n'), ((3496, 3546), 'flask.jsonify', 'jsonify', (["{'status': 400, 'message': 'Wrong input'}"], {}), "({'status': 400, 'message': 'Wrong input'})\n", (3503, 3546), False, 'from flask import jsonify, Blueprint, request, json, make_response\n')]