from django import forms

from .models import PostizInstallBrief


class PostizInstallBriefForm(forms.ModelForm):
    class Meta:
        model = PostizInstallBrief
        fields = [
            "title",
            "public_url",
            "node_version",
            "package_manager",
            "postgres_ready",
            "redis_ready",
            "temporal_ready",
            "email_provider",
            "upload_strategy",
            "notes",
        ]
        widgets = {
            "title": forms.TextInput(
                attrs={
                    "class": "form-control form-control-lg",
                    "placeholder": "Hosted Postiz workspace",
                }
            ),
            "public_url": forms.URLInput(
                attrs={
                    "class": "form-control form-control-lg",
                    "placeholder": "https://your-machine.example.com",
                }
            ),
            "node_version": forms.Select(attrs={"class": "form-select"}),
            "package_manager": forms.Select(attrs={"class": "form-select"}),
            "email_provider": forms.Select(attrs={"class": "form-select"}),
            "upload_strategy": forms.Select(attrs={"class": "form-select"}),
            "notes": forms.Textarea(
                attrs={
                    "class": "form-control",
                    "rows": 5,
                    "placeholder": "Optional notes about ports, reverse proxy decisions, or missing credentials.",
                }
            ),
            "postgres_ready": forms.CheckboxInput(attrs={"class": "form-check-input"}),
            "redis_ready": forms.CheckboxInput(attrs={"class": "form-check-input"}),
            "temporal_ready": forms.CheckboxInput(attrs={"class": "form-check-input"}),
        }
        help_texts = {
            "title": "A friendly name for this VM install brief.",
            "public_url": "Use the public URL already attached to this machine.",
            "node_version": "Postiz development requires Node.js 18 or newer.",
            "package_manager": "pnpm is the default workflow in the development guide.",
            "postgres_ready": "Check this if PostgreSQL is already available on the VM.",
            "redis_ready": "Check this if Redis is already available on the VM.",
            "temporal_ready": "Check this if the Temporal stack is already reachable.",
            "email_provider": "Pick the first email path you expect to configure.",
            "upload_strategy": "Choose how uploaded files should be stored initially.",
        }

    def clean_title(self):
        title = self.cleaned_data["title"].strip()
        if len(title) < 3:
            raise forms.ValidationError("Use at least 3 characters so the brief is recognizable.")
        return title

    def clean_public_url(self):
        public_url = self.cleaned_data["public_url"].strip()
        if not public_url.startswith(("http://", "https://")):
            raise forms.ValidationError("Include http:// or https:// so the reverse-proxy target is unambiguous.")
        return public_url
