from django import forms
from django.forms import ValidationError, widgets

from webapp import models


class ArticleForm(forms.ModelForm):
    class Meta:
        model = models.Article
        fields = ('title', 'text', 'tags')
        
        widgets = {
            'title': widgets.TextInput(attrs={
                'class': 'form-control mb-3',
                'placeholder': 'Title',
            }),
            'text': widgets.Textarea(attrs={
                'class': 'form-control mb-3',
                'placeholder': 'Write some text',
            }),
            'tags': widgets.CheckboxSelectMultiple({
                'class': 'list-unstyled ps-0 mb-3',
            })
        }
    
    def clean(self):
        if self.data['title'] == self.data['text']:
            raise ValidationError('Text should not duplicate title')

        return super().clean()


class SearchForm(forms.Form):
    search = forms.CharField(max_length=100, required=False, label='Найти')


class CommentForm(forms.ModelForm):
    class Meta:
        model = models.Comment
        fields = ('text',)
        
        widgets = {
            'text': widgets.Textarea(attrs={
                'class': 'form-control mb-3',
                'placeholder': 'Write some text',
            }),
        }
