package com.devjob.controller;

import com.devjob.dto.ApiResponse;
import com.devjob.dto.MemberJoinRequest;
import com.devjob.dto.MemberResponse;
import com.devjob.security.CustomUserDetails;
import com.devjob.service.MemberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 회원 API.
 *
 * - POST /api/members/join → 201 Created (이메일 중복이면 409 Conflict)
 * - GET  /api/members/me   → 로그인한 회원의 내 정보 조회 (EP23, JWT 인증 필요)
 */
@Tag(name = "회원", description = "Member API")
@RestController
@RequestMapping("/api/members")
@RequiredArgsConstructor
public class MemberController {

    private final MemberService memberService;

    @Operation(summary = "회원가입", description = "이메일 중복 체크 후 BCrypt로 비밀번호를 암호화하여 저장합니다")
    @PostMapping("/join")
    public ResponseEntity<ApiResponse<MemberResponse>> join(@RequestBody @Valid MemberJoinRequest req) {
        return ResponseEntity.status(HttpStatus.CREATED)
                .body(ApiResponse.ok(memberService.join(req)));
    }

    @Operation(summary = "내 정보 조회", description = "JWT로 인증된 로그인 회원 본인의 정보를 조회합니다", security = @io.swagger.v3.oas.annotations.security.SecurityRequirement(name = "bearerAuth"))
    @GetMapping("/me")
    public ApiResponse<MemberResponse> me(@AuthenticationPrincipal CustomUserDetails userDetails) {
        return ApiResponse.ok(MemberResponse.from(memberService.findById(userDetails.getId())));
    }
}
