package kz.konturai.parser.controller;

import kz.konturai.parser.dto.ApiResponse;
import kz.konturai.parser.dto.ParserResultDto;
import kz.konturai.parser.model.MarketItem;
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.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

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

@ExtendWith(MockitoExtension.class)
class ParserAdminControllerTest {

    @Mock
    private ParserManagerService parserManagerService;

    @Mock
    private MarketItemService marketItemService;

    @InjectMocks
    private ParserAdminController parserAdminController;

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

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

        when(parserManagerService.hasParser(sourceName)).thenReturn(true);
        when(parserManagerService.runParser(sourceName)).thenReturn(mockItems);
        when(marketItemService.getTotalItemsCount()).thenReturn(100L);

        // When
        ResponseEntity<ApiResponse<ParserResultDto>> response = parserAdminController.parseSource(sourceName);

        // Then
        assertEquals(HttpStatus.OK, response.getStatusCode());
        assertNotNull(response.getBody());
        assertTrue(response.getBody().isSuccess());
        assertEquals(sourceName, response.getBody().getData().getSource());
        assertEquals(1, response.getBody().getData().getProcessedItems());
    }

    @Test
    void testParseSource_ParserNotFound() {
        // Given
        String sourceName = "nonexistent";
        when(parserManagerService.hasParser(sourceName)).thenReturn(false);

        // When
        ResponseEntity<ApiResponse<ParserResultDto>> response = parserAdminController.parseSource(sourceName);

        // Then
        assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
        assertNotNull(response.getBody());
        assertFalse(response.getBody().isSuccess());
        assertTrue(response.getBody().getMessage().contains("Парсер не найден"));
    }

    @Test
    void testParseAll_Success() {
        // Given
        List<ParserResultDto> mockResults = new ArrayList<>();
        ParserResultDto result = new ParserResultDto("kursiv", 5, 100, "completed");
        mockResults.add(result);

        when(parserManagerService.runAllParsers()).thenReturn(CompletableFuture.completedFuture(mockResults));

        // When
        ResponseEntity<ApiResponse<List<ParserResultDto>>> response = parserAdminController.parseAll();

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

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

        // When
        ResponseEntity<ApiResponse<List<String>>> response = parserAdminController.getAvailableParsers();

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

    @Test
    void testCheckParserExists_Success() {
        // Given
        String sourceName = "kursiv";
        when(parserManagerService.hasParser(sourceName)).thenReturn(true);

        // When
        ResponseEntity<ApiResponse<Boolean>> response = parserAdminController.checkParserExists(sourceName);

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