import axios, { AxiosInstance } from 'axios';
import { WatchtowerSDK } from '../../index';
import { HealthResponse } from '../../endpoints/health/types';

// Mock axios
jest.mock('axios');
const mockedAxios = axios as jest.Mocked<typeof axios>;

describe('HealthEndpoint', () => {
  const baseURL = 'https://watchtower-go-supabase-822142465400.europe-west1.run.app';
  let sdk: WatchtowerSDK;
  let mockAxiosInstance: jest.Mocked<AxiosInstance>;

  beforeEach(() => {
    // Clear all mocks before each test
    jest.clearAllMocks();
    
    // Create mock axios instance
    mockAxiosInstance = {
      get: jest.fn(),
      post: jest.fn(),
      put: jest.fn(),
      delete: jest.fn(),
    } as unknown as jest.Mocked<AxiosInstance>;

    // Mock axios.create to return our mock instance
    mockedAxios.create.mockReturnValue(mockAxiosInstance);
    
    // Create a new SDK instance for each test
    sdk = new WatchtowerSDK(baseURL);
  });

  describe('check', () => {
    const mockHealthResponse: HealthResponse = {
      status: 'ok',
      supabase: 'connected',
      timestamp: '2025-05-13T18:14:46Z'
    };

    it('should return health status', async () => {
      // Mock the axios get method
      mockAxiosInstance.get.mockResolvedValue({ data: mockHealthResponse });

      const response = await sdk.health.check();

      // Verify the response
      expect(response).toEqual(mockHealthResponse);
      expect(response.status).toBe('ok');
      expect(response.supabase).toBe('connected');
      expect(response.timestamp).toBeDefined();
    });

    it('should handle error responses', async () => {
      // Mock the axios get method to throw an error
      mockAxiosInstance.get.mockRejectedValue(new Error('Network error'));

      // Verify that the error is thrown
      await expect(sdk.health.check()).rejects.toThrow('Network error');
    });

    it('should make request to correct endpoint', async () => {
      mockAxiosInstance.get.mockResolvedValue({ data: mockHealthResponse });

      await sdk.health.check();

      // Verify that the request was made to the correct endpoint
      expect(mockAxiosInstance.get).toHaveBeenCalledWith('/health', undefined);
    });
  });
}); 