package com.devjob.controller;

import com.devjob.dto.ApiResponse;
import com.devjob.dto.ApplicationResponse;
import com.devjob.security.CustomUserDetails;
import com.devjob.service.ApplicationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
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.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * EP24: 지원하기 API.
 * - POST /api/jobs/{jobId}/apply : 공고에 지원
 * - GET  /api/members/me/applications : 내 지원 내역 조회
 */
@Tag(name = "지원")
@RestController
@RequiredArgsConstructor
public class ApplicationController {

    private final ApplicationService applicationService;

    @Operation(summary = "공고 지원", description = "로그인한 회원이 채용 공고에 지원합니다.", security = @SecurityRequirement(name = "bearerAuth"))
    @PostMapping("/api/jobs/{jobId}/apply")
    public ResponseEntity<ApiResponse<ApplicationResponse>> apply(
            @PathVariable Long jobId,
            @AuthenticationPrincipal CustomUserDetails userDetails
    ) {
        ApplicationResponse response = applicationService.apply(userDetails.getId(), jobId);
        return ResponseEntity.status(HttpStatus.CREATED).body(ApiResponse.ok(response));
    }

    @Operation(summary = "내 지원 내역 조회", security = @SecurityRequirement(name = "bearerAuth"))
    @GetMapping("/api/members/me/applications")
    public ApiResponse<List<ApplicationResponse>> myApplications(@AuthenticationPrincipal CustomUserDetails userDetails) {
        return ApiResponse.ok(applicationService.findMyApplications(userDetails.getId()));
    }
}
