import { screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { renderWithProviders } from "../utils/testHelpers";
import { TasksPage } from "./tasks";

const mutateUpdateTask = vi.fn();
const mutateDeleteTask = vi.fn();

vi.mock("../hooks/tablos", () => ({
  useTablosList: () => ({
    data: [
      {
        id: "tablo-1",
        name: "Projet Alpha",
        color: "bg-blue-500",
      },
    ],
    isLoading: false,
  }),
}));

vi.mock("../hooks/tasks", () => ({
  useAllTasks: () => ({
    data: [
      {
        id: "task-1",
        tablo_id: "tablo-1",
        title: "Task A",
        description: "Description",
        status: "todo",
        due_date: null,
        assignee_id: "123",
        assignee_name: "John Doe",
        assignee_avatar: null,
        tablos: {
          id: "tablo-1",
          name: "Projet Alpha",
          color: "bg-blue-500",
        },
      },
    ],
    isLoading: false,
  }),
  useUpdateTask: () => ({
    mutate: mutateUpdateTask,
  }),
  useDeleteTask: () => ({
    mutate: mutateDeleteTask,
  }),
}));

vi.mock("@xtablo/tablo-views", async (importOriginal) => {
  const actual = await importOriginal<typeof import("@xtablo/tablo-views")>();
  return {
    ...actual,
    GanttChart: () => <div data-testid="gantt-chart" />,
    TaskModal: () => null,
  };
});

describe("TasksPage", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("deletes a task from the inline kanban action", async () => {
    const user = userEvent.setup();

    renderWithProviders(<TasksPage />, {
      route: "/tasks?view=kanban",
      path: "/tasks",
    });

    await user.click(screen.getByRole("button", { name: "Supprimer la tâche Task A" }));

    expect(mutateDeleteTask).toHaveBeenCalledWith("task-1");
  });

  it("deletes a task from the inline list action", async () => {
    const user = userEvent.setup();

    renderWithProviders(<TasksPage />, {
      route: "/tasks?view=aggregated",
      path: "/tasks",
    });

    await user.click(screen.getByRole("button", { name: "Supprimer la tâche Task A" }));

    expect(mutateDeleteTask).toHaveBeenCalledWith("task-1");
  });
});
