package com.bio.bio_backend.domain.user.member.controller;

import com.bio.bio_backend.global.dto.ApiResponseDto;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import com.bio.bio_backend.domain.user.member.dto.MemberDto;
import com.bio.bio_backend.domain.user.member.dto.CreateMemberRequestDto;
import com.bio.bio_backend.domain.user.member.dto.CreateMemberResponseDto;
import com.bio.bio_backend.domain.user.member.service.MemberService;
import com.bio.bio_backend.domain.user.member.mapper.MemberMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import com.bio.bio_backend.global.constants.ApiResponseCode;
import com.bio.bio_backend.global.annotation.LogExecution;

@Tag(name = "Member", description = "회원 관련 API")
@RestController
@RequestMapping("/members")
@RequiredArgsConstructor
@Slf4j
public class MemberController {

    private final MemberService memberService;
    private final MemberMapper memberMapper;
    private final BCryptPasswordEncoder bCryptPasswordEncoder;

    @LogExecution("회원 등록")
    @Operation(summary = "회원 등록", description = "새로운 회원을 등록합니다.")
    @ApiResponses({
        @ApiResponse(responseCode = "201", description = "회원 가입 성공"),
        @ApiResponse(responseCode = "400", description = "잘못된 요청 데이터", content = @Content(schema = @Schema(implementation = ApiResponseDto.class))),
        @ApiResponse(responseCode = "409", description = "중복된 사용자 정보", content = @Content(schema = @Schema(implementation = ApiResponseDto.class)))
    })
    @PostMapping("/register")
    public ResponseEntity<ApiResponseDto<CreateMemberResponseDto>> createMember(@RequestBody @Valid CreateMemberRequestDto requestDto) {
        MemberDto member = memberMapper.toMemberDto(requestDto);
        MemberDto createdMember = memberService.createMember(member);
        CreateMemberResponseDto responseDto = memberMapper.toCreateMemberResponseDto(createdMember);
        ApiResponseDto<CreateMemberResponseDto> apiResponse = ApiResponseDto.success(ApiResponseCode.COMMON_SUCCESS_CREATED, responseDto);
        
        return ResponseEntity.status(HttpStatus.CREATED).body(apiResponse);
    }

    // @PostMapping("/member/list")
    // public ResponseEntity<List<ResponseMember>> getMemberList(@RequestBody(required = false) Map<String, String> params) {

    //     if(params == null){
    //         params = new HashMap<>();
    //     }

    //     Iterable<MemberDTO> memberList = memberService.selectMemberList(params);

    //     List<ResponseMember> result = new ArrayList<>();

    //     memberList.forEach(m -> {
    //         result.add(new ModelMapper().map(m, ResponseMember.class));
    //     });

    //     return ResponseEntity.status(HttpStatus.OK).body(result);
    // }

    // @GetMapping("/member/{seq}")
    // public ResponseEntity<ResponseMember> selectMember(@PathVariable("seq") int seq) {

    //     MemberDTO member = memberService.selectMember(seq);

    //     ResponseMember responseMember = mapper.map(member, ResponseMember.class);

    //     return ResponseEntity.status(HttpStatus.OK).body(responseMember);
    // }

    // @PutMapping("/member")
    // public ApiResponseDto<Void> updateMember(@RequestBody @Valid CreateMemberRequestDTO requestMember, @AuthenticationPrincipal MemberDTO registrant) {
    //     // 현재 JWT는 사용자 id 값을 통하여 생성, 회원정보 변경 시 JWT 재발급 여부 검토

    //     MemberDTO member = mapper.map(requestMember, MemberDTO.class);

    //     if (requestMember.getPassword() != null) {
    //         member.setPw(bCryptPasswordEncoder.encode(requestMember.getPassword()));
    //     }

    //     member.setRegSeq(registrant.getSeq());
    //     memberService.updateMember(member);

    //     return ApiResponseDto.success(ApiResponseCode.USER_INFO_CHANGE, null);
    // }

    // @DeleteMapping("/member")
    // public ApiResponseDto<Void> deleteMember(@RequestBody @Valid CreateMemberRequestDTO requestMember){

    //     MemberDTO member = mapper.map(requestMember, MemberDTO.class);

    //     memberService.deleteMember(member);

    //     return ApiResponseDto.success(ApiResponseCode.USER_DELETE_SUCCESSFUL, null);
    // }

    // @PostMapping("/logout")
    // public ApiResponseDto<Void> logout(@AuthenticationPrincipal MemberDTO member) {

    //     String id = member.getId();

    //     try {
    //         memberService.deleteRefreshToken(id);
    //     } catch (Exception e) {
    //         return ApiResponseDto.fail(ApiResponseCode.INTERNAL_SERVER_ERROR, null);
    //     }

    //     return ApiResponseDto.success(ApiResponseCode.LOGOUT_SUCCESSFUL, null);
    // }

}
