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.PersonAutoReplyDto;
import com.kospo.talk.service.AutoReplyService;
import com.kospo.talk.service.PersonAutoReplyService;
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 = "PersonAutoReply Api", description = "개인 자동응답 관리")
@RequestMapping("/api")
@RequiredArgsConstructor
public class PersonAutoReplyController {
    private final PersonAutoReplyService personAutoReplyService;

    @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 =
                                    {PersonAutoReplyDto.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("/person/autoReply")
    public ResponseEntity findAll(
            @Parameter(description = "내용") @RequestParam(name="content", required = false) String content,
            Principal principal
    ) {
        return ResponseEntity.ok(personAutoReplyService.findByWorkAndSabunAndLikeContent(PersonAutoReplyDto.builder()
                        .sabun(principal.getName())
                        .content(content == null ? "" : "%"+content+"%")
                .build()));
    }

    @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("/person/autoReply")
    public ResponseEntity save(@RequestBody PersonAutoReplyDto personAutoReplyDto, Principal principal) {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        personAutoReplyDto.setSabun(user.getSabun());
        personAutoReplyService.save(personAutoReplyDto);
        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("/person/autoReply/{id}")
    public ResponseEntity delete(
            @Parameter(description = "아이디") @PathVariable("id") int id,
            Principal principal) {
        personAutoReplyService.delete(id);
        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("/person/autoReply")
    public ResponseEntity update(@RequestBody PersonAutoReplyDto personAutoReplyDto, Principal principal) {
        personAutoReplyService.update(personAutoReplyDto);
        return ResponseEntity.ok("success");
    }
}
