package com.devjob.job.controller;

import com.devjob.job.domain.JobStatus;
import com.devjob.common.dto.ApiResponse;
import com.devjob.job.dto.JobCreateRequest;
import com.devjob.job.dto.JobResponse;
import com.devjob.job.dto.JobSearchRequest;
import com.devjob.job.dto.JobUpdateRequest;
import com.devjob.common.dto.PageResponse;
import com.devjob.job.service.JobService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.net.URI;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 채용 공고 API. devjob-job 서비스, 8082번 포트.
 */
@Tag(name = "채용 공고")
@RestController
@RequestMapping("/api/jobs")
@RequiredArgsConstructor
public class JobController {

    private final JobService jobService;

    @Operation(summary = "채용 공고 등록")
    @PostMapping
    public ResponseEntity<ApiResponse<JobResponse>> create(@RequestBody @Valid JobCreateRequest req,
            @RequestHeader("X-User-Id") Long registeredBy) {   // 작성자 = 인증 사용자(3장부터 게이트웨이가 주입)
        JobResponse job = jobService.create(req, registeredBy);
        return ResponseEntity.created(URI.create("/api/jobs/" + job.id())).body(ApiResponse.ok(job));
    }

    @Operation(summary = "채용 공고 단건 조회")
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<JobResponse>> getOne(@PathVariable Long id) {
        return jobService.findById(id)
                .map(job -> ResponseEntity.ok(ApiResponse.ok(job)))
                .orElse(ResponseEntity.notFound().build());
    }

    @Operation(summary = "채용 공고 목록 조회")
    @GetMapping
    public ApiResponse<PageResponse<JobResponse>> getJobs(
            @RequestParam(defaultValue = "OPEN") JobStatus status,
            @PageableDefault(size = 10, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) {
        return ApiResponse.ok(jobService.findByStatus(status, pageable));
    }

    @Operation(summary = "채용 공고 수정")
    @PutMapping("/{id}")
    public ApiResponse<JobResponse> update(@PathVariable Long id, @RequestBody @Valid JobUpdateRequest req) {
        return ApiResponse.ok(jobService.update(id, req));
    }

    @Operation(summary = "채용 공고 검색")
    @GetMapping("/search")
    public ApiResponse<PageResponse<JobResponse>> search(
            @ModelAttribute JobSearchRequest request,
            @PageableDefault(size = 10, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) {
        return ApiResponse.ok(jobService.search(request, pageable));
    }

    @Operation(summary = "채용 공고 삭제")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        jobService.delete(id);
        return ResponseEntity.noContent().build();
    }
}
