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

describe("SwitchBoolean (props-based composition)", () => {
  it("mounts and renders the label", () => {
    const wrapper = mount(SwitchBoolean, { props: { label: "Notifications" } })
    expect(wrapper.text()).toContain("Notifications")
  })

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

  it("forwards modelValue to the switch via aria-checked", () => {
    const wrapper = mount(SwitchBoolean, {
      props: { label: "X", modelValue: true },
    })
    expect(wrapper.find("[role='switch']").attributes("aria-checked")).toBe(
      "true",
    )
  })

  it("emits update:modelValue when toggled", async () => {
    const wrapper = mount(SwitchBoolean, {
      props: { label: "X", modelValue: false },
    })
    await wrapper.find("[role='switch']").trigger("click")
    const emitted = wrapper.emitted("update:modelValue")
    expect(emitted).toBeTruthy()
    expect(emitted![0]).toEqual([true])
  })

  it("shows trueLabel / falseLabel suffix based on state", async () => {
    const onWrapper = mount(SwitchBoolean, {
      props: {
        label: "Visibility",
        trueLabel: "Online",
        falseLabel: "Offline",
        modelValue: true,
      },
    })
    expect(onWrapper.text()).toContain("Online")
    const offWrapper = mount(SwitchBoolean, {
      props: {
        label: "Visibility",
        trueLabel: "Online",
        falseLabel: "Offline",
        modelValue: false,
      },
    })
    expect(offWrapper.text()).toContain("Offline")
  })

  it("respects disabled / required", () => {
    const wrapper = mount(SwitchBoolean, {
      props: { label: "X", disabled: true, required: true },
    })
    expect(wrapper.find("[role='switch']").attributes("disabled")).toBeDefined()
  })

  it("renders helpText and errors", () => {
    const wrapper = mount(SwitchBoolean, {
      props: { label: "X", helpText: "Hint", errors: ["Bad"] },
    })
    expect(wrapper.find("[data-testid='field-frame-help']").text()).toBe("Hint")
    expect(wrapper.find("[data-testid='field-frame-errors']").text()).toContain(
      "Bad",
    )
  })

  it("works without any props at all", () => {
    const wrapper = mount(SwitchBoolean)
    expect(wrapper.find("[role='switch']").exists()).toBe(true)
  })
})
