"""
Custom form widgets for date, time, and datetime fields.

These widgets use DaisyUI input classes and provide JavaScript enhancement
via CSS class hooks. The accompanying static files render a full calendar
date picker, time picker, and combined datetime picker — all respecting the
user's display-format, week-start, and 12/24-hour preferences stored in
their ``UserProfile``.
"""
from django import forms


class DatePickerWidget(forms.DateInput):
    """
    Date picker widget using DaisyUI input styling.

    Renders a text input that opens a calendar popup on click/focus.
    The JavaScript reads ``data-user-date-format`` and
    ``data-user-week-start`` from the ``<body>`` tag (set by
    ``{% themis_body_attrs %}``).
    """

    class Media:
        css = {"all": ("themis/css/datetime-widgets.css",)}
        js = ("themis/js/date-picker.js",)

    def __init__(self, attrs=None):
        default_attrs = {
            "class": "input input-bordered w-full themis-date-picker",
            "type": "text",
        }
        if attrs:
            default_attrs.update(attrs)
        super().__init__(attrs=default_attrs, format="%Y-%m-%d")


class TimePickerWidget(forms.TimeInput):
    """
    Time picker widget using DaisyUI input styling.

    Renders a text input that opens an hour/minute selector on click/focus.
    The JavaScript reads ``data-user-time-format`` from the ``<body>`` tag.
    """

    class Media:
        css = {"all": ("themis/css/datetime-widgets.css",)}
        js = ("themis/js/time-picker.js",)

    def __init__(self, attrs=None):
        default_attrs = {
            "class": "input input-bordered w-full themis-time-picker",
            "type": "text",
        }
        if attrs:
            default_attrs.update(attrs)
        super().__init__(attrs=default_attrs, format="%H:%M")


class DateTimePickerWidget(forms.DateTimeInput):
    """
    Combined date and time picker widget using DaisyUI input styling.

    Clicking the input opens a calendar for the date portion.
    A clock icon button on the right opens the time picker.
    Both scripts are included via the Media class.
    """

    class Media:
        css = {"all": ("themis/css/datetime-widgets.css",)}
        js = (
            "themis/js/date-picker.js",
            "themis/js/time-picker.js",
        )

    def __init__(self, attrs=None):
        default_attrs = {
            "class": "input input-bordered w-full themis-datetime-picker",
            "type": "text",
        }
        if attrs:
            default_attrs.update(attrs)
        super().__init__(attrs=default_attrs, format="%Y-%m-%d %H:%M")


class MonthDayPickerWidget(forms.TextInput):
    """
    Month-day picker for recurring dates (birthdays, anniversaries).

    No year component — opens a calendar in month-day-only mode.
    Uses the ``themis-monthday-picker`` class.
    """

    class Media:
        css = {"all": ("themis/css/datetime-widgets.css",)}
        js = ("themis/js/date-picker.js",)

    def __init__(self, attrs=None):
        default_attrs = {
            "class": "input input-bordered w-full themis-monthday-picker",
            "type": "text",
            "placeholder": "MM-DD",
            "pattern": r"(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])",
        }
        if attrs:
            default_attrs.update(attrs)
        super().__init__(attrs=default_attrs)
