import { jest } from "@jest/globals";
import { renderHook, act } from "@testing-library/react";
import useCountdown from "../components/useCountdown.js";

describe("useCountdown Hook", () => {
  beforeEach(() => {
    jest.useFakeTimers();
  });

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

  it("should initialize with the start value", () => {
    const { result } = renderHook(() => useCountdown(10));
    expect(result.current.count).toBe(10);
    expect(result.current.isRunning).toBe(false);
  });

  it("should start countdown when startCountdown is called", () => {
    const { result } = renderHook(() => useCountdown(10, 1000));

    act(() => {
      result.current.startCountdown();
    });

    expect(result.current.isRunning).toBe(true);
  });

  it("should decrement count when running", () => {
    const { result } = renderHook(() => useCountdown(10, 1000));

    act(() => {
      result.current.startCountdown();
    });

    act(() => {
      jest.advanceTimersByTime(1000);
    });

    expect(result.current.count).toBe(9);
  });

  it("should stop countdown when stopCountdown is called", () => {
    const { result } = renderHook(() => useCountdown(10, 1000));

    act(() => {
      result.current.startCountdown();
    });

    act(() => {
      jest.advanceTimersByTime(1000);
    });

    act(() => {
      result.current.stopCountdown();
    });

    expect(result.current.isRunning).toBe(false);

    const countAfterStop = result.current.count;

    act(() => {
      jest.advanceTimersByTime(2000);
    });

    expect(result.current.count).toBe(countAfterStop);
  });

  it("should reset countdown when resetCountdown is called", () => {
    const { result } = renderHook(() => useCountdown(10, 1000));

    act(() => {
      result.current.startCountdown();
    });

    act(() => {
      jest.advanceTimersByTime(3000);
    });

    expect(result.current.count).toBe(7);

    act(() => {
      result.current.resetCountdown();
    });

    expect(result.current.count).toBe(10);
    expect(result.current.isRunning).toBe(false);
  });

  it("should restart from start value when count reaches 1", () => {
    const { result } = renderHook(() => useCountdown(3, 1000));

    act(() => {
      result.current.startCountdown();
    });

    // Count down to 1
    act(() => {
      jest.advanceTimersByTime(2000);
    });
    expect(result.current.count).toBe(1);

    // Next tick should restart
    act(() => {
      jest.advanceTimersByTime(1000);
    });
    expect(result.current.count).toBe(3);
  });

  it("should use custom interval", () => {
    const { result } = renderHook(() => useCountdown(5, 500));

    act(() => {
      result.current.startCountdown();
    });

    act(() => {
      jest.advanceTimersByTime(500);
    });

    expect(result.current.count).toBe(4);
  });

  it("should clear interval on unmount", () => {
    const { result, unmount } = renderHook(() => useCountdown(10, 1000));

    act(() => {
      result.current.startCountdown();
    });

    unmount();

    // No errors should occur
    expect(true).toBe(true);
  });
});
