import { render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { TestUserStoreProvider, UserStoreProvider } from "./UserStoreProvider";

// Mock dependencies
vi.mock("@tanstack/react-query", () => ({
  useQuery: () => ({
    data: {
      id: "test-user-id",
      name: "Test User",
      streamToken: null,
    },
    isPending: false,
  }),
}));

vi.mock("@xtablo/shared/contexts/SessionContext", () => ({
  useSession: () => ({
    session: {
      access_token: "test-token",
    },
  }),
}));

vi.mock("../lib/api", () => ({
  api: {
    get: vi.fn(() =>
      Promise.resolve({
        data: {
          id: "test-user-id",
          name: "Test User",
          streamToken: null,
        },
      })
    ),
  },
}));

describe("UserStoreProvider", () => {
  it("renders children", () => {
    render(
      <UserStoreProvider>
        <div>Test Child</div>
      </UserStoreProvider>
    );
    expect(screen.getByText("Test Child")).toBeInTheDocument();
  });

  it("renders without crashing", () => {
    const { container } = render(
      <UserStoreProvider>
        <div>Content</div>
      </UserStoreProvider>
    );
    expect(container).toBeInTheDocument();
  });
});

describe("TestUserStoreProvider", () => {
  const mockUser = {
    id: "test-user-id",
    name: "Test User",
    streamToken: null,
    avatar_url: null,
    email: null,
    first_name: null,
    is_temporary: false,
    last_name: null,
    short_user_id: "short-id",
    last_signed_in: null,
    plan: "none" as const,
    created_at: new Date().toISOString(),
  };

  it("renders children with user", () => {
    render(
      <TestUserStoreProvider user={mockUser}>
        <div>Test Child</div>
      </TestUserStoreProvider>
    );
    expect(screen.getByText("Test Child")).toBeInTheDocument();
  });

  it("renders children without user", () => {
    render(
      <TestUserStoreProvider user={null}>
        <div>Test Child</div>
      </TestUserStoreProvider>
    );
    expect(screen.getByText("Test Child")).toBeInTheDocument();
  });

  it("renders without crashing", () => {
    const { container } = render(
      <TestUserStoreProvider user={mockUser}>
        <div>Content</div>
      </TestUserStoreProvider>
    );
    expect(container).toBeInTheDocument();
  });
});
