import { describe, it, expect } from "vitest"
import { mount } from "@vue/test-utils"
import DateTimePicker from "./DateTimePicker.vue"

describe("DateTimePicker (props-based composition)", () => {
  describe("rendering", () => {
    it("renders the label and trigger button", () => {
      const wrapper = mount(DateTimePicker, {
        props: { label: "Starts at" },
      })
      expect(wrapper.text()).toContain("Starts at")
      expect(wrapper.find("button").exists()).toBe(true)
    })

    it("shows the default placeholder when no value is set", () => {
      const wrapper = mount(DateTimePicker, {
        props: { label: "Starts at" },
      })
      expect(wrapper.text()).toContain("Pick a date & time")
    })

    it("hides the label when hideLabel is true", () => {
      const wrapper = mount(DateTimePicker, {
        props: { label: "Starts at", hideLabel: true },
      })
      expect(wrapper.find("[data-testid='field-frame-label']").exists()).toBe(
        false,
      )
    })
  })

  describe("value binding", () => {
    it("renders an ISO datetime as date + time in the trigger", () => {
      const wrapper = mount(DateTimePicker, {
        props: { label: "Starts at", modelValue: "2024-06-15T13:30:00" },
      })
      expect(wrapper.text()).toContain("2024-06-15")
      expect(wrapper.text()).toContain("13:30")
    })
  })

  describe("state matrix", () => {
    it("disables the trigger when disabled", () => {
      const wrapper = mount(DateTimePicker, {
        props: { label: "Starts at", disabled: true },
      })
      expect(wrapper.find("button").attributes("disabled")).toBeDefined()
    })

    it("marks the trigger as readonly via data attribute", () => {
      const wrapper = mount(DateTimePicker, {
        props: { label: "Starts at", readonly: true },
      })
      expect(wrapper.find("button").attributes("data-readonly")).toBe("true")
    })

    it("renders the asterisk for required", () => {
      const wrapper = mount(DateTimePicker, {
        props: { label: "Starts at", required: true },
      })
      expect(wrapper.html()).toContain("after:content-['*']")
    })

    it("renders help text", () => {
      const wrapper = mount(DateTimePicker, {
        props: { label: "Starts at", helpText: "Pick start time" },
      })
      expect(wrapper.text()).toContain("Pick start time")
    })
  })

  describe("errors", () => {
    it("renders errors from props", () => {
      const wrapper = mount(DateTimePicker, {
        props: { label: "Starts at", errors: ["Required"] },
      })
      expect(wrapper.text()).toContain("Required")
    })
  })

  it("works without any props at all", () => {
    const wrapper = mount(DateTimePicker)
    expect(wrapper.find("button").exists()).toBe(true)
  })
})
