package com.kamco.cd.training.members;

import com.kamco.cd.training.config.api.ApiResponseDto;
import com.kamco.cd.training.members.dto.MembersDto;
import com.kamco.cd.training.members.dto.MembersDto.Basic;
import com.kamco.cd.training.members.service.MembersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
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 jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.web.bind.annotation.*;

@Tag(name = "회원정보 관리", description = "회원정보 관리 API")
@RestController
@RequestMapping("/api/members")
@RequiredArgsConstructor
public class MembersApiController {

  private final AuthenticationManager authenticationManager;
  private final MembersService membersService;

  @Operation(summary = "회원정보 목록", description = "회원정보 조회")
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "200",
            description = "검색 성공",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Page.class))),
        @ApiResponse(responseCode = "400", description = "잘못된 검색 조건", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @PostMapping("/search")
  public ApiResponseDto<Page<Basic>> getMemberList(
      @RequestBody @Valid MembersDto.SearchReq searchReq) {
    return ApiResponseDto.ok(membersService.findByMembers(searchReq));
  }

  @Operation(
      summary = "사용자 비밀번호 변경",
      description = "로그인 성공후 status가 INACTIVE일때 로그인 id를 memberId로 path 생성필요")
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "201",
            description = "사용자 비밀번호 변경",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Long.class))),
        @ApiResponse(responseCode = "400", description = "잘못된 요청 데이터", content = @Content),
        @ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @PatchMapping("/{memberId}/password")
  public ApiResponseDto<String> resetPassword(
      @PathVariable String memberId, @RequestBody @Valid MembersDto.InitReq initReq) {
    membersService.resetPassword(memberId, initReq);
    return ApiResponseDto.createOK(memberId);
  }
}
