from flask_wtf import FlaskForm
from wtforms import StringField,PasswordField,SubmitField,BooleanField,SelectField,validators
from wtforms.validators import DataRequired,Email,Length,EqualTo,ValidationError
from Opcenter import bcrypt,auth
import email_validator




#this is for the registration 
class RegistrationForm(FlaskForm):
    username = StringField('Username',validators=[DataRequired(),Length(min=2,max=20)]) 
    email = StringField('Email',validators=[DataRequired(),Email()])
    password = PasswordField('Password',validators=[DataRequired()])
    confirm_password = PasswordField('Confirm Password',validators=[DataRequired(), EqualTo('password')])
    accessType = SelectField('Access Type',choices=[('admin','Admin'),('user','User')])
    submit = SubmitField('Add User')
    
    def validate_username(self,username):
        user = auth.find_one({"username": username.data})
        if user:
            raise ValidationError('That username is taken. Please choose a different one.')


    def validate_email(self,email):
        user = auth.find_one({"email": email.data})
        if user:
            raise ValidationError('That email is taken. Please choose a different one.')

class LoginForm(FlaskForm):
    email =  StringField('Email', validators=[validators.DataRequired(), validators.Regexp('^.+$', message='Invalid email address')]) 
    password = PasswordField('Password', validators=[validators.DataRequired(), validators.Length(min=8, max=50)])
    remember = BooleanField('Remember Me')
    submit = SubmitField('Login')



class RequestResetForm(FlaskForm):
    email = StringField('Email',validators=[DataRequired(),Email()])
    submit = SubmitField('Request Password Reset')

    def validate_email(self,email):
        user = auth.find_one({"email": email.data})
        if user is None:
            raise ValidationError('There is no account with that email. You must register first.')


class ResetPasswordForm(FlaskForm):
    password = PasswordField('Password',validators=[DataRequired()])
    confirm_password = PasswordField('Confirm Password',validators=[DataRequired(), EqualTo('password')])
    submit = SubmitField('Reset Password')