// @vitest-environment jsdom
import { describe, it, expect, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import { DaysOfWeekStrip } from "./DaysOfWeekStrip.js";

vi.mock("@/i18n/provider.js", () => ({
  useTranslation: () => ({ t: (k: string) => k }),
}));

describe("DaysOfWeekStrip", () => {
  it("renders 7 day boxes", () => {
    render(<DaysOfWeekStrip flightBitString="1234567" />);
    const boxes = screen.getAllByTestId(/^day-of-week-\d$/);
    expect(boxes).toHaveLength(7);
  });

  // Real API shape: `daysOfWeek.flight` is a STRING OF DIGITS where each
  // character is the ISO weekday number of an active day (1=Mon..7=Sun).
  // E.g. "156" means Mon + Fri + Sat are operating; "1234567" = every day.

  it("marks Mon/Tue/Wed active, Thu-Sun inactive for '123'", () => {
    render(<DaysOfWeekStrip flightBitString="123" />);
    expect(screen.getByTestId("day-of-week-0").className).not.toMatch(/--inactive/);
    expect(screen.getByTestId("day-of-week-1").className).not.toMatch(/--inactive/);
    expect(screen.getByTestId("day-of-week-2").className).not.toMatch(/--inactive/);
    expect(screen.getByTestId("day-of-week-3").className).toMatch(/--inactive/);
    expect(screen.getByTestId("day-of-week-6").className).toMatch(/--inactive/);
  });

  it("marks all active for '1234567'", () => {
    render(<DaysOfWeekStrip flightBitString="1234567" />);
    for (let i = 0; i < 7; i++) {
      expect(screen.getByTestId(`day-of-week-${i}`).className).not.toMatch(/--inactive/);
    }
  });

  it("marks all inactive for ''", () => {
    render(<DaysOfWeekStrip flightBitString="" />);
    for (let i = 0; i < 7; i++) {
      expect(screen.getByTestId(`day-of-week-${i}`).className).toMatch(/--inactive/);
    }
  });

  it("renders DAYS.1 through DAYS.7 labels in order", () => {
    render(<DaysOfWeekStrip flightBitString="1234567" />);
    expect(screen.getByTestId("day-of-week-0").textContent).toContain("DAYS.1");
    expect(screen.getByTestId("day-of-week-6").textContent).toContain("DAYS.7");
  });

  // Real-world: SU 6188 returns "156" → Mon, Fri, Sat operating.
  it("Mon/Fri/Sat pattern '156': positions 0,4,5 active; 1,2,3,6 inactive", () => {
    render(<DaysOfWeekStrip flightBitString="156" />);
    expect(screen.getByTestId("day-of-week-0").className).not.toMatch(/--inactive/);
    expect(screen.getByTestId("day-of-week-4").className).not.toMatch(/--inactive/);
    expect(screen.getByTestId("day-of-week-5").className).not.toMatch(/--inactive/);
    expect(screen.getByTestId("day-of-week-1").className).toMatch(/--inactive/);
    expect(screen.getByTestId("day-of-week-2").className).toMatch(/--inactive/);
    expect(screen.getByTestId("day-of-week-3").className).toMatch(/--inactive/);
    expect(screen.getByTestId("day-of-week-6").className).toMatch(/--inactive/);
  });

  it("Tue-only '2': position 1 active; all others inactive", () => {
    render(<DaysOfWeekStrip flightBitString="2" />);
    expect(screen.getByTestId("day-of-week-1").className).not.toMatch(/--inactive/);
    for (const i of [0, 2, 3, 4, 5, 6]) {
      expect(screen.getByTestId(`day-of-week-${i}`).className).toMatch(/--inactive/);
    }
  });

  it("ignores non-digit characters and out-of-range digits", () => {
    render(<DaysOfWeekStrip flightBitString="1x9 7" />);
    expect(screen.getByTestId("day-of-week-0").className).not.toMatch(/--inactive/);
    expect(screen.getByTestId("day-of-week-6").className).not.toMatch(/--inactive/);
    for (const i of [1, 2, 3, 4, 5]) {
      expect(screen.getByTestId(`day-of-week-${i}`).className).toMatch(/--inactive/);
    }
  });
});
