package kz.konturai.parser.controller;

import kz.konturai.parser.dto.ApiResponse;
import kz.konturai.parser.service.MarketItemService;
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 static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class SourceNamesEndpointTest {

    @Mock
    private MarketItemService marketItemService;

    @InjectMocks
    private MarketItemController marketItemController;

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

    @Test
    void testGetSources_Success() {
        // Given
        List<String> mockSources = List.of(
                "Kursiv (Бизнес/экономика)",
                "Kapital.kz (Бизнес)",
                "LSM.kz",
                "РБК",
                "Ведомости");
        when(marketItemService.getAllSourceNames()).thenReturn(mockSources);

        // When
        ResponseEntity<ApiResponse<List<String>>> response = marketItemController.getSources();

        // Then
        assertEquals(HttpStatus.OK, response.getStatusCode());
        assertNotNull(response.getBody());
        assertTrue(response.getBody().isSuccess());
        assertEquals(5, response.getBody().getData().size());
        assertTrue(response.getBody().getData().contains("Kursiv (Бизнес/экономика)"));
        assertTrue(response.getBody().getData().contains("Kapital.kz (Бизнес)"));
        assertTrue(response.getBody().getData().contains("LSM.kz"));
        assertTrue(response.getBody().getData().contains("РБК"));
        assertTrue(response.getBody().getData().contains("Ведомости"));
    }

    @Test
    void testGetSources_EmptyList() {
        // Given
        when(marketItemService.getAllSourceNames()).thenReturn(List.of());

        // When
        ResponseEntity<ApiResponse<List<String>>> response = marketItemController.getSources();

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

    @Test
    void testGetSources_ServiceError() {
        // Given
        when(marketItemService.getAllSourceNames()).thenThrow(new RuntimeException("Database error"));

        // When
        ResponseEntity<ApiResponse<List<String>>> response = marketItemController.getSources();

        // Then
        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
        assertNotNull(response.getBody());
        assertFalse(response.getBody().isSuccess());
        assertTrue(response.getBody().getMessage().contains("Ошибка при получении источников"));
    }
}
