package com.kospo.talk.controller;

import com.kospo.talk.config.exception.CustomErrorResponse;
import com.kospo.talk.model.User;
import com.kospo.talk.payload.AutoReplyDto;
import com.kospo.talk.payload.ManualDto;
import com.kospo.talk.service.AutoReplyService;
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 = "AutoReply Api", description = "자동응답 관리")
@RequestMapping("/api")
@RequiredArgsConstructor
public class AutoReplyController {
    private final AutoReplyService autoReplyService;

    @Operation(summary = "자동응답 조회", description = "본인의 work id 로 해당 자동응답 조회")
    @ApiResponses({
            @ApiResponse(description = "Success", responseCode = "200",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json", array = @ArraySchema(
                            schema = @Schema(oneOf =
                                    {AutoReplyDto.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("/autoReply")
    public ResponseEntity findAll(
            @Parameter(description = "검색") @RequestParam(name="workId", required = false) String workId,
            @Parameter(description = "타입: (0010 : 방생성 메시지(온라인), 0020 : 방생성 메시지 (오프라인), 0300: 시작하기 메시지)") @RequestParam(name="type", required = false) String type,
            Principal principal
    ) {
        return ResponseEntity.ok(autoReplyService.findByWorkIdAndType(workId, type));
    }


    @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 =
                                    {AutoReplyDto.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("/autoReply/person")
    public ResponseEntity findBySabun(
            @Parameter(description = "검색") @RequestParam(name="workId", required = false) String workId,
            @Parameter(description = "타입: (0010 : 방생성 메시지(온라인), 0020 : 방생성 메시지 (오프라인), 0300: 시작하기 메시지)") @RequestParam(name="type", required = false) String type,
            Principal principal
    ) {
        return ResponseEntity.ok(autoReplyService.findBySabun(principal.getName()));
    }

    @Operation(summary = "자동응답 저장", description = "자동응답 저장")
    @ApiResponses({
            @ApiResponse(description = "Success", responseCode = "200",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "text/plain", 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("/autoReply")
    public ResponseEntity save(@RequestBody AutoReplyDto autoReplyDto, Principal principal) {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        autoReplyDto.setInsSabun(user.getSabun());
        autoReplyDto.setInsName(user.getName());
        autoReplyDto.setInsDate(LocalDateTime.now());
        autoReplyService.save(autoReplyDto);
        return ResponseEntity.ok("success");
    }

    @Operation(summary = "자동응답 수정", description = "자동응답 수정")
    @ApiResponses({
            @ApiResponse(description = "Success", responseCode = "200",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "text/plain", 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("/autoReply")
    public ResponseEntity update(@RequestBody AutoReplyDto autoReplyDto, Principal principal) {
        autoReplyService.update(autoReplyDto, principal.getName());
        return ResponseEntity.ok("success");
    }

    @Operation(summary = "자동응답 삭제", description = "자동응답 삭제")
    @ApiResponses({
            @ApiResponse(description = "Success", responseCode = "200",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "text/plain", 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("/autoReply/{workId}/{type}")
    public ResponseEntity delete(
            @Parameter(description = "업무아이디") @PathVariable String workId,
            @Parameter(description = "타입") @PathVariable String type,
            Principal principal) {
        autoReplyService.delete(AutoReplyDto.builder()
                .workId(workId).type(type).build(), principal.getName());
        return ResponseEntity.ok("success");
    }
}
