/**
 * Unit tests for CurrentDutyView: no-duty message, duty card with contacts.
 * Ported from webapp/js/currentDuty.test.js renderCurrentDutyContent / showCurrentDutyView.
 */

import { describe, it, expect, beforeEach, vi } from "vitest";
import { render, screen, fireEvent } from "@testing-library/react";
import { CurrentDutyView } from "./CurrentDutyView";
import { TooltipProvider } from "@/components/ui/tooltip";
import { resetAppStore } from "@/test/test-utils";

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

vi.mock("@/lib/api", () => ({
  fetchDuties: vi.fn().mockResolvedValue([]),
  AccessDeniedError: class AccessDeniedError extends Error {
    serverDetail?: string;
    constructor(m: string, d?: string) {
      super(m);
      this.serverDetail = d;
    }
  },
}));

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

  it("shows loading then no-duty message when no active duty", async () => {
    const onBack = vi.fn();
    render(<CurrentDutyView onBack={onBack} />);
    await screen.findByText(/No one is on duty|Сейчас никто не дежурит/i, {}, { timeout: 3000 });
    expect(screen.getByText(/Back to calendar|Назад к календарю/i)).toBeInTheDocument();
  });

  it("back button calls onBack when clicked", async () => {
    const onBack = vi.fn();
    render(<CurrentDutyView onBack={onBack} />);
    await screen.findByText(/No one is on duty|Сейчас никто не дежурит/i, {}, { timeout: 3000 });
    const buttons = screen.getAllByRole("button", { name: /Back to calendar|Назад к календарю/i });
    fireEvent.click(buttons[buttons.length - 1]);
    expect(onBack).toHaveBeenCalled();
  });

  it("shows Close button when openedFromPin is true", async () => {
    const onBack = vi.fn();
    render(<CurrentDutyView onBack={onBack} openedFromPin={true} />);
    await screen.findByText(/No one is on duty|Сейчас никто не дежурит/i, {}, { timeout: 3000 });
    expect(screen.getByRole("button", { name: /Close|Закрыть/i })).toBeInTheDocument();
    expect(screen.queryByText(/Back to calendar|Назад к календарю/i)).not.toBeInTheDocument();
  });

  it("shows Open calendar button in no-duty view and it calls onBack", async () => {
    const onBack = vi.fn();
    render(<CurrentDutyView onBack={onBack} />);
    await screen.findByText(/No one is on duty|Сейчас никто не дежурит/i, {}, { timeout: 3000 });
    const openCalendarBtn = screen.getByRole("button", {
      name: /Open calendar|Открыть календарь/i,
    });
    expect(openCalendarBtn).toBeInTheDocument();
    fireEvent.click(openCalendarBtn);
    expect(onBack).toHaveBeenCalled();
  });

  it("shows contact info not set when duty has no phone or username", async () => {
    const { fetchDuties } = await import("@/lib/api");
    const now = new Date();
    const start = new Date(now.getTime() - 60 * 60 * 1000); // 1 hour ago
    const end = new Date(now.getTime() + 60 * 60 * 1000); // 1 hour from now
    const dutyNoContacts = {
      id: 1,
      user_id: 1,
      start_at: start.toISOString(),
      end_at: end.toISOString(),
      event_type: "duty" as const,
      full_name: "Test User",
      phone: null,
      username: null,
    };
    vi.mocked(fetchDuties).mockResolvedValue([dutyNoContacts]);
    const onBack = vi.fn();
    render(<CurrentDutyView onBack={onBack} />);
    await screen.findByText("Test User", {}, { timeout: 3000 });
    expect(
      screen.getByText(/Contact info not set|Контактные данные не указаны/i)
    ).toBeInTheDocument();
    vi.mocked(fetchDuties).mockResolvedValue([]);
  });

  it("shows ends_at line when duty is active", async () => {
    const { fetchDuties } = await import("@/lib/api");
    const now = new Date();
    const start = new Date(now.getTime() - 60 * 60 * 1000);
    const end = new Date(now.getTime() + 2 * 60 * 60 * 1000);
    const duty = {
      id: 1,
      user_id: 1,
      start_at: start.toISOString(),
      end_at: end.toISOString(),
      event_type: "duty" as const,
      full_name: "Test User",
      phone: null,
      username: null,
    };
    vi.mocked(fetchDuties).mockResolvedValue([duty]);
    render(<CurrentDutyView onBack={vi.fn()} />);
    await screen.findByText("Test User", {}, { timeout: 3000 });
    expect(
      screen.getByText(/Until end of shift at|До конца смены в/i)
    ).toBeInTheDocument();
    vi.mocked(fetchDuties).mockResolvedValue([]);
  });

  it("error state shows Retry as first button", async () => {
    const { fetchDuties } = await import("@/lib/api");
    vi.mocked(fetchDuties).mockRejectedValue(new Error("Network error"));
    render(<CurrentDutyView onBack={vi.fn()} />);
    await screen.findByText(/Could not load|Не удалось загрузить/i, {}, { timeout: 3000 });
    const buttons = screen.getAllByRole("button");
    expect(buttons[0]).toHaveAccessibleName(/Retry|Повторить/i);
    vi.mocked(fetchDuties).mockResolvedValue([]);
  });

  it("403 shows AccessDeniedScreen with Back button and no Retry", async () => {
    const { fetchDuties, AccessDeniedError } = await import("@/lib/api");
    vi.mocked(fetchDuties).mockRejectedValue(
      new AccessDeniedError("ACCESS_DENIED", "Custom 403 message")
    );
    const onBack = vi.fn();
    render(<CurrentDutyView onBack={onBack} />);
    await screen.findByText(/Access denied|Доступ запрещён/i, {}, { timeout: 3000 });
    expect(
      screen.getByText(/Open the app again from Telegram|Откройте приложение снова из Telegram/i)
    ).toBeInTheDocument();
    expect(screen.getByText("Custom 403 message")).toBeInTheDocument();
    expect(
      screen.getByRole("button", { name: /Back to calendar|Назад к календарю/i })
    ).toBeInTheDocument();
    expect(screen.queryByRole("button", { name: /Retry|Повторить/i })).not.toBeInTheDocument();
    fireEvent.click(screen.getByRole("button", { name: /Back to calendar|Назад к календарю/i }));
    expect(onBack).toHaveBeenCalled();
    vi.mocked(fetchDuties).mockResolvedValue([]);
  });

  it("shows copy phone and copy Telegram buttons when duty has contacts", async () => {
    const { fetchDuties } = await import("@/lib/api");
    const now = new Date();
    const start = new Date(now.getTime() - 60 * 60 * 1000);
    const end = new Date(now.getTime() + 60 * 60 * 1000);
    const dutyWithContacts = {
      id: 1,
      user_id: 1,
      start_at: start.toISOString(),
      end_at: end.toISOString(),
      event_type: "duty" as const,
      full_name: "Test User",
      phone: "+79991234567",
      username: "testuser",
    };
    vi.mocked(fetchDuties).mockResolvedValue([dutyWithContacts]);
    render(
      <TooltipProvider>
        <CurrentDutyView onBack={vi.fn()} />
      </TooltipProvider>
    );
    await screen.findByText("Test User", {}, { timeout: 3000 });
    expect(
      screen.getByRole("button", { name: /Copy phone number|Скопировать номер/i })
    ).toBeInTheDocument();
    expect(
      screen.getByRole("button", {
        name: /Copy Telegram username|Скопировать логин Telegram/i,
      })
    ).toBeInTheDocument();
    vi.mocked(fetchDuties).mockResolvedValue([]);
  });
});
