# (c) cavaliba.com - test / field templates / native date & datetime pickers

"""
The date/datetime edit snippets render the browser-native pickers
(<input type="date"> / <input type="datetime-local">) instead of a plain text
input.

Both pickers silently show nothing when their value attribute isn't in the
exact wire format they expect, so what matters is the value attribute, not just
the type:
  * date       wants 'YYYY-MM-DD'  - what FieldDate stores, verbatim
  * datetime   wants 'YYYY-MM-DDThh:mm:ss' - the 'T' separator, where
               FieldDatetime stores 'YYYY-MM-DD hh:mm:ss'

The golden rows live in test_field_render_edit.py; this file pins the picker
specifics (type, step, separator) and the empty-value case, which the fully
populated golden fixture can't reach.
"""

from django.test import TestCase

from tests import helper


class TestFieldRenderEditDatePicker(TestCase):
    def datapoint(self, dataformat, value):
        """An edit-mode datapoint for a single-valued date/datetime field."""
        return {
            "fieldname": f"f_{dataformat}",
            "displayname": f"F {dataformat}",
            "description": "",
            "dataformat": dataformat,
            "dataformat_ext": "",
            "is_multi": False,
            "value": value,
        }

    def render(self, dataformat, value):
        """Normalized edit-mode markup for that datapoint."""
        return helper.render_datapoint(self.datapoint(dataformat, value), "edit")

    def test_date_uses_the_native_date_input(self):
        self.assertIn('<input type="date"', self.render("date", ["2026-01-15"]))

    def test_date_value_is_passed_through_unchanged(self):
        self.assertIn('value="2026-01-15"', self.render("date", ["2026-01-15"]))

    def test_date_empty_value_renders_an_empty_attribute(self):
        self.assertIn('value=""', self.render("date", []))

    def test_datetime_uses_the_native_datetime_local_input(self):
        self.assertIn(
            '<input type="datetime-local"', self.render("datetime", ["2026-01-15 10:20:30"])
        )

    def test_datetime_exposes_seconds_with_step_one(self):
        # default step=60 hides the seconds box and POSTs hh:mm only
        self.assertIn('step="1"', self.render("datetime", ["2026-01-15 10:20:30"]))

    def test_datetime_value_separator_is_rewritten_to_T(self):
        out = self.render("datetime", ["2026-01-15 10:20:30"])
        self.assertIn('value="2026-01-15T10:20:30"', out)
        self.assertNotIn("2026-01-15 10:20:30", out)

    def test_datetime_empty_value_does_not_render_a_bare_separator(self):
        # "T" alone is not a valid datetime-local value
        out = self.render("datetime", [])
        self.assertIn('value=""', out)
        self.assertNotIn('value="T"', out)

    def test_date_multi_is_still_unsupported(self):
        datapoint = self.datapoint("date", ["2026-01-15"])
        datapoint["is_multi"] = True
        out = helper.render_datapoint(datapoint, "edit")
        self.assertIn("Unsupported Multi-value", out)
        self.assertNotIn("<input", out)

    def test_datetime_multi_is_still_unsupported(self):
        datapoint = self.datapoint("datetime", ["2026-01-15 10:20:30"])
        datapoint["is_multi"] = True
        out = helper.render_datapoint(datapoint, "edit")
        self.assertIn("Unsupported Multi-value", out)
        self.assertNotIn("<input", out)
