/**
 * Integration test for main page: calendar and header visible, lang from store.
 * Ported from webapp/js/main.test.js applyLangToUi.
 */

import { describe, it, expect, beforeEach, vi } from "vitest";
import { render, screen, waitFor } from "@testing-library/react";
import Page from "./page";
import { resetAppStore } from "@/test/test-utils";
import { useAppStore } from "@/store/app-store";

vi.mock("@/hooks/use-telegram-auth", () => ({
  useTelegramAuth: () => ({
    initDataRaw: "test-init",
    startParam: undefined,
    isLocalhost: true,
  }),
}));

vi.mock("@/hooks/use-month-data", () => ({
  useMonthData: () => ({
    retry: vi.fn(),
  }),
}));

describe("Page", () => {
  beforeEach(() => {
    resetAppStore();
  });

  it("renders calendar and header when store has default state", async () => {
    render(<Page />);
    expect(await screen.findByRole("grid", { name: "Calendar" })).toBeInTheDocument();
    expect(screen.getByRole("button", { name: /previous month/i })).toBeInTheDocument();
    expect(screen.getByRole("button", { name: /next month/i })).toBeInTheDocument();
  });

  it("sets document title and lang from store lang", async () => {
    useAppStore.getState().setLang("en");
    render(<Page />);
    await screen.findByRole("grid", { name: "Calendar" });
    expect(document.title).toBe("Duty Calendar");
    expect(document.documentElement.lang).toBe("en");
  });

  it("sets document title for ru when store lang is ru", async () => {
    (globalThis.window as unknown as { __DT_LANG?: string }).__DT_LANG = "ru";
    render(<Page />);
    await screen.findByRole("grid", { name: "Calendar" });
    await waitFor(() => {
      expect(document.title).toBe("Календарь дежурств");
    });
  });
});
