package cokr.xit.ens.modules.common.ctgy.sys.util.presentation;

import cokr.xit.ens.core.aop.EnsResponseVO;
import cokr.xit.ens.core.exception.EnsException;
import cokr.xit.ens.core.monitor.slack.support.ChannelGetter;
import cokr.xit.ens.core.monitor.slack.support.ImageSender;
import cokr.xit.ens.core.monitor.slack.support.MessageSender;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "SlackController")
@Slf4j
@RequiredArgsConstructor
@RestController
public class SlackController {

    @Value("${slack.token}")
    private String TOKEN;
    @Value("${slack.api.users}")
    private String API_USERS;
    @Value("${slack.api.chat}")
    private String API_CHAT;

    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
            @Content(mediaType = "application/json", examples = {
                    @ExampleObject(name = "Sample Example..."
                            , summary = "이미지 전송하기"
                            , value = "{\"email\":\"xxxxxx@gmail.com\",\"message\":\"안녕하세요. xxxx 입니다.\",\"img_url\":\"https://is5-ssl.mzstatic.com/image/thumb/Purple3/v4/d3/72/5c/d3725c8f-c642-5d69-1904-aa36e4297885/source/256x256bb.jpg\",\"pretext\":\"샘플이미지\"}"
                    )
            })
    })
    @Operation(summary = "Slack 이미지 전송")
    @PostMapping(value = "/sys/util/slack/send/photo", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EnsResponseVO> slackSendPhoto(@RequestBody Map<String, String> mParam) {
        try {
            ImageSender.builder()
                    .API_URL(API_CHAT)
                    .message(mParam.get("message"))
                    .imgUrl(mParam.get("img_url"))
                    .pretext(mParam.get("pretext"))
                    .build()
                    .execute(TOKEN, ChannelGetter.builder()
                            .TOKEN(TOKEN)
                            .API_URL(API_USERS)
                            .email(mParam.get("email"))
                            .build()
                            .execute());

            return new ResponseEntity<EnsResponseVO>(EnsResponseVO.okBuilder().build(), HttpStatus.OK);
        } catch (EnsException e) {
            return new ResponseEntity<EnsResponseVO>(EnsResponseVO.errBuilder().errCode(e.getErrCd()).errMsg(e.getMessage()).build(), HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }


    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
            @Content(mediaType = "application/json", examples = {
                    @ExampleObject(name = "Sample Example..."
                            , summary = "메시지 전송하기"
                            , value = "{\"email\":\"xxxxxx@gmail.com\",\"message\":\"안녕하세요. xxxx 입니다.\"}"
                    )
            })
    })
    @Operation(summary = "Slack 메시지 전송")
    @PostMapping(value = "/sys/util/slack/send/msg", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EnsResponseVO> slackSendMsg(@RequestBody Map<String, String> mParam) {
        try {
            MessageSender.builder()
                    .API_URL(API_CHAT)
                    .message(mParam.get("message"))
                    .build()
                    .execute(TOKEN, ChannelGetter.builder()
                            .TOKEN(TOKEN)
                            .API_URL(API_USERS)
                            .email(mParam.get("email"))
                            .build()
                            .execute());


            return new ResponseEntity<EnsResponseVO>(EnsResponseVO.okBuilder().build(), HttpStatus.OK);
        } catch (EnsException e) {
            return new ResponseEntity<EnsResponseVO>(EnsResponseVO.errBuilder().errCode(e.getErrCd()).errMsg(e.getMessage()).build(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "email"),
    })
    @Operation(summary = "Slack ID 조회")
    @GetMapping(value = "/sys/util/slack/ids/{email}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EnsResponseVO<String>> slackId(@PathVariable String email) {
        try {
            String slackInternalId = ChannelGetter.builder()
                    .TOKEN(TOKEN)
                    .API_URL(API_USERS)
                    .email(email)
                    .build()
                    .execute();

            return new ResponseEntity<EnsResponseVO<String>>(EnsResponseVO.<String>okBuilder().resultInfo(slackInternalId).build(), HttpStatus.OK);
        } catch (EnsException e) {
            return new ResponseEntity<EnsResponseVO<String>>(EnsResponseVO.<String>errBuilder().errCode(e.getErrCd()).errMsg(e.getMessage()).build(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = false, content = {
            @Content(mediaType = "application/json", examples = {
                    @ExampleObject(name = "Sample Example..."
                            , summary = "Unchecked Exception 오류 전송 테스트"
                            , value = "{}")
            })
    })
    @Operation(summary = "Unchecked Exception 오류 전송 테스트")
    @PostMapping(value = "/sys/util/slack/error", produces = MediaType.APPLICATION_JSON_VALUE)
    public void slackError() {
        throw new RuntimeException("실행 오류 테스트!!!");
    }
}