package com.devjob.user.controller;

import com.devjob.common.dto.ApiResponse;
import com.devjob.user.dto.MemberJoinRequest;
import com.devjob.user.dto.MemberResponse;
import com.devjob.user.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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.PathVariable;
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 /users/join → 201 Created (이메일 중복이면 409 Conflict)
 * - GET  /users/me   → 로그인한 회원의 내 정보 조회
 * - GET  /users/{id} → 다른 서비스(Job/Application)가 작성자 정보를 표시할 때 조회 (EP08 참고)
 */
@Tag(name = "회원")
@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
public class MemberController {

    private final MemberService memberService;

    @Operation(summary = "회원가입")
    @PostMapping("/join")
    public ResponseEntity<ApiResponse<MemberResponse>> join(@RequestBody @Valid MemberJoinRequest req) {
        return ResponseEntity.status(HttpStatus.CREATED)
                .body(ApiResponse.ok(memberService.join(req)));
    }

    @Operation(summary = "내 정보 조회")
    @GetMapping("/me")
    public ApiResponse<MemberResponse> me(@RequestHeader("X-User-Id") Long memberId) {
        return ApiResponse.ok(MemberResponse.from(memberService.findById(memberId)));
    }

    @Operation(summary = "회원 단건 조회 (서비스 간 호출용)")
    @GetMapping("/{id}")
    public ApiResponse<MemberResponse> getById(@PathVariable Long id) {
        return ApiResponse.ok(MemberResponse.from(memberService.findById(id)));
    }
}
