from flask import request
from flask_jwt_extended import create_access_token
from ika_web.app.api.database.models import User
from flask_restful import Resource
import datetime
from mongoengine.errors import FieldDoesNotExist, NotUniqueError, DoesNotExist
from ika_web.app.api.resources.errors import SchemaValidationError, EmailAlreadyExistsError, UnauthorizedError, \
InternalServerError

class SignupApi(Resource):
    def post(self):
        """
        post [summary]

        Raises:
            SchemaValidationError: [description]
            EmailAlreadyExistsError: [description]
            InternalServerError: [description]

        Returns:
            [type]: [description]
        """
        try:
            body = request.get_json()
            user = User(**body)
            user.hash_password()
            user.save()
            id = user.id
            return {'id': str(id)}, 200
        except FieldDoesNotExist:
            raise SchemaValidationError
        except NotUniqueError:
            raise EmailAlreadyExistsError
        except Exception as e:
            raise InternalServerError

class LoginApi(Resource):
    def post(self):
        """
        post [summary]

        Raises:
            UnauthorizedError: [description]
            UnauthorizedError: [description]
            InternalServerError: [description]

        Returns:
            [type]: [description]
        """
        try:
            body = request.get_json()
            user = User.objects.get
            authorized = user.check_password(body.get)
            if not authorized:
                raise UnauthorizedError

            expires = datetime.timedelta(days=7)
            access_token = create_access_token(identity=str(user.id), expires_delta=expires)
            return {'token': access_token}, 200
        except (UnauthorizedError, DoesNotExist):
            raise UnauthorizedError
        except Exception as e:
            raise InternalServerError