"""
Themis forms — profile settings and API key management.
"""
from django import forms

from themis.models import UserProfile, UserAPIKey


class ProfileSettingsForm(forms.ModelForm):
    """Form for editing user preferences."""

    class Meta:
        model = UserProfile
        fields = [
            "home_timezone",
            "current_timezone",
            "date_format",
            "time_format",
            "thousand_separator",
            "week_start",
            "theme_mode",
            "theme_name",
            "dark_theme_name",
            "notifications_enabled",
            "notifications_min_level",
            "browser_notifications_enabled",
            "notification_retention_days",
        ]
        widgets = {
            "home_timezone": forms.Select(attrs={"class": "select select-bordered w-full"}),
            "current_timezone": forms.Select(attrs={"class": "select select-bordered w-full"}),
            "date_format": forms.Select(attrs={"class": "select select-bordered w-full"}),
            "time_format": forms.Select(attrs={"class": "select select-bordered w-full"}),
            "thousand_separator": forms.Select(attrs={"class": "select select-bordered w-full"}),
            "week_start": forms.Select(attrs={"class": "select select-bordered w-full"}),
            "theme_mode": forms.Select(attrs={"class": "select select-bordered w-full"}),
            "theme_name": forms.Select(attrs={"class": "select select-bordered w-full"}),
            "dark_theme_name": forms.Select(attrs={"class": "select select-bordered w-full"}),
            "notifications_enabled": forms.CheckboxInput(attrs={"class": "toggle toggle-primary"}),
            "notifications_min_level": forms.Select(attrs={"class": "select select-bordered w-full"}),
            "browser_notifications_enabled": forms.CheckboxInput(attrs={"class": "toggle toggle-primary"}),
            "notification_retention_days": forms.NumberInput(attrs={
                "class": "input input-bordered w-full",
                "min": "1",
                "max": "365",
            }),
        }


class APIKeyCreateForm(forms.Form):
    """Form for creating a new API key. Handles encryption before save."""

    service_name = forms.CharField(
        max_length=100,
        widget=forms.TextInput(attrs={
            "class": "input input-bordered w-full",
            "placeholder": "e.g. OpenAI, Anthropic, CalDAV",
        }),
    )
    key_type = forms.ChoiceField(
        choices=UserAPIKey.KEY_TYPE_CHOICES,
        widget=forms.Select(attrs={"class": "select select-bordered w-full"}),
    )
    label = forms.CharField(
        max_length=100,
        required=False,
        widget=forms.TextInput(attrs={
            "class": "input input-bordered w-full",
            "placeholder": "Optional nickname (e.g. 'Work account')",
        }),
    )
    key_value = forms.CharField(
        widget=forms.Textarea(attrs={
            "class": "textarea textarea-bordered w-full",
            "rows": 3,
            "placeholder": "Paste your key or credential here",
        }),
        help_text="This value will be encrypted at rest. "
                  "It will not be displayed in full after saving.",
    )
    instructions = forms.CharField(
        required=False,
        widget=forms.Textarea(attrs={
            "class": "textarea textarea-bordered w-full",
            "rows": 4,
            "placeholder": "How to obtain and use this key...",
        }),
    )
    help_url = forms.URLField(
        required=False,
        widget=forms.URLInput(attrs={
            "class": "input input-bordered w-full",
            "placeholder": "https://docs.example.com/api-keys",
        }),
    )


class APIKeyEditForm(forms.ModelForm):
    """Form for editing API key metadata (not the key value itself)."""

    class Meta:
        model = UserAPIKey
        fields = [
            "service_name",
            "key_type",
            "label",
            "instructions",
            "help_url",
            "is_active",
        ]
        widgets = {
            "service_name": forms.TextInput(attrs={"class": "input input-bordered w-full"}),
            "key_type": forms.Select(attrs={"class": "select select-bordered w-full"}),
            "label": forms.TextInput(attrs={"class": "input input-bordered w-full"}),
            "instructions": forms.Textarea(attrs={
                "class": "textarea textarea-bordered w-full",
                "rows": 4,
            }),
            "help_url": forms.URLInput(attrs={"class": "input input-bordered w-full"}),
            "is_active": forms.CheckboxInput(attrs={"class": "toggle toggle-primary"}),
        }
