package com.kospo.talk.controller;

import com.kospo.talk.config.exception.CustomErrorResponse;
import com.kospo.talk.model.Question;
import com.kospo.talk.model.QuestionId;
import com.kospo.talk.model.User;
import com.kospo.talk.payload.QuestionDto;
import com.kospo.talk.service.QuestionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.time.LocalDateTime;

@RestController
@Tag(name = "Question Api", description = "질문 관리")
@RequestMapping("/api")
@RequiredArgsConstructor
public class QuestionController {
    private final QuestionService questionService;

    @Operation(summary = "질문 조회", description = "본인 업무 시스템 질문 조회")
    @ApiResponses({
            @ApiResponse(description = "Success", responseCode = "200",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json", array = @ArraySchema(
                            schema = @Schema(oneOf =
                                    {Question.class})
                    ))),
            @ApiResponse(description = "Not found", responseCode = "404",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "text/plain", schema = @Schema(oneOf =
                            {String.class}))),
            @ApiResponse(description = "Internal Error", responseCode = "500",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json", schema = @Schema(oneOf =
                            {CustomErrorResponse.class })))
    })
    @GetMapping("/question")
    public ResponseEntity questionList(
            Principal principal
    ) {
        return ResponseEntity.ok(questionService.findBySabun(principal.getName()));
    }

    @Operation(summary = "질문 조회", description = "업무별 질문 조회")
    @ApiResponses({
            @ApiResponse(description = "Success", responseCode = "200",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json", array = @ArraySchema(
                            schema = @Schema(oneOf =
                                    {Question.class})
                    ))),
            @ApiResponse(description = "Not found", responseCode = "404",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "text/plain", schema = @Schema(oneOf =
                            {String.class}))),
            @ApiResponse(description = "Internal Error", responseCode = "500",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json", schema = @Schema(oneOf =
                            {CustomErrorResponse.class })))
    })
    @GetMapping("/question/{workId}")
    public ResponseEntity chatList(
            @Parameter(description = "업무아이디") @PathVariable("workId") String workId,
            Principal principal
    ) {
        return ResponseEntity.ok(questionService.findByWorkId(workId));
    }

    @Operation(summary = "질문 저장", description = "유지보수 담당자 질문 데이터 저장")
    @ApiResponses({
            @ApiResponse(description = "Success", responseCode = "200",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @Schema(oneOf =
                                    {String.class})
                    )),
            @ApiResponse(description = "Not found", responseCode = "404",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "text/plain", schema = @Schema(oneOf =
                            {String.class}))),
            @ApiResponse(description = "Internal Error", responseCode = "500",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json", schema = @Schema(oneOf =
                            {CustomErrorResponse.class })))
    })
    @PostMapping("/question")
    public ResponseEntity save(
            @RequestBody QuestionDto questionDto
    ) {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        questionDto.setInsSabun(user.getSabun());
        questionDto.setInsName(user.getName());
        questionDto.setInsDate(LocalDateTime.now());
        questionService.save(questionDto);
        return ResponseEntity.ok("success");
    }

    @Operation(summary = "질문 수정", description = "유지보수 담당자 질문 데이터 수정")
    @ApiResponses({
            @ApiResponse(description = "Success", responseCode = "200",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @Schema(oneOf =
                                    {String.class})
                    )),
            @ApiResponse(description = "Not found", responseCode = "404",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "text/plain", schema = @Schema(oneOf =
                            {String.class}))),
            @ApiResponse(description = "Internal Error", responseCode = "500",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json", schema = @Schema(oneOf =
                            {CustomErrorResponse.class })))
    })
    @PutMapping("/question")
    public ResponseEntity update(
            @RequestBody QuestionDto questionDto,
            Principal principal
    ) {
        questionService.update(questionDto);
        return ResponseEntity.ok("success");
    }

    @Operation(summary = "질문 삭제", description = "유지보수 담당자 질문 데이터 삭제")
    @ApiResponses({
            @ApiResponse(description = "Success", responseCode = "200",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @Schema(oneOf =
                                    {String.class})
                    )),
            @ApiResponse(description = "Not found", responseCode = "404",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "text/plain", schema = @Schema(oneOf =
                            {String.class}))),
            @ApiResponse(description = "Internal Error", responseCode = "500",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json", schema = @Schema(oneOf =
                            {CustomErrorResponse.class })))
    })
    @DeleteMapping("/question/{workId}/{sort}")
    public ResponseEntity delete(
            @Parameter(description = "업무아이디") @PathVariable("workId") String workId,
            @Parameter(description = "순차") @PathVariable("sort") Integer sort,
            Principal principal
    ) {
        questionService.delete(QuestionId.builder()
                .workId(workId)
                .sort(sort)
                .build());
        return ResponseEntity.ok("success");
    }

}
