from django import forms
from django.utils.text import slugify

from .models import Message, ResakaProfile


AVATAR_CHOICES = [
    ("#22D3EE", "Lagoon"),
    ("#FF7A59", "Sunset"),
    ("#9EF3D5", "Mint"),
    ("#F7C948", "Gold"),
]

REACTION_CHOICES = [
    ("❤️", "Love"),
    ("🔥", "Fire"),
    ("😂", "Laugh"),
    ("👏", "Clap"),
]


class ProfileForm(forms.ModelForm):
    class Meta:
        model = ResakaProfile
        fields = ["display_name", "handle", "status_text", "avatar_color"]
        widgets = {
            "display_name": forms.TextInput(attrs={"placeholder": "Ex. Rija", "class": "form-control"}),
            "handle": forms.TextInput(attrs={"placeholder": "ex. rija_resaka", "class": "form-control"}),
            "status_text": forms.TextInput(attrs={"placeholder": "Disponible pour discuter ce soir", "class": "form-control"}),
            "avatar_color": forms.Select(attrs={"class": "form-select"}, choices=AVATAR_CHOICES),
        }
        labels = {
            "display_name": "Nom affiché",
            "handle": "Pseudo",
            "status_text": "Statut",
            "avatar_color": "Couleur avatar",
        }

    def clean_handle(self):
        handle = slugify(self.cleaned_data["handle"])
        if not handle:
            raise forms.ValidationError("Choisissez un pseudo valide.")
        if ResakaProfile.objects.filter(handle=handle).exists():
            raise forms.ValidationError("Ce pseudo existe déjà.")
        return handle


class ConversationStartForm(forms.Form):
    recipient = forms.ModelChoiceField(
        queryset=ResakaProfile.objects.none(),
        label="Destinataire",
        widget=forms.Select(attrs={"class": "form-select"}),
    )
    body = forms.CharField(
        label="Premier message",
        max_length=1000,
        widget=forms.Textarea(
            attrs={
                "rows": 3,
                "placeholder": "Écris ton premier message privé…",
                "class": "form-control",
            }
        ),
    )

    def __init__(self, *args, active_profile=None, **kwargs):
        super().__init__(*args, **kwargs)
        queryset = ResakaProfile.objects.all().order_by("display_name")
        if active_profile:
            queryset = queryset.exclude(pk=active_profile.pk)
        self.fields["recipient"].queryset = queryset
        self.active_profile = active_profile

    def clean_recipient(self):
        recipient = self.cleaned_data["recipient"]
        if self.active_profile and recipient.pk == self.active_profile.pk:
            raise forms.ValidationError("Choisissez un autre profil pour démarrer une discussion.")
        return recipient


class MessageForm(forms.ModelForm):
    class Meta:
        model = Message
        fields = ["body"]
        widgets = {
            "body": forms.Textarea(
                attrs={
                    "rows": 3,
                    "placeholder": "Tapez un message privé ou ajoutez des emojis…",
                    "class": "form-control js-message-body",
                }
            )
        }
        labels = {"body": "Message"}


class ReactionForm(forms.Form):
    reaction = forms.ChoiceField(
        choices=REACTION_CHOICES,
        widget=forms.HiddenInput(),
    )
