import { describe, it, expect } from "vitest";
import { render, screen } from "@testing-library/react";
import TreatmentTimeline, {
  treatmentStepsWithUrgency,
  type TreatmentStep,
  type UrgencyLevel,
} from "@/components/TreatmentTimeline";

describe("TreatmentTimeline", () => {
  const mockSteps: TreatmentStep[] = [
    { action: "Remove affected leaves immediately", urgency: "immediate" },
    { action: "Apply copper fungicide within a week", urgency: "within-week" },
    { action: "Monitor plant health regularly", urgency: "ongoing" },
  ];

  it("renders all treatment steps", () => {
    render(<TreatmentTimeline steps={mockSteps} />);
    expect(screen.getByText("Remove affected leaves immediately")).toBeInTheDocument();
    expect(screen.getByText("Apply copper fungicide within a week")).toBeInTheDocument();
    expect(screen.getByText("Monitor plant health regularly")).toBeInTheDocument();
  });

  it("renders numbered step indicators", () => {
    render(<TreatmentTimeline steps={mockSteps} />);
    expect(screen.getByText("1")).toBeInTheDocument();
    expect(screen.getByText("2")).toBeInTheDocument();
    expect(screen.getByText("3")).toBeInTheDocument();
  });

  it("renders urgency badges", () => {
    render(<TreatmentTimeline steps={mockSteps} />);
    expect(screen.getByText("Immediate")).toBeInTheDocument();
    expect(screen.getByText("Within a week")).toBeInTheDocument();
    expect(screen.getByText("Ongoing")).toBeInTheDocument();
  });

  it("renders disclaimer at bottom", () => {
    render(<TreatmentTimeline steps={mockSteps} />);
    expect(screen.getByText(/Treatments may vary/i)).toBeInTheDocument();
    expect(screen.getByText(/consult a certified plant pathologist/i)).toBeInTheDocument();
  });

  it("renders empty state when no steps provided", () => {
    render(<TreatmentTimeline steps={[]} />);
    expect(screen.getByText(/No treatment steps available/i)).toBeInTheDocument();
  });

  it("renders single step without connector line", () => {
    render(<TreatmentTimeline steps={[mockSteps[0]]} />);
    expect(screen.getByText("Remove affected leaves immediately")).toBeInTheDocument();
  });

  it("uses list role for timeline", () => {
    render(<TreatmentTimeline steps={mockSteps} />);
    const list = screen.getByRole("list");
    expect(list).toBeInTheDocument();
  });
});

describe("treatmentStepsWithUrgency", () => {
  it("maps first step to immediate urgency", () => {
    const steps = treatmentStepsWithUrgency(["First action"]);
    expect(steps[0].urgency).toBe("immediate");
    expect(steps[0].action).toBe("First action");
  });

  it("maps second step to within-week urgency", () => {
    const steps = treatmentStepsWithUrgency(["First", "Second"]);
    expect(steps[1].urgency).toBe("within-week");
  });

  it("maps remaining steps to ongoing urgency", () => {
    const steps = treatmentStepsWithUrgency(["First", "Second", "Third", "Fourth"]);
    expect(steps[2].urgency).toBe("ongoing");
    expect(steps[3].urgency).toBe("ongoing");
  });

  it("returns empty array for empty input", () => {
    const steps = treatmentStepsWithUrgency([]);
    expect(steps).toEqual([]);
  });

  it("preserves action text", () => {
    const actions = ["Action A", "Action B", "Action C"];
    const steps = treatmentStepsWithUrgency(actions);
    expect(steps.map((s) => s.action)).toEqual(actions);
  });
});
