import { renderHook } from '@testing-library/react-native';

import { usePlayer } from '@/features/voice/hooks/use-player';

const mockUseAudioPlayer = jest.fn();
const mockUseAudioPlayerStatus = jest.fn();

jest.mock('expo-audio', () => ({
  useAudioPlayer: (...args: unknown[]) => mockUseAudioPlayer(...args),
  useAudioPlayerStatus: (...args: unknown[]) =>
    mockUseAudioPlayerStatus(...args),
}));

jest.mock('@/core/audio/audio-focus', () => ({
  audioFocus: {
    acquireForPlayback: jest.fn(),
    releaseIfOwnedBy: jest.fn(),
    onOwnerChange: jest.fn(() => jest.fn()),
  },
}));

describe('usePlayer', () => {
  beforeEach(() => {
    mockUseAudioPlayer.mockReset();
    mockUseAudioPlayerStatus.mockReset();

    mockUseAudioPlayer.mockReturnValue({
      pause: jest.fn(),
      play: jest.fn(),
    });
    mockUseAudioPlayerStatus.mockReturnValue({
      isLoaded: false,
      playing: false,
      currentTime: 0,
      duration: 0,
    });
  });

  test('keeps the native audio session active while app-level audio focus owns teardown', () => {
    renderHook(() => usePlayer());

    expect(mockUseAudioPlayer).toHaveBeenCalledWith(
      null,
      expect.objectContaining({
        downloadFirst: false,
        keepAudioSessionActive: true,
      }),
    );
  });
});
