package kz.konturai.parser.controller;

import kz.konturai.parser.dto.ApiResponse;
import kz.konturai.parser.model.MarketItem;
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.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

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

@ExtendWith(MockitoExtension.class)
class MarketItemControllerTest {

    @Mock
    private MarketItemService marketItemService;

    @InjectMocks
    private MarketItemController marketItemController;

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

    @Test
    void testGetItems_Success() {
        // Given
        List<MarketItem> mockItems = new ArrayList<>();
        MarketItem item = new MarketItem();
        item.setTitle("Test News");
        mockItems.add(item);

        Page<MarketItem> mockPage = new PageImpl<>(mockItems);
        when(marketItemService.parseDate(any())).thenReturn(null);
        when(marketItemService.getAllItems(any())).thenReturn(mockPage);

        // When
        ResponseEntity<ApiResponse<Page<MarketItem>>> response = marketItemController.getItems(null, null, null,
                PageRequest.of(0, 20));

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

    @Test
    void testGetStats_Success() {
        // Given
        when(marketItemService.getTotalItemsCount()).thenReturn(100L);
        when(marketItemService.getSourceStatistics()).thenReturn(java.util.Map.of("test", 50L));

        // When
        ResponseEntity<ApiResponse<kz.konturai.parser.dto.ParserStatsDto>> response = marketItemController.getStats();

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

    @Test
    void testGetSources_Success() {
        // Given
        List<String> mockSources = List.of("kursiv", "kapital", "lsm");
        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(3, response.getBody().getData().size());
    }
}
