package kr.co.accura.wtm.api;

import kr.co.accura.wbx.spring.common.SecurityUtils;
import kr.co.accura.wtm.domain.approval.dto.*;
import kr.co.accura.wtm.domain.approval.service.ApprovalService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/api/wtm/approvals")
@RequiredArgsConstructor
public class ApprovalController {

    private final ApprovalService approvalService;

    @GetMapping("/pending")
    public Page<ApprovalLineItemDto> pending(Pageable pageable) {
        return approvalService.getPending(SecurityUtils.getCurrentUserId(), pageable);
    }

    @PostMapping("/{approvalId}/approve")
    public ResponseEntity<Map<String, String>> approve(
            @PathVariable Long approvalId,
            @RequestBody(required = false) ApprovalActionRequest request) {
        approvalService.approve(approvalId, SecurityUtils.getCurrentUserId(),
                request != null ? request.comment() : null);
        return ResponseEntity.ok(Map.of("message", "승인 완료"));
    }

    @PostMapping("/{approvalId}/reject")
    public ResponseEntity<Map<String, String>> reject(
            @PathVariable Long approvalId,
            @RequestBody(required = false) ApprovalActionRequest request) {
        approvalService.reject(approvalId, SecurityUtils.getCurrentUserId(),
                request != null ? request.comment() : null);
        return ResponseEntity.ok(Map.of("message", "반려 완료"));
    }

    @PostMapping("/batch-approve")
    public ResponseEntity<Map<String, String>> batchApprove(
            @RequestBody BatchApproveRequest request) {
        approvalService.batchApprove(request.lineIds(), SecurityUtils.getCurrentUserId(), request.comment());
        return ResponseEntity.ok(Map.of("message", "일괄 승인 완료"));
    }

    @PostMapping("/{approvalId}/comments")
    public ResponseEntity<Map<String, String>> addComment(
            @PathVariable Long approvalId,
            @RequestBody ApprovalActionRequest request) {
        approvalService.addComment(approvalId, SecurityUtils.getCurrentUserId(), request.comment());
        return ResponseEntity.ok(Map.of("message", "코멘트 등록 완료"));
    }

    @GetMapping("/{approvalId}")
    public ApprovalDto getDetail(@PathVariable Long approvalId) {
        return approvalService.getApprovalDetail(approvalId);
    }

    @GetMapping("/history")
    public Page<ApprovalLineItemDto> history(Pageable pageable) {
        return approvalService.getHistory(SecurityUtils.getCurrentUserId(), pageable);
    }

    @GetMapping("/overdue")
    public Page<ApprovalLineItemDto> overdue(Pageable pageable) {
        // For now returns pending items — can be enhanced with date filter
        return approvalService.getPending(SecurityUtils.getCurrentUserId(), pageable);
    }
}
