package com.devjob.application.controller;

import com.devjob.common.dto.ApiResponse;
import com.devjob.application.dto.ApplicationResponse;
import com.devjob.application.service.ApplicationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 지원 API. devjob-application 서비스, 8083번 포트.
 *
 * EP08: 인증된 사용자 ID를 X-User-Id 헤더로 전달받는다.
 * 지금은 게이트웨이가 없으므로 클라이언트가 직접 헤더를 채워 테스트한다 — EP20에서
 * 게이트웨이의 JwtAuthFilter가 이 헤더를 자동으로 채워주도록 바뀐다.
 */
@Tag(name = "지원")
@RestController
@RequestMapping("/applications")
@RequiredArgsConstructor
public class ApplicationController {

    private final ApplicationService applicationService;

    @Operation(summary = "공고 지원")
    @PostMapping
    public ResponseEntity<ApiResponse<ApplicationResponse>> apply(
            @AuthenticationPrincipal Long userId,
            @RequestParam Long jobId
    ) {
        return ResponseEntity.status(HttpStatus.CREATED).body(ApiResponse.ok(applicationService.apply(userId, jobId)));
    }

    @Operation(summary = "내 지원 내역 조회")
    @GetMapping("/my")
    public ApiResponse<List<ApplicationResponse>> myApplications(@AuthenticationPrincipal Long userId) {
        return ApiResponse.ok(applicationService.findMyApplications(userId));
    }

    @Operation(summary = "지원 내역 단건 조회")
    @GetMapping("/{id}")
    public ApiResponse<ApplicationResponse> getOne(@AuthenticationPrincipal Long userId, @PathVariable Long id) {
        return ApiResponse.ok(applicationService.findById(userId, id));
    }
}
