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/CabinetTab', () => ({
  CabinetTab: ({ householdId }: { householdId: string }) => (
    <div data-testid="cabinet-tab">{householdId}</div>
  ),
}));

import CabinetPage from '../../../../../src/app/(dashboard)/medicines/cabinet/page';

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

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

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