package com.kospo.talk.controller;

import com.kospo.talk.config.exception.CustomErrorResponse;
import com.kospo.talk.payload.WorkDto;
import com.kospo.talk.payload.WorkLogoDto;
import com.kospo.talk.service.WorkService;
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.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Date;

@RestController
@Tag(name = "Work Api", description = "업무 관리")
@RequestMapping("/api")
@RequiredArgsConstructor
public class WorkController {
    private final WorkService workService;

    @Operation(summary = "업무 조회", description = "사이트 URL 로 담당자 조회")
    @ApiResponses({
            @ApiResponse(description = "Success", responseCode = "200",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json", array = @ArraySchema(
                            schema = @Schema(oneOf =
                                    {WorkDto.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("/work")
    @Transactional(readOnly = true)
    public ResponseEntity findByWorkURl(
            @Parameter(description = "사이트url") @RequestParam(value="url", defaultValue = "") String url,
            @Parameter(description = "업무코드") @RequestParam(value="workId", defaultValue = "") String workId,
            Principal principal
    ) {
        return ResponseEntity.ok(workService.findByWorkUrl(url, workId, 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 =
                                    {WorkDto.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("/work/dept")
    @Transactional(readOnly = true)
    public ResponseEntity findByWorkCategory(
            @Parameter(description = "상위코드") @RequestParam(value="pWorkId", defaultValue = "") String pWorkId,
            Principal principal
    ) {
        return ResponseEntity.ok(workService.findByWorkDepth(pWorkId));
    }

    @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 =
                                    {WorkDto.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("/work/search")
    @Transactional(readOnly = true)
    public ResponseEntity findByWorkSearch(
            @Parameter(description = "업무명&담당자명") @RequestParam(value="name", defaultValue = "") String name,
            Principal principal
    ) {
        return ResponseEntity.ok(workService.findByWorkSearech(name));
    }

    @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 =
                                    {WorkDto.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("/work/dept/all")
    @Transactional(readOnly = true)
    public ResponseEntity findByAlLDepth(
            @Parameter(description = "업무코드") @RequestParam(value="workId", defaultValue = "") String workId,
            Principal principal
    ) {
        return ResponseEntity.ok(workService.findByAlLDepth(workId));
    }

    @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 =
                                    {WorkDto.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("/work/all")
    @Transactional(readOnly = true)
    public ResponseEntity findAll(
            Principal principal
    ) {
        return ResponseEntity.ok(workService.findAll());
    }

    @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 =
                                    {WorkDto.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("/work/me")
    @Transactional(readOnly = true)
    public ResponseEntity findBySabun(
            Principal principal
    ) {
        return ResponseEntity.ok(workService.findSabun(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 =
                                    {WorkLogoDto.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("/work/logo")
    public ResponseEntity logoSave(
            @RequestBody WorkLogoDto workLogoDto,
            Principal principal
    ) {
        return ResponseEntity.ok(workService.logoSave(workLogoDto));
    }

    @Operation(summary = "시스템 로고 수정", description = "시스템 로고 수정")
    @ApiResponses({
            @ApiResponse(description = "Success", responseCode = "200",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @Schema(oneOf =
                                    {WorkLogoDto.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("/work/logo")
    public ResponseEntity logoUpdate(
            @RequestBody WorkLogoDto workLogoDto,
            Principal principal
    ) {
        return ResponseEntity.ok(workService.logoSave(workLogoDto));
    }
}
