# File: plugins/admin/forms.py

from flask_wtf import FlaskForm
from wtforms import (
    StringField,
    PasswordField,
    SelectField,
    BooleanField,
    IntegerField,
    SubmitField
)
from wtforms.validators import DataRequired, Optional, ValidationError, NumberRange
import re

def email_check(form, field):
    pattern = re.compile(r'^[^@]+@[^@]+\.[^@]+$')
    if not field.data or not pattern.match(field.data):
        raise ValidationError('Invalid email address.')

class UserForm(FlaskForm):
    email = StringField(
        'Email',
        validators=[DataRequired(), email_check]
    )
    password = PasswordField(
        'Password',
        validators=[Optional()]
    )
    role = SelectField(
        'Role',
        choices=[('user', 'User'), ('admin', 'Admin')],
        validators=[DataRequired()]
    )
    is_verified = BooleanField('Verified')
    excluded_from_analytics = BooleanField('Exclude from Analytics')
    invites_remaining = IntegerField(
        'Invites Remaining',
        default=0,
        validators=[DataRequired(), NumberRange(min=0)]
    )
    ban_type = SelectField(
        'Account Status',
        choices=[
            ('active', 'Active'),
            ('temp', 'Temporarily Suspended'),
            ('perm', 'Permanently Banned')
        ],
        validators=[DataRequired()]
    )
    suspend_days = IntegerField(
        'Suspend days',
        default=7,
        validators=[Optional(), NumberRange(min=1, max=365)]
    )
    submit = SubmitField('Save')


class AdjustInvitesForm(FlaskForm):
    delta = IntegerField(
        'Adjustment',
        validators=[DataRequired(), NumberRange()]
    )
    submit = SubmitField('Adjust Invites')
