package com.kospo.talk.controller;

import com.kospo.talk.config.exception.CustomErrorResponse;
import com.kospo.talk.model.Talk;
import com.kospo.talk.service.TalkMemberService;
import com.kospo.talk.service.TalkService;
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.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

@RestController
@Tag(name = "Talk Api", description = "채팅 관리")
@RequestMapping("/api")
@RequiredArgsConstructor
public class TalkController {
    private final TalkService talkService;
    private final TalkMemberService talkMemberService;

    @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 =
                                    {Talk.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("/talk")
    public ResponseEntity talkList(
            Principal principal
    ) {
        return ResponseEntity.ok(talkService.findByMemberSabun(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 =
                                    {Talk.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("/talk/group")
    public ResponseEntity talkGroupList(
            @Parameter(description = "시작 라인") @RequestParam(name = "start") Integer start,
            @Parameter(description = "가져올 데이터수") @RequestParam(name = "row") Integer row,
            Principal principal
    ) {
        return ResponseEntity.ok(talkService.findTalkGroup(principal.getName(), start, row));
    }

    @Operation(summary = "그룹톡 정보 조회", description = "talkId 해당되는 정보 조회")
    @ApiResponses({
            @ApiResponse(description = "Success", responseCode = "200",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json", array = @ArraySchema(
                            schema = @Schema(oneOf =
                                    {Talk.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("/talk/group/{talkId}")
    public ResponseEntity talkGroupList(
            @Parameter(description = "톡아이디") @PathVariable String talkId,
            Principal principal
    ) {
        return ResponseEntity.ok(talkService.findTalkGroupByTalkId(principal.getName(), talkId));
    }

    @Operation(summary = "마크 설정", description = "마크 활성화시 최상단 고정")
    @ApiResponses({
            @ApiResponse(description = "Success", responseCode = "200",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json", schema = @Schema(oneOf =
                                    {Boolean.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("/talk/mark/{talkId}")
    public ResponseEntity markYn(
            @Parameter(description = "채팅 아이디") @PathVariable("talkId") String talkId,
            Principal principal) {
        boolean flag = talkMemberService.updateMark(talkId, principal.getName());
        return ResponseEntity.ok(flag);
    }

    @Operation(summary = "삭제 설정", description = "채팅방이 종료된 경우에만 본인의 채팅방 삭제(실제 삭제 아님 상태제어)")
    @ApiResponses({
            @ApiResponse(description = "Success", responseCode = "200",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json", schema = @Schema(oneOf =
                                    {Boolean.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("/talk/delete/{talkId}")
    public ResponseEntity deleteYn(
            @Parameter(description = "채팅 아이디") @PathVariable("talkId") String talkId,
            Principal principal) {
        boolean flag = talkMemberService.updateDelete(talkId, principal.getName());
        return ResponseEntity.ok(flag);
    }

}
