"""
Tests for Themis custom form widgets.
"""
from django.test import TestCase

from themis.widgets import (
    DatePickerWidget,
    DateTimePickerWidget,
    MonthDayPickerWidget,
    TimePickerWidget,
)


class DatePickerWidgetTest(TestCase):
    """Tests for DatePickerWidget."""

    def test_default_attrs(self):
        """Widget has DaisyUI input classes and themis-date-picker class."""
        widget = DatePickerWidget()
        self.assertIn("input", widget.attrs["class"])
        self.assertIn("input-bordered", widget.attrs["class"])
        self.assertIn("themis-date-picker", widget.attrs["class"])

    def test_input_type(self):
        """Widget renders as text input."""
        widget = DatePickerWidget()
        self.assertEqual(widget.input_type, "text")

    def test_format(self):
        """Widget uses ISO date format."""
        widget = DatePickerWidget()
        self.assertEqual(widget.format, "%Y-%m-%d")

    def test_custom_attrs_merged(self):
        """Custom attrs are merged with defaults."""
        widget = DatePickerWidget(attrs={"id": "my-date", "placeholder": "Pick a date"})
        self.assertIn("themis-date-picker", widget.attrs["class"])
        self.assertEqual(widget.attrs["id"], "my-date")
        self.assertEqual(widget.attrs["placeholder"], "Pick a date")

    def test_media_css(self):
        """Widget includes datetime-widgets.css."""
        widget = DatePickerWidget()
        css_files = widget.media._css.get("all", [])
        self.assertTrue(
            any("datetime-widgets.css" in f for f in css_files)
        )

    def test_media_js(self):
        """Widget includes date-picker.js."""
        widget = DatePickerWidget()
        js_files = widget.media._js
        self.assertTrue(
            any("date-picker.js" in f for f in js_files)
        )

    def test_media_js_does_not_include_time(self):
        """Date-only widget does not include time-picker.js."""
        widget = DatePickerWidget()
        js_files = widget.media._js
        self.assertFalse(
            any("time-picker.js" in f for f in js_files)
        )


class TimePickerWidgetTest(TestCase):
    """Tests for TimePickerWidget."""

    def test_default_attrs(self):
        """Widget has DaisyUI input classes and themis-time-picker class."""
        widget = TimePickerWidget()
        self.assertIn("input", widget.attrs["class"])
        self.assertIn("themis-time-picker", widget.attrs["class"])

    def test_input_type(self):
        """Widget renders as text input."""
        widget = TimePickerWidget()
        self.assertEqual(widget.input_type, "text")

    def test_format(self):
        """Widget uses HH:MM time format."""
        widget = TimePickerWidget()
        self.assertEqual(widget.format, "%H:%M")

    def test_custom_attrs_merged(self):
        """Custom attrs are merged with defaults."""
        widget = TimePickerWidget(attrs={"id": "my-time"})
        self.assertIn("themis-time-picker", widget.attrs["class"])
        self.assertEqual(widget.attrs["id"], "my-time")

    def test_media_js(self):
        """Widget includes time-picker.js."""
        widget = TimePickerWidget()
        js_files = widget.media._js
        self.assertTrue(
            any("time-picker.js" in f for f in js_files)
        )

    def test_media_js_does_not_include_date(self):
        """Time-only widget does not include date-picker.js."""
        widget = TimePickerWidget()
        js_files = widget.media._js
        self.assertFalse(
            any("date-picker.js" in f for f in js_files)
        )

    def test_media_css(self):
        """Widget includes datetime-widgets.css."""
        widget = TimePickerWidget()
        css_files = widget.media._css.get("all", [])
        self.assertTrue(
            any("datetime-widgets.css" in f for f in css_files)
        )


class DateTimePickerWidgetTest(TestCase):
    """Tests for DateTimePickerWidget."""

    def test_default_attrs(self):
        """Widget has DaisyUI input classes and themis-datetime-picker class."""
        widget = DateTimePickerWidget()
        self.assertIn("input", widget.attrs["class"])
        self.assertIn("themis-datetime-picker", widget.attrs["class"])

    def test_input_type(self):
        """Widget renders as text input."""
        widget = DateTimePickerWidget()
        self.assertEqual(widget.input_type, "text")

    def test_format(self):
        """Widget uses combined date-time format."""
        widget = DateTimePickerWidget()
        self.assertEqual(widget.format, "%Y-%m-%d %H:%M")

    def test_custom_attrs_merged(self):
        """Custom attrs are merged with defaults."""
        widget = DateTimePickerWidget(attrs={"id": "my-datetime"})
        self.assertIn("themis-datetime-picker", widget.attrs["class"])
        self.assertEqual(widget.attrs["id"], "my-datetime")

    def test_media_includes_both_js(self):
        """DateTime widget includes both date-picker.js and time-picker.js."""
        widget = DateTimePickerWidget()
        js_files = widget.media._js
        self.assertTrue(
            any("date-picker.js" in f for f in js_files),
            "date-picker.js not found in DateTime widget media",
        )
        self.assertTrue(
            any("time-picker.js" in f for f in js_files),
            "time-picker.js not found in DateTime widget media",
        )

    def test_media_css(self):
        """Widget includes datetime-widgets.css."""
        widget = DateTimePickerWidget()
        css_files = widget.media._css.get("all", [])
        self.assertTrue(
            any("datetime-widgets.css" in f for f in css_files)
        )


class MonthDayPickerWidgetTest(TestCase):
    """Tests for MonthDayPickerWidget."""

    def test_default_attrs(self):
        """Widget has DaisyUI input classes and themis-monthday-picker class."""
        widget = MonthDayPickerWidget()
        self.assertIn("input", widget.attrs["class"])
        self.assertIn("themis-monthday-picker", widget.attrs["class"])

    def test_input_type(self):
        """Widget renders as text input."""
        widget = MonthDayPickerWidget()
        self.assertEqual(widget.input_type, "text")

    def test_placeholder(self):
        """Widget has MM-DD placeholder."""
        widget = MonthDayPickerWidget()
        self.assertEqual(widget.attrs["placeholder"], "MM-DD")

    def test_pattern(self):
        """Widget has HTML pattern for MM-DD validation."""
        widget = MonthDayPickerWidget()
        self.assertIn("pattern", widget.attrs)

    def test_custom_attrs_merged(self):
        """Custom attrs are merged with defaults."""
        widget = MonthDayPickerWidget(attrs={"id": "birthday"})
        self.assertIn("themis-monthday-picker", widget.attrs["class"])
        self.assertEqual(widget.attrs["id"], "birthday")

    def test_media_css(self):
        """Widget includes datetime-widgets.css."""
        widget = MonthDayPickerWidget()
        css_files = widget.media._css.get("all", [])
        self.assertTrue(
            any("datetime-widgets.css" in f for f in css_files)
        )

    def test_media_js(self):
        """Widget includes date-picker.js (for month-day calendar)."""
        widget = MonthDayPickerWidget()
        js_files = widget.media._js
        self.assertTrue(
            any("date-picker.js" in f for f in js_files)
        )
