import { describe, expect, it, vi } from "vitest";
import { renderWithProviders } from "../utils/testHelpers";
import { OAuthSigninPage } from "./oauth-signin";

const mockSignUpToStream = vi.fn();

vi.mock("@xtablo/shared/hooks/auth", () => ({
  useSignUpToStream: () => ({
    signUpToStream: mockSignUpToStream,
  }),
}));

vi.mock("../lib/api", () => ({
  api: {},
}));

describe("OAuthSigninPage", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    localStorage.clear();
    vi.useFakeTimers();
  });

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

  it("renders without crashing", () => {
    const { container } = renderWithProviders(<OAuthSigninPage />);
    expect(container).toBeInTheDocument();
  });

  it.skip("renders empty component", () => {
    const { container } = renderWithProviders(<OAuthSigninPage />);
    expect(container.firstChild).toBeEmptyDOMElement();
  });

  it.skip("navigates to home when session exists without redirectUrl", () => {
    renderWithProviders(<OAuthSigninPage />);

    vi.advanceTimersByTime(150);

    expect(mockSignUpToStream).toHaveBeenCalled();
  });

  it.skip("navigates to redirectUrl when session exists with redirectUrl", () => {
    localStorage.setItem("redirectUrl", "/dashboard");
    renderWithProviders(<OAuthSigninPage />);

    vi.advanceTimersByTime(150);

    expect(mockSignUpToStream).toHaveBeenCalled();
    expect(localStorage.getItem("redirectUrl")).toBeNull();
  });

  it.skip("decodes redirectUrl before navigation", () => {
    localStorage.setItem("redirectUrl", "%2Fdashboard%2Ftest");
    renderWithProviders(<OAuthSigninPage />);

    vi.advanceTimersByTime(150);
  });

  it("signs up to stream with access token", () => {
    renderWithProviders(<OAuthSigninPage />);

    vi.advanceTimersByTime(150);

    expect(mockSignUpToStream).toHaveBeenCalled();
  });

  it("clears interval on unmount", () => {
    const { unmount } = renderWithProviders(<OAuthSigninPage />);

    unmount();

    // If interval isn't cleared, this would cause issues
    vi.advanceTimersByTime(150);
  });
});
