import { describe, test, expect, vi } from 'vitest';
import { createContainer, createTestContainer } from '../container';
import { LEAGUE_SERVICE_TOKEN, LOGGER_TOKEN } from '../tokens';
import { ContainerProvider } from '../providers/ContainerProvider';
import { useInject } from '../hooks/useInject';
import { renderHook } from '@testing-library/react';

describe('DI System', () => {
  test('createContainer creates a container', () => {
    const container = createContainer();
    expect(container).toBeDefined();
  });

  test('container can resolve registered services', () => {
    const container = createContainer();
    const logger = container.get(LOGGER_TOKEN);
    expect(logger).toBeDefined();
  });

  test('createTestContainer allows mocking', async () => {
    const mockLeagueService = {
      getAllLeagues: vi.fn().mockResolvedValue([{ id: '1', name: 'Test League' }]),
    };

    const overrides = new Map([
      [LEAGUE_SERVICE_TOKEN, mockLeagueService],
    ]);

    const container = await createTestContainer(overrides);
    
    const service = container.get(LEAGUE_SERVICE_TOKEN);
    expect(service.getAllLeagues).toBeDefined();
  });

  test('useInject hook works with ContainerProvider', async () => {
    const mockLeagueService = {
      getAllLeagues: vi.fn().mockResolvedValue([{ id: '1', name: 'Test League' }]),
    };

    const overrides = new Map([
      [LEAGUE_SERVICE_TOKEN, mockLeagueService],
    ]);

    const container = await createTestContainer(overrides);

    const { result } = renderHook(() => useInject(LEAGUE_SERVICE_TOKEN), {
      wrapper: ({ children }) => (
        <ContainerProvider container={container}>
          {children}
        </ContainerProvider>
      ),
    });

    // The hook should return the service
    expect(result.current).toBeDefined();
  });
});