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.when;

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

import org.junit.jupiter.api.DisplayName;
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.BoardController;
import com.demo1.board.api.dto.BoardRequest;
import com.demo1.board.api.dto.BoardResponse;
import com.demo1.board.domain.Board;
import com.demo1.board.service.BoardService;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

@ExtendWith(MockitoExtension.class)
class BoardControllerTest {

    @Mock
    private BoardService boardService;

    @InjectMocks
    private BoardController boardController;

    @Test
    @DisplayName("게시물 목록 호출 성공")
    void testGetBoards() {
        // Arrange
        when(boardService.getBoards()).thenReturn(Collections.singletonList(new Board()));

        // Act
        ResponseEntity<List<BoardResponse>> response = boardController.getBoards();

        // Assert
        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
        assertThat(response.getBody()).isNotNull();
        assertThat(response.getBody()).hasSize(1);
    }

    @Test
    @DisplayName("게시물 호출 성공")
    void testGetBoard() {
        // Arrange
        when(boardService.getBoard(anyLong())).thenReturn(new Board());

        // Act
        ResponseEntity<BoardResponse> response = boardController.getBoard(1L);

        // Assert
        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
        assertThat(response.getBody()).isNotNull();
    }

    @Test
    @DisplayName("게시물 생성 성공")
    void testCreateBoard() {
        // Arrange
        BoardRequest request = new BoardRequest();
        when(boardService.createBoard(request)).thenReturn(new Board());

        // Act
        ResponseEntity<BoardResponse> response = boardController.createBoard(request);

        // Assert
        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
        assertThat(response.getBody()).isNotNull();
    }

    @Test
    @DisplayName("게시물 수정 성공")
    void testUpdateBoard() {
        // Arrange
        BoardRequest request = new BoardRequest();
        when(boardService.updateBoard(anyLong(), any(BoardRequest.class))).thenReturn(new Board());

        // Act
        ResponseEntity<BoardResponse> response = boardController.updateBoard(1L, request);

        // Assert
        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
        assertThat(response.getBody()).isNotNull();
    }

    @Test
    @DisplayName("게시물 삭제 성공")
    void testDeleteBoard() {
        // Arrange
        when(boardService.deleteBoard(anyLong())).thenReturn(1L);

        // Act
        ResponseEntity<Long> response = boardController.deleteBoard(1L);

        // Assert
        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
        assertThat(response.getBody()).isEqualTo(1L);
    }
}

