import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { renderHook, act } from "@testing-library/react";
import { useSetup } from "../useSetup";
import { useServerHealth } from "../useServerHealth";
import * as setupApi from "../../api/setup";
import * as configApi from "../../api/config";
import type { Fail2BanLogResponse, ServiceStatusResponse } from "../../types/config";

vi.mock("../../api/setup");
vi.mock("../../api/config");

const mockedGetSetupStatus = vi.mocked(setupApi.getSetupStatus);
const mockedFetchServiceStatus = vi.mocked(configApi.fetchServiceStatus);
const mockedFetchFail2BanLog = vi.mocked(configApi.fetchFail2BanLog);

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

  afterEach(() => {
    vi.useRealTimers();
  });

  it("loads setup status on mount", async () => {
    mockedGetSetupStatus.mockResolvedValue({ completed: true });

    const { result } = renderHook(() => useSetup());

    expect(result.current.loading).toBe(true);
    await act(async () => {
      await Promise.resolve();
    });

    expect(mockedGetSetupStatus).toHaveBeenCalledTimes(1);
    expect(result.current.status).toEqual({ completed: true });
    expect(result.current.loading).toBe(false);
    expect(result.current.error).toBeNull();
  });

  it("cancels stale setup fetches when refresh is called again", async () => {
    const firstResult = { completed: false };
    const secondResult = { completed: true };
    let callCount = 0;

    mockedGetSetupStatus.mockImplementation(async (signal?: AbortSignal) => {
      const response = callCount++ === 0 ? firstResult : secondResult;
      await new Promise((resolve) => setTimeout(resolve, 10));
      if (signal?.aborted) {
        throw new Error("Request aborted");
      }
      return response;
    });

    const { result } = renderHook(() => useSetup());

    await act(async () => {
      void result.current.refresh();
      void result.current.refresh();
      vi.advanceTimersByTime(20);
      await Promise.resolve();
    });

    expect(mockedGetSetupStatus).toHaveBeenCalledTimes(3);
    expect(result.current.status).toEqual(secondResult);
    expect(result.current.loading).toBe(false);
  });
});

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

  afterEach(() => {
    vi.useRealTimers();
  });

  it("updates state only for the latest refresh and aborts stale requests", async () => {
    const firstService = { running: false } as unknown as ServiceStatusResponse;
    const secondService = { running: true } as unknown as ServiceStatusResponse;
    const firstLog = { lines: [] } as unknown as Fail2BanLogResponse;
    const secondLog = { lines: ["ok"] } as unknown as Fail2BanLogResponse;
    let callCount = 0;

    mockedFetchServiceStatus.mockImplementation(async (signal?: AbortSignal) => {
      const response = callCount++ === 0 ? firstService : secondService;
      await new Promise((resolve) => setTimeout(resolve, 10));
      if (signal?.aborted) {
        throw new Error("Request aborted");
      }
      return response;
    });
    mockedFetchFail2BanLog.mockImplementation(async (_lines?: number, _filter?: string, signal?: AbortSignal) => {
      const response = callCount === 1 ? firstLog : secondLog;
      await new Promise((resolve) => setTimeout(resolve, 10));
      if (signal?.aborted) {
        throw new Error("Request aborted");
      }
      return response;
    });

    const { result } = renderHook(() => useServerHealth(10, "test"));

    await act(async () => {
      void result.current.refresh();
      void result.current.refresh();
      vi.advanceTimersByTime(20);
      await Promise.resolve();
    });

    expect(result.current.status).toEqual(secondService);
    expect(result.current.logData).toEqual(secondLog);
    expect(result.current.error).toBeNull();
    expect(mockedFetchServiceStatus).toHaveBeenCalledTimes(2);
    expect(mockedFetchFail2BanLog).toHaveBeenCalledTimes(2);
  });
});
