import type { Meta, StoryObj } from "@storybook/react";
import { expect } from "@storybook/test";
import { withMockGrackleRoute } from "@grackle-ai/web-components";
import { TaskPage } from "./TaskPage.js";

const meta: Meta<typeof TaskPage> = {
  component: TaskPage,
  parameters: { skipRouter: true },
};
export default meta;
type Story = StoryObj<typeof meta>;

/** Overview tab renders for a task with status "working". */
export const WorkingTaskOverview: Story = {
  decorators: [withMockGrackleRoute(["/tasks/task-001"], "/tasks/:taskId")],
  play: async ({ canvas }) => {
    // Task title should be visible
    await expect(canvas.getByTestId("task-title")).toBeInTheDocument();
    // Task status badge should be visible
    await expect(canvas.getByTestId("task-status")).toBeInTheDocument();
    // Tab bar should have Overview, Stream, Findings tabs
    await expect(canvas.getByRole("tab", { name: "Overview" })).toBeInTheDocument();
    await expect(canvas.getByRole("tab", { name: "Stream" })).toBeInTheDocument();
    await expect(canvas.getByRole("tab", { name: "Findings" })).toBeInTheDocument();
  },
};

/** Stream tab is active when the URL ends with /stream. */
export const TaskWithStream: Story = {
  decorators: [withMockGrackleRoute(["/tasks/task-001/stream"], "/tasks/:taskId/stream")],
  play: async ({ canvas }) => {
    // Task title should be visible
    await expect(canvas.getByTestId("task-title")).toBeInTheDocument();
    // Stream tab should be selected
    const streamTab = canvas.getByRole("tab", { name: "Stream" });
    await expect(streamTab).toHaveAttribute("aria-selected", "true");
  },
};

/** Not-started task shows Start button and Overview tab. */
export const NotStartedTask: Story = {
  decorators: [withMockGrackleRoute(["/tasks/task-001c"], "/tasks/:taskId")],
  play: async ({ canvas }) => {
    await expect(canvas.getByTestId("task-title")).toBeInTheDocument();
    // Overview tab should be selected by default for not_started tasks
    const overviewTab = canvas.getByRole("tab", { name: "Overview" });
    await expect(overviewTab).toHaveAttribute("aria-selected", "true");
  },
};

/** Blocked task on Stream tab hides Start CTA and shows blocked message. */
export const BlockedTaskStreamHidesStart: Story = {
  decorators: [withMockGrackleRoute(["/tasks/task-001c/stream"], "/tasks/:taskId/stream")],
  play: async ({ canvas }) => {
    // Stream tab should be selected
    const streamTab = canvas.getByRole("tab", { name: "Stream" });
    await expect(streamTab).toHaveAttribute("aria-selected", "true");
    // Start CTA should NOT be present (task is blocked by task-001b which is "working")
    await expect(canvas.queryByTestId("stream-start-cta")).toBeNull();
    // Blocked message should be visible instead
    await expect(canvas.getByTestId("stream-blocked-message")).toBeInTheDocument();
  },
};
