from django import forms
from django.contrib.auth.models import User
from chat.models import UserProfile, Channel


class RegisterForm(forms.Form):
    username = forms.CharField(max_length=150)
    email = forms.EmailField()
    password = forms.CharField(widget=forms.PasswordInput)

    def clean_username(self):
        username = self.cleaned_data['username']
        if User.objects.filter(username=username).exists():
            raise forms.ValidationError('Username already taken.')
        return username

    def clean_email(self):
        email = self.cleaned_data['email']
        if User.objects.filter(email=email).exists():
            raise forms.ValidationError('Email already in use.')
        return email


class ProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ['avatar', 'bio']
        widgets = {
            'bio': forms.Textarea(attrs={'rows': 3}),
        }


class ChannelForm(forms.ModelForm):
    class Meta:
        model = Channel
        fields = ['name', 'description']


class MessageFileForm(forms.Form):
    file = forms.FileField(required=False)
    content = forms.CharField(required=False, widget=forms.HiddenInput)
