import { render, screen, waitFor } from "@testing-library/react";
import { MemoryRouter } from "react-router-dom";
import { describe, expect, it, vi } from "vitest";
import { WeeklyView } from "../src/components/WeeklyView";

vi.mock("../src/lib/api", () => ({
  api: {
    reports: {
      weekly: vi.fn().mockResolvedValue({
        days: [
          { date: "2026-05-11", totalMinutes: 480, totalCostCents: 38400 },
          { date: "2026-05-12", totalMinutes: 0, totalCostCents: 0 },
          { date: "2026-05-13", totalMinutes: 0, totalCostCents: 0 },
          { date: "2026-05-14", totalMinutes: 0, totalCostCents: 0 },
          { date: "2026-05-15", totalMinutes: 240, totalCostCents: 19200 },
          { date: "2026-05-16", totalMinutes: 0, totalCostCents: 0 },
          { date: "2026-05-17", totalMinutes: 0, totalCostCents: 0 },
        ],
        weekTotalMinutes: 720,
        weekTotalCostCents: 57600,
      }),
    },
    timeEntries: { list: vi.fn().mockResolvedValue([]) },
  },
}));

vi.mock("../src/state/UserContext", () => ({
  useUser: () => ({
    selectedUserId: 1,
    selectedUser: null,
    users: [],
    setSelectedUserId: vi.fn(),
  }),
}));

describe("WeeklyView", () => {
  it("renders daily totals from report", async () => {
    render(
      <MemoryRouter>
        <WeeklyView />
      </MemoryRouter>,
    );

    await waitFor(() => {
      expect(screen.getByText("8:00")).toBeInTheDocument(); // 480 min
      expect(screen.getByText("4:00")).toBeInTheDocument(); // 240 min
    });
  });

  it("renders the week total", async () => {
    render(
      <MemoryRouter>
        <WeeklyView />
      </MemoryRouter>,
    );

    await waitFor(() => {
      // 720 min = 12:00, rendered as "Week total: 12:00"
      expect(screen.getByText(/Week total.*12:00/)).toBeInTheDocument();
    });
  });
});
