import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import type React from 'react';

const { mockUseApi } = vi.hoisted(() => ({ mockUseApi: vi.fn() }));

vi.mock('@/lib/useApi', () => ({ useApi: mockUseApi }));
vi.mock('next/link', () => ({
  default: (props: { href?: string; children: React.ReactNode }) => props.children,
}));
vi.mock('@/app/(dashboard)/medicines/LibraryTab', () => ({
  LibraryTab: ({ householdId }: { householdId: string }) => (
    <div data-testid="library-tab">{householdId}</div>
  ),
}));

import LibraryPage from '../../../../../src/app/(dashboard)/medicines/library/page';

describe('LibraryPage', () => {
  it('shows skeleton when loading', () => {
    mockUseApi.mockReturnValue({ householdId: null, isLoading: true });
    render(<LibraryPage />);
    expect(screen.getByText('Medicine Library')).toBeInTheDocument();
    expect(screen.queryByTestId('library-tab')).not.toBeInTheDocument();
  });

  it('shows household prompt when no householdId', () => {
    mockUseApi.mockReturnValue({ householdId: null, isLoading: false });
    render(<LibraryPage />);
    expect(screen.getByText(/create or join a household/)).toBeInTheDocument();
  });

  it('renders LibraryTab when householdId exists', () => {
    mockUseApi.mockReturnValue({ householdId: 'hh1', isLoading: false });
    render(<LibraryPage />);
    expect(screen.getByTestId('library-tab')).toHaveTextContent('hh1');
  });
});
