import { beforeEach, describe, expect, it } from "vitest"
import { mount } from "@vue/test-utils"
import { createPinia, setActivePinia } from "pinia"
import InfoCard from "./InfoCard.vue"

describe("InfoCard", () => {
  beforeEach(() => {
    setActivePinia(createPinia())
  })

  it("mounts without error", () => {
    const wrapper = mount(InfoCard, { props: {} })
    expect(wrapper.exists()).toBe(true)
  })

  it("renders the title when set", () => {
    const wrapper = mount(InfoCard, {
      props: { title: "Account summary" },
    })
    expect(wrapper.text()).toContain("Account summary")
  })

  it("renders the subtitle when set", () => {
    const wrapper = mount(InfoCard, {
      props: { title: "Header", subtitle: "Helper subtitle" },
    })
    expect(wrapper.text()).toContain("Helper subtitle")
  })

  it("renders the card chrome by default", () => {
    const wrapper = mount(InfoCard, { props: {} })
    expect(wrapper.find("[data-testid='info-card-bare']").exists()).toBe(false)
  })

  it("renders without chrome when showCard is false", () => {
    const wrapper = mount(InfoCard, {
      props: { showCard: false },
    })
    expect(wrapper.find("[data-testid='info-card-bare']").exists()).toBe(true)
  })

  it("renders each field's label and resolved value", () => {
    const wrapper = mount(InfoCard, {
      props: {
        fields: [
          { label: "Name", value: "user.name" },
          { label: "Email", value: "user.email" },
        ],
        item: {
          user: { name: "Stephen Peek", email: "peek@gramercytech.com" },
        },
      },
    })
    expect(wrapper.text()).toContain("Name")
    expect(wrapper.text()).toContain("Stephen Peek")
    expect(wrapper.text()).toContain("Email")
    expect(wrapper.text()).toContain("peek@gramercytech.com")
    expect(wrapper.findAll("[data-testid='info-card-field']").length).toBe(2)
  })

  it("supports prepend/append modifiers on field values", () => {
    const wrapper = mount(InfoCard, {
      props: {
        fields: [
          { label: "Price", value: "price", prepend: "$", append: " USD" },
        ],
        item: { price: 42 },
      },
    })
    expect(wrapper.text()).toContain("$42 USD")
  })

  it("renders the header icon when configured", () => {
    const wrapper = mount(InfoCard, {
      props: { title: "T", icon: "star" },
    })
    expect(wrapper.find("[data-testid='info-card-icon-star']").exists()).toBe(
      true,
    )
  })

  it("renders an empty fields list cleanly", () => {
    const wrapper = mount(InfoCard, {
      props: { title: "Empty", fields: [] },
    })
    expect(wrapper.find("[data-testid='info-card-fields']").exists()).toBe(
      false,
    )
    expect(wrapper.text()).toContain("Empty")
  })
})
