from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, IntegerField, SelectField
from wtforms.validators import DataRequired, Length, ValidationError, Optional
from minibase.database.models import Company
import minibase.database.utils as dbUtils


# Defines the form class to be used for the user registretion
class companyForm(FlaskForm):
    # Decalarion of the fields for the form and it's propereties
    company_name = StringField('Name', validators=[DataRequired(), Length(min=3, max=20)])
    legal_entity = SelectField('Legal Entity', validators=[DataRequired()])
    relation = SelectField('Relation', validators=[DataRequired()])
    industry = SelectField('Area', validators=[DataRequired()])
    status = SelectField('Status', validators=[DataRequired()])
    website = StringField('Website', validators=[Optional(), Length(min=3, max=100)])
    country = SelectField('Country', validators=[DataRequired()])
    state = StringField('State', validators=[DataRequired()])
    city = StringField('City', validators=[DataRequired()])
    post = IntegerField('Zip', validators=[DataRequired()])
    street = StringField('Street', validators=[DataRequired()])
    no = IntegerField('No', validators=[DataRequired()])
    submit = SubmitField('Register Company')

    # Queries to be made in order to validate the form : If Company name exitst within the same country
    # Case Insensistive
    def validate_company_name(self, company_name):
        company_query = Company.query.filter(Company.name.ilike(self.company_name.data))  # Database Querry ilike means case insessitive
        for company in company_query:
            if company.name:
                if company.country_bill == self.country.data:
                    raise ValidationError('This company in this contry alredy exist')


class selectCompanyForm(FlaskForm):  # Defines the form class to be used for the user registretion
    # Decalarion of the fields for the form and it's propereties
    company = SelectField('Company Name', validators=[DataRequired()])
    submit = SubmitField('Show Projects')
