from flask import current_app
from flask_wtf import FlaskForm
from wtforms import (StringField, PasswordField, BooleanField, SubmitField, EmailField, IntegerField, DateField,
                     SelectField, SelectMultipleField, FieldList, FormField, FloatField, TextAreaField)
from wtforms.validators import DataRequired, Length, Email, NumberRange, Optional, ValidationError, InputRequired
import pytz

from common.models.entitlements import PeriodStatus


class LicenseTierForm(FlaskForm):
    name = StringField('Name', validators=[DataRequired(), Length(max=50)])
    version = StringField('Version', validators=[DataRequired(), Length(max=50)])
    start_date = DateField('Start Date', id='form-control datepicker', validators=[DataRequired()])
    end_date = DateField('End Date', id='form-control datepicker', validators=[Optional()])
    basic_fee_d = FloatField('Basic Fee ($)', validators=[InputRequired(), NumberRange(min=0)])
    basic_fee_e = FloatField('Basic Fee (€)', validators=[InputRequired(), NumberRange(min=0)])
    max_storage_mb = IntegerField('Max Storage (MiB)', validators=[DataRequired(), NumberRange(min=1)])
    additional_storage_price_d = FloatField('Additional Storage Fee ($)',
                                            validators=[InputRequired(), NumberRange(min=0)])
    additional_storage_price_e = FloatField('Additional Storage Fee (€)',
                                            validators=[InputRequired(), NumberRange(min=0)])
    additional_storage_bucket = IntegerField('Additional Storage Bucket Size (MiB)',
                                             validators=[DataRequired(), NumberRange(min=1)])
    included_embedding_mb = IntegerField('Included Embeddings (MiB)',
                                             validators=[DataRequired(), NumberRange(min=1)])
    additional_embedding_price_d = FloatField('Additional Embedding Fee ($)',
                                              validators=[InputRequired(), NumberRange(min=0)])
    additional_embedding_price_e = FloatField('Additional Embedding Fee (€)',
                                              validators=[InputRequired(), NumberRange(min=0)])
    additional_embedding_bucket = IntegerField('Additional Embedding Bucket Size (MiB)',
                                               validators=[DataRequired(), NumberRange(min=1)])
    included_interaction_tokens = IntegerField('Included Embedding Tokens (M Tokens)',
                                               validators=[DataRequired(), NumberRange(min=1)])
    additional_interaction_token_price_d = FloatField('Additional Interaction Token Fee ($)',
                                                      validators=[InputRequired(), NumberRange(min=0)])
    additional_interaction_token_price_e = FloatField('Additional Interaction Token Fee (€)',
                                                      validators=[InputRequired(), NumberRange(min=0)])
    additional_interaction_bucket = IntegerField('Additional Interaction Bucket Size (M Tokens)',
                                                 validators=[DataRequired(), NumberRange(min=1)])
    standard_overage_embedding = FloatField('Standard Overage Embedding (%)',
                                            validators=[DataRequired(), NumberRange(min=0)], default=0)
    standard_overage_interaction = FloatField('Standard Overage Interaction (%)',
                                              validators=[DataRequired(), NumberRange(min=0)], default=0)


class LicenseForm(FlaskForm):
    start_date = DateField('Start Date', id='form-control datepicker', validators=[DataRequired()])
    nr_of_periods = IntegerField('Number of Periods',
                                 validators=[DataRequired(), NumberRange(min=1, max=12)], default=12)
    currency = StringField('Currency', validators=[Optional(), Length(max=20)])
    yearly_payment = BooleanField('Yearly Payment', default=False)
    basic_fee = FloatField('Basic Fee', validators=[InputRequired(), NumberRange(min=0)])
    max_storage_mb = IntegerField('Max Storage (MiB)', validators=[DataRequired(), NumberRange(min=1)])
    additional_storage_allowed = BooleanField('Additional Storage Allowed', default=True)
    additional_storage_price = FloatField('Additional Storage Token Fee',
                                          validators=[InputRequired(), NumberRange(min=0)])
    additional_storage_bucket = IntegerField('Additional Storage Bucket Size (MiB)',
                                             validators=[DataRequired(), NumberRange(min=1)])
    included_embedding_mb = IntegerField('Included Embedding Tokens (MiB)',
                                         validators=[DataRequired(), NumberRange(min=1)])
    additional_embedding_allowed = BooleanField('Additional Embedding Allowed', default=True)
    additional_embedding_price = FloatField('Additional Embedding Token Fee',
                                            validators=[InputRequired(), NumberRange(min=0)])
    additional_embedding_bucket = IntegerField('Additional Embedding Bucket Size (MiB)',
                                               validators=[DataRequired(), NumberRange(min=1)])
    included_interaction_tokens = IntegerField('Included Interaction Tokens (M Tokens)',
                                               validators=[DataRequired(), NumberRange(min=1)])
    additional_interaction_allowed = BooleanField('Additional Interaction Allowed', default=True)
    additional_interaction_token_price = FloatField('Additional Interaction Token Fee',
                                                    validators=[InputRequired(), NumberRange(min=0)])
    additional_interaction_bucket = IntegerField('Additional Interaction Bucket Size (M Tokens)',
                                                 validators=[DataRequired(), NumberRange(min=1)])
    overage_embedding = FloatField('Overage Embedding (%)',
                                   validators=[DataRequired(), NumberRange(min=0)],
                                   default=0)
    overage_interaction = FloatField('Overage Interaction (%)',
                                     validators=[DataRequired(), NumberRange(min=0)],
                                     default=0)


class EditPeriodForm(FlaskForm):
    period_start = DateField('Period Start', id='form-control datepicker', validators=[DataRequired()])
    period_end = DateField('Period End', id='form-control datepicker', validators=[DataRequired()])
    status = SelectField('Status', choices=[], validators=[DataRequired()])

    def __init__(self, *args, **kwargs):
        super(EditPeriodForm, self).__init__(*args, **kwargs)
        self.status.choices = [(item.name, item.value) for item in PeriodStatus]


