from django import forms
from django.core.mail import send_mail
from .models import Post, Comment


class CommentCreateForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = (
            'content_type',
            'object_id',
            'content',
        )
        labels = {
            'content': ''
        }
        widgets = {
            'content_type': forms.HiddenInput(),
            'object_id': forms.HiddenInput(),
            'content': forms.Textarea(attrs={
                'placeholder': "Add your comment here, supports markdown"
            })
        }

    def send_notification(self):
        # subject = self.cleaned_data['content_object']
        author = self.instance.author
        message = self.cleaned_data.get('content')
        post = Post.objects.get(pk=self.cleaned_data['object_id'])
        recipients = list(post.recipients.all().values_list('email', flat=True))
        if author in recipients:
            recipients.remove(author.email)

        send_mail(
            post.title,
            message,
            author.email,
            recipients
        )


class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = [
            'title',
            'content',
            'recipients'
        ]
        labels = {
            'content': ''
        }
        widgets = {
            'recipients': forms.CheckboxSelectMultiple()
        }
