from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed
from wtforms import StringField, TextAreaField, PasswordField, BooleanField, SelectField, FloatField, SubmitField, HiddenField
from wtforms.validators import DataRequired, Email, Length, EqualTo, ValidationError, Optional
from models import User

class LoginForm(FlaskForm):
    username = StringField('Nom d\'utilisateur', validators=[DataRequired()])
    password = PasswordField('Mot de passe', validators=[DataRequired()])
    remember_me = BooleanField('Se souvenir de moi')
    submit = SubmitField('Connexion')

class UserForm(FlaskForm):
    username = StringField('Nom d\'utilisateur', validators=[DataRequired(), Length(min=3, max=64)])
    email = StringField('Email', validators=[DataRequired(), Email()])
    password = PasswordField('Mot de passe', validators=[Length(min=6, max=128)])
    confirm_password = PasswordField('Confirmer le mot de passe', 
                                    validators=[EqualTo('password', message="Les mots de passe doivent correspondre")])
    role = SelectField('Rôle', choices=[('user', 'Utilisateur'), ('admin', 'Administrateur')])
    submit = SubmitField('Enregistrer')
    
    def validate_username(self, username):
        user = User.query.filter_by(username=username.data).first()
        if user and user.id != getattr(self, 'user_id', None):
            raise ValidationError('Ce nom d\'utilisateur est déjà pris.')
    
    def validate_email(self, email):
        user = User.query.filter_by(email=email.data).first()
        if user and user.id != getattr(self, 'user_id', None):
            raise ValidationError('Cette adresse email est déjà utilisée.')

class CategoryForm(FlaskForm):
    name = StringField('Nom', validators=[DataRequired(), Length(max=50)])
    description = TextAreaField('Description')
    parent_id = SelectField('Catégorie parente', coerce=int, validators=[Optional()])
    submit = SubmitField('Enregistrer')

class ProductForm(FlaskForm):
    name = StringField('Nom', validators=[DataRequired(), Length(max=100)])
    description = TextAreaField('Description', validators=[DataRequired()])
    price = FloatField('Prix', validators=[Optional()])
    image = FileField('Image', validators=[FileAllowed(['jpg', 'png', 'jpeg'], 'Images uniquement!')])
    category_id = SelectField('Catégorie', coerce=int, validators=[DataRequired()])
    is_available = BooleanField('Disponible', default=True)
    condition = SelectField('État', choices=[
        ('new', 'Neuf'),
        ('reconditioned', 'Reconditionné'),
        ('used', 'Occasion')
    ])
    submit = SubmitField('Enregistrer')

class ServiceForm(FlaskForm):
    name = StringField('Nom', validators=[DataRequired(), Length(max=100)])
    description = TextAreaField('Description', validators=[DataRequired()])
    image = FileField('Image', validators=[FileAllowed(['jpg', 'png', 'jpeg'], 'Images uniquement!')])
    is_active = BooleanField('Actif')
    submit = SubmitField('Enregistrer')

class ContactForm(FlaskForm):
    name = StringField('Nom', validators=[DataRequired(), Length(max=100)])
    email = StringField('Email', validators=[DataRequired(), Email()])
    phone = StringField('Téléphone', validators=[Length(max=20)])
    message = TextAreaField('Message', validators=[DataRequired()])
    submit = SubmitField('Envoyer')



class SettingsForm(FlaskForm):
    company_name = StringField('Nom de l\'entreprise', validators=[DataRequired()])
    company_description = TextAreaField('Description de l\'entreprise')
    company_address = StringField('Adresse')
    company_email = StringField('Email', validators=[Email()])
    company_phone = StringField('Téléphone')
    submit = SubmitField('Enregistrer')

class PageForm(FlaskForm):
    title = StringField('Titre', validators=[DataRequired(), Length(max=100)])
    slug = StringField('Slug (URL)', validators=[DataRequired(), Length(max=100)])
    description = TextAreaField('Description', validators=[Length(max=255)])
    is_active = BooleanField('Actif')
    meta_keywords = StringField('Mots-clés SEO', validators=[Length(max=255)])
    meta_description = TextAreaField('Description SEO', validators=[Length(max=255)])
    submit = SubmitField('Enregistrer')
    
    def validate_slug(self, slug):
        from models import Page
        page = Page.query.filter_by(slug=slug.data).first()
        if page and page.id != getattr(self, 'page_id', None):
            raise ValidationError('Ce slug est déjà utilisé pour une autre page.')

class SectionForm(FlaskForm):
    page_id = SelectField('Page', coerce=int, validators=[DataRequired()])
    title = StringField('Titre', validators=[DataRequired(), Length(max=100)])
    content = TextAreaField('Contenu')
    section_type = SelectField('Type de section', choices=[
        ('text', 'Texte simple'),
        ('banner', 'Bannière'),
        ('gallery', 'Galerie'),
        ('contact', 'Formulaire de contact'),
        ('products', 'Liste de produits'),
        ('services', 'Liste de services'),
        ('custom', 'Personnalisé')
    ])
    order = StringField('Ordre d\'affichage', default='0')
    is_active = BooleanField('Actif', default=True)
    background_color = StringField('Couleur de fond', default='#ffffff')
    css_class = StringField('Classes CSS additionnelles')
    image = FileField('Image de fond', validators=[FileAllowed(['jpg', 'png', 'jpeg'], 'Images uniquement!')])
    submit = SubmitField('Enregistrer')
