package com.demo1.unit;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

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 com.demo1.board.api.dto.BoardRequest;
import com.demo1.board.domain.Board;
import com.demo1.board.domain.BoardMapper;
import com.demo1.board.service.BoardServiceImpl;

@ExtendWith(MockitoExtension.class)
class BoardServiceTest {

    @Mock
    private BoardMapper boardMapper;

    @InjectMocks
    private BoardServiceImpl boardService;

    @Test
    void testGetBoards() {
        // Arrange
        when(boardMapper.getAllBoards()).thenReturn(Collections.singletonList(new Board()));

        // Act
        List<Board> boards = boardService.getBoards();

        // Assert
        assertThat(boards).isNotNull();
        assertThat(boards).hasSize(1);
    }

    @Test
    void testGetBoard() {
        // Arrange
        when(boardMapper.getBoardById(anyLong())).thenReturn(Optional.of(new Board()));

        // Act
        Board board = boardService.getBoard(1L);

        // Assert
        assertThat(board).isNotNull();
    }

    @Test
    void testCreateBoard() {
        // Arrange
        BoardRequest request = new BoardRequest("title","content");
        Board updatedBoard = new Board(1L,"title","content");
        when(boardMapper.getBoardById(null)).thenReturn(Optional.of(updatedBoard));

        // Act
        Board board = boardService.createBoard(request);

        // Assert
        assertThat(board).isEqualTo(updatedBoard);
        verify(boardMapper, times(1)).insertBoard(any(Board.class));
    }


    @Test
    void testUpdateBoard() {
        // Arrange
        BoardRequest request = new BoardRequest();
        Board updatedBoard = new Board();
        when(boardMapper.updateBoard(any(Board.class))).thenReturn(1);
        when(boardMapper.getBoardById(anyLong())).thenReturn(Optional.of(updatedBoard));

        // Act
        Board board = boardService.updateBoard(1L, request);

        // Assert
        assertThat(board).isEqualTo(updatedBoard);
        verify(boardMapper, times(1)).updateBoard(any(Board.class));
    }

    @Test
    void testDeleteBoard() {
        // Act
        Long deletedId = boardService.deleteBoard(1L);

        // Assert
        assertThat(deletedId).isEqualTo(1L);
        verify(boardMapper, times(1)).deleteBoard(1L); // Verify method call
    }
}
