package kz.konturai.parser.controller;

import kz.konturai.parser.dto.ApiResponse;
import kz.konturai.parser.dto.HealthCheckDto;
import kz.konturai.parser.service.MarketItemService;
import kz.konturai.parser.service.ParserManagerService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class HealthCheckControllerTest {

    @Mock
    private MarketItemService marketItemService;

    @Mock
    private ParserManagerService parserManagerService;

    @InjectMocks
    private HealthCheckController healthCheckController;

    @BeforeEach
    void setUp() {
        // Setup common test data
    }

    @Test
    void testHealthCheck_Success() {
        // When
        ResponseEntity<ApiResponse<HealthCheckDto>> response = healthCheckController.healthCheck();

        // Then
        assertEquals(HttpStatus.OK, response.getStatusCode());
        assertNotNull(response.getBody());
        assertTrue(response.getBody().isSuccess());
        assertEquals("UP", response.getBody().getData().getStatus());
    }

    @Test
    void testCheckMongoConnection_Success() {
        // Given
        when(marketItemService.getTotalItemsCount()).thenReturn(100L);

        // When
        ResponseEntity<ApiResponse<HealthCheckDto>> response = healthCheckController.checkMongoConnection();

        // Then
        assertEquals(HttpStatus.OK, response.getStatusCode());
        assertNotNull(response.getBody());
        assertTrue(response.getBody().isSuccess());
        assertEquals("UP", response.getBody().getData().getStatus());
    }

    @Test
    void testCheckMongoConnection_Error() {
        // Given
        when(marketItemService.getTotalItemsCount()).thenThrow(new RuntimeException("Connection failed"));

        // When
        ResponseEntity<ApiResponse<HealthCheckDto>> response = healthCheckController.checkMongoConnection();

        // Then
        assertEquals(HttpStatus.SERVICE_UNAVAILABLE, response.getStatusCode());
        assertNotNull(response.getBody());
        assertFalse(response.getBody().isSuccess());
        assertEquals("DOWN", response.getBody().getData().getStatus());
    }

    @Test
    void testGetSchedulerInfo_Success() {
        // Given
        List<String> mockParsers = List.of("kursiv", "kapital", "lsm");
        when(parserManagerService.getAvailableParsers()).thenReturn(mockParsers);
        when(parserManagerService.getParserCount()).thenReturn(3);

        // When
        ResponseEntity<ApiResponse<Map<String, Object>>> response = healthCheckController.getSchedulerInfo();

        // Then
        assertEquals(HttpStatus.OK, response.getStatusCode());
        assertNotNull(response.getBody());
        assertTrue(response.getBody().isSuccess());
        assertTrue(response.getBody().getData().containsKey("schedulerEnabled"));
        assertTrue(response.getBody().getData().containsKey("activeParsers"));
    }

    @Test
    void testGetSystemInfo_Success() {
        // Given
        List<String> mockParsers = List.of("kursiv", "kapital");
        when(parserManagerService.getAvailableParsers()).thenReturn(mockParsers);
        when(parserManagerService.getParserCount()).thenReturn(2);
        when(marketItemService.getTotalItemsCount()).thenReturn(50L);
        when(marketItemService.getSourceStatistics()).thenReturn(Map.of("test", 25L));

        // When
        ResponseEntity<ApiResponse<Map<String, Object>>> response = healthCheckController.getSystemInfo();

        // Then
        assertEquals(HttpStatus.OK, response.getStatusCode());
        assertNotNull(response.getBody());
        assertTrue(response.getBody().isSuccess());
        assertTrue(response.getBody().getData().containsKey("parsers"));
        assertTrue(response.getBody().getData().containsKey("data"));
        assertTrue(response.getBody().getData().containsKey("system"));
    }

    @Test
    void testCheckParsersHealth_Success() {
        // Given
        List<String> mockParsers = List.of("kursiv", "kapital", "lsm");
        when(parserManagerService.getAvailableParsers()).thenReturn(mockParsers);

        // When
        ResponseEntity<ApiResponse<Map<String, Object>>> response = healthCheckController.checkParsersHealth();

        // Then
        assertEquals(HttpStatus.OK, response.getStatusCode());
        assertNotNull(response.getBody());
        assertTrue(response.getBody().isSuccess());
        assertEquals("UP", response.getBody().getData().get("status"));
        assertEquals(3, response.getBody().getData().get("totalParsers"));
    }
}
