package com.kospo.talk.controller;

import com.kospo.talk.config.exception.CustomErrorResponse;
import com.kospo.talk.config.utils.CommonUtils;
import com.kospo.talk.config.utils.CryptoUtil;
import com.kospo.talk.model.User;
import com.kospo.talk.payload.ListeningDto;
import com.kospo.talk.payload.MasterDto;
import com.kospo.talk.service.UserService;
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.web.bind.annotation.*;

import java.security.Principal;

@RestController
@Tag(name = "User Api", description = "사용자 관리")
@RequestMapping("/api/user")
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;

    @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 =
                                    {User.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("/info")
    public ResponseEntity userInfo(
//            @Parameter(description = "사번") @Htt(value="sabun", required = false) String sabun,
            Principal principal) {
//        System.out.println(principal.getName());
        return ResponseEntity.ok(userService.findByUserid(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 =
                                    {User.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("/info/{sabun}")
    public ResponseEntity userInfo(
            @Parameter(description = "사번") @PathVariable(value="sabun", required = false) String sabun,
            Principal principal) {
        return ResponseEntity.ok(userService.findByUserid(sabun));
    }

    @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 =
                                    {User.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("/info/sm")
    public ResponseEntity smUserInfo(
            Principal principal) {
        return ResponseEntity.ok(userService.findSmByUserid(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 =
                                    {User.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("/info/client")
    public ResponseEntity clientUserInfo(
            Principal principal) {
        return ResponseEntity.ok(userService.findClientByUserid(principal.getName()));
    }

    @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 })))
    })
    @GetMapping("/info/enc")
    public ResponseEntity encryptSabun(
            @RequestParam("sabun") String sabun,
            Principal principal) throws Exception {
        String encSabun = CryptoUtil.encrypt(sabun);
        return ResponseEntity.ok(encSabun);
    }

    @Operation(summary = "메시지 공통 상태 조회", description = "메시지 공통 상태 조회")
    @ApiResponses({
            @ApiResponse(description = "Success", responseCode = "200",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "text/plain", schema = @Schema(oneOf =
                            {Integer.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(value = "/listening")
    public ResponseEntity getListening(
            @RequestParam("talkId") String talkId,
            Principal principal
    ) {

        return ResponseEntity.ok(CommonUtils.getListening(
                ListeningDto.builder()
                        .sabun(principal.getName())
                        .talkId(talkId).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(value = "/listening")
    public ResponseEntity setListening(
            @RequestBody ListeningDto listeningDto,
            Principal principal
    ) {

        listeningDto.setSabun(principal.getName());
        CommonUtils.setListening(listeningDto);
        return ResponseEntity.ok("success");
    }

}
