# Copyright (c) 2019, IRIS-HEP
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import requests
import sys
import traceback

from flask import current_app
from flask_restful import Resource, reqparse

from servicex.models import UserModel
from .slack_msg_builder import signup

parser = reqparse.RequestParser()
parser.add_argument('full_name', help='This field cannot be blank', required=True)
parser.add_argument('email', help='This field cannot be blank', required=True)
parser.add_argument('institution')
parser.add_argument('experiment')
parser.add_argument('password', help='This field cannot be blank', required=True)
parser.add_argument('confirm_password', help='This field cannot be blank', required=True)


class UserRegistration(Resource):

    def post(self):
        data = parser.parse_args()

        if UserModel.find_by_email(data['email']):
            return {'message': 'User {} already exists'.format(data['email'])}

        if data.password != data.confirm_password:
            return {'message': 'Passwords do not match'}
        data.key = UserModel.generate_hash(data.password)

        new_user = UserModel(
            email=data.email,
            full_name=data.full_name,
            institution=data.institution,
            key=data.key,
            experiment=data.experiment,
        )

        try:
            new_user.save_to_db()
            webhook_url = current_app.config.get("SIGNUP_WEBHOOK_URL")
            if webhook_url:
                res = requests.post(webhook_url, signup(new_user.email))
                # Raise exception on error (e.g. bad request or forbidden url)
                res.raise_for_status()
            return {
                'message': 'User {} added to pending user list'.format(data['email']),
            }
        except Exception:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            traceback.print_tb(exc_traceback, limit=20, file=sys.stdout)
            print(exc_value)
            return {'message': str(exc_value)}, 500
