package com.zioinfo.hrm.recruitment;

import com.zioinfo.hrm.common.ApiResponse;
import com.zioinfo.hrm.common.AuthSupport;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/hrm/recruitment")
@RequiredArgsConstructor
public class RecruitmentController {

    private final RecruitmentService recruitmentService;

    @GetMapping("/postings")
    public ApiResponse<Map<String, Object>> listPostings(
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {
        return ApiResponse.ok(recruitmentService.listPostings(status, keyword, page, size));
    }

    @GetMapping("/postings/{id}")
    public ApiResponse<Map<String, Object>> getPosting(@PathVariable Long id) {
        return ApiResponse.ok(recruitmentService.getPosting(id));
    }

    @PostMapping("/postings")
    public ApiResponse<Void> createPosting(@RequestBody Map<String, Object> posting, Authentication auth) {
        recruitmentService.createPosting(posting, AuthSupport.actor(auth));
        return ApiResponse.ok(null);
    }

    @PutMapping("/postings/{id}")
    public ApiResponse<Void> updatePosting(@PathVariable Long id, @RequestBody Map<String, Object> posting) {
        recruitmentService.updatePosting(id, posting);
        return ApiResponse.ok(null);
    }

    @PostMapping("/postings/{id}/publish")
    public ApiResponse<Void> publish(@PathVariable Long id, Authentication auth) {
        AuthSupport.requireManager(auth);
        recruitmentService.publishPosting(id);
        return ApiResponse.ok(null);
    }

    @PostMapping("/postings/{id}/close")
    public ApiResponse<Void> close(@PathVariable Long id, Authentication auth) {
        AuthSupport.requireManager(auth);
        recruitmentService.closePosting(id);
        return ApiResponse.ok(null);
    }

    @GetMapping("/applicants")
    public ApiResponse<List<Map<String, Object>>> getApplicants(
            @RequestParam(required = false) Long postingId,
            @RequestParam(required = false) String status) {
        return ApiResponse.ok(recruitmentService.getApplicants(postingId, status));
    }

    @GetMapping("/applicants/{id}")
    public ApiResponse<Map<String, Object>> getApplicant(@PathVariable Long id) {
        return ApiResponse.ok(recruitmentService.getApplicant(id));
    }

    @PostMapping("/applicants/{postingId}")
    public ApiResponse<Void> addApplicant(@PathVariable Long postingId,
                                           @RequestBody Map<String, Object> applicant, Authentication auth) {
        recruitmentService.addApplicant(postingId, applicant, AuthSupport.actor(auth));
        return ApiResponse.ok(null);
    }

    @PatchMapping("/applicants/{id}/status")
    public ApiResponse<Void> updateStatus(@PathVariable Long id,
                                           @RequestParam String status,
                                           @RequestParam(required = false) String memo,
                                           Authentication auth) {
        AuthSupport.requireManager(auth);
        recruitmentService.updateApplicantStatus(id, status, memo);
        return ApiResponse.ok(null);
    }

    @GetMapping("/interviews")
    public ApiResponse<List<Map<String, Object>>> getInterviews(
            @RequestParam(required = false) Long postingId,
            @RequestParam(required = false) Long applicantId) {
        return ApiResponse.ok(recruitmentService.getInterviews(postingId, applicantId));
    }

    @PostMapping("/interviews")
    public ApiResponse<Void> scheduleInterview(@RequestBody Map<String, Object> interview, Authentication auth) {
        recruitmentService.scheduleInterview(interview, AuthSupport.actor(auth));
        return ApiResponse.ok(null);
    }

    @PutMapping("/interviews/{id}")
    public ApiResponse<Void> updateInterview(@PathVariable Long id, @RequestBody Map<String, Object> interview) {
        recruitmentService.updateInterview(id, interview);
        return ApiResponse.ok(null);
    }
}
