package com.ga.api.controller.approval;

import com.ga.api.service.approval.ApprovalService;
import com.ga.common.annotation.DataChangeLog;
import com.ga.common.annotation.RequirePermission;
import com.ga.common.model.ApiResponse;
import com.ga.common.model.PageResponse;
import com.ga.core.vo.approval.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "결재 관리")
@RestController
@RequestMapping("/api/approvals")
@RequiredArgsConstructor
public class ApprovalController {

    private final ApprovalService service;

    // ── 결재선 ─────────────────────────────────────────────────────────────────

    @GetMapping("/lines")
    @RequirePermission(menu = "APPROVAL", perm = "READ")
    public ApiResponse<PageResponse<ApprovalLineResp>> listLines(
            @ModelAttribute ApprovalLineSearchParam param) {
        return ApiResponse.ok(service.listLines(param));
    }

    @GetMapping("/lines/{lineId}")
    @RequirePermission(menu = "APPROVAL", perm = "READ")
    public ApiResponse<ApprovalLineResp> detailLine(@PathVariable Long lineId) {
        return ApiResponse.ok(service.detailLine(lineId));
    }

    @GetMapping("/lines/{lineId}/steps")
    @RequirePermission(menu = "APPROVAL", perm = "READ")
    public ApiResponse<List<ApprovalLineStepResp>> stepsOfLine(@PathVariable Long lineId) {
        return ApiResponse.ok(service.stepsOfLine(lineId));
    }

    @PostMapping("/lines")
    @RequirePermission(menu = "APPROVAL", perm = "CREATE")
    @DataChangeLog(menu = "APPROVAL", table = "approval_line")
    public ApiResponse<Long> createLine(@Valid @RequestBody ApprovalLineSaveReq req) {
        return ApiResponse.ok(service.createLine(req));
    }

    @PutMapping("/lines/{lineId}")
    @RequirePermission(menu = "APPROVAL", perm = "UPDATE")
    @DataChangeLog(menu = "APPROVAL", table = "approval_line")
    public ApiResponse<Void> updateLine(@PathVariable Long lineId,
                                        @Valid @RequestBody ApprovalLineSaveReq req) {
        service.updateLine(lineId, req);
        return ApiResponse.ok();
    }

    @PostMapping("/lines/{lineId}/steps")
    @RequirePermission(menu = "APPROVAL", perm = "UPDATE")
    @DataChangeLog(menu = "APPROVAL", table = "approval_line_step")
    public ApiResponse<Void> addStep(@PathVariable Long lineId,
                                     @Valid @RequestBody ApprovalLineStepSaveReq req) {
        req.setLineId(lineId);
        service.addStep(req);
        return ApiResponse.ok();
    }

    // ── 결재 요청 ──────────────────────────────────────────────────────────────

    @GetMapping("/requests")
    @RequirePermission(menu = "APPROVAL", perm = "READ")
    public ApiResponse<PageResponse<ApprovalRequestResp>> listRequests(
            @ModelAttribute ApprovalRequestSearchParam param) {
        return ApiResponse.ok(service.listRequests(param));
    }

    @GetMapping("/requests/mine")
    @RequirePermission(menu = "APPROVAL", perm = "READ")
    public ApiResponse<PageResponse<ApprovalRequestResp>> myRequests(
            @ModelAttribute ApprovalRequestSearchParam param) {
        return ApiResponse.ok(service.myRequests(param));
    }

    @GetMapping("/requests/pending")
    @RequirePermission(menu = "APPROVAL", perm = "READ")
    public ApiResponse<PageResponse<ApprovalRequestResp>> pendingRequests(
            @ModelAttribute ApprovalRequestSearchParam param) {
        return ApiResponse.ok(service.pendingRequests(param));
    }

    @GetMapping("/requests/{requestId}")
    @RequirePermission(menu = "APPROVAL", perm = "READ")
    public ApiResponse<ApprovalRequestResp> detailRequest(@PathVariable Long requestId) {
        return ApiResponse.ok(service.detailRequest(requestId));
    }

    @GetMapping("/requests/{requestId}/history")
    @RequirePermission(menu = "APPROVAL", perm = "READ")
    public ApiResponse<List<ApprovalHistoryResp>> historyOfRequest(@PathVariable Long requestId) {
        return ApiResponse.ok(service.historyOfRequest(requestId));
    }

    @PostMapping("/requests")
    @RequirePermission(menu = "APPROVAL", perm = "CREATE")
    @DataChangeLog(menu = "APPROVAL", table = "approval_request")
    public ApiResponse<Long> createRequest(@Valid @RequestBody ApprovalRequestSaveReq req) {
        return ApiResponse.ok(service.createRequest(req));
    }

    @PostMapping("/requests/{requestId}/advance")
    @RequirePermission(menu = "APPROVAL", perm = "APPROVE")
    @DataChangeLog(menu = "APPROVAL", table = "approval_request")
    public ApiResponse<Void> advance(@PathVariable Long requestId,
                                     @Valid @RequestBody AdvanceReq req) {
        service.advance(requestId, req.getApproverId(), req.getAction(), req.getComment());
        return ApiResponse.ok();
    }

    @PostMapping("/requests/{requestId}/cancel")
    @RequirePermission(menu = "APPROVAL", perm = "UPDATE")
    @DataChangeLog(menu = "APPROVAL", table = "approval_request")
    public ApiResponse<Void> cancel(@PathVariable Long requestId) {
        service.cancel(requestId);
        return ApiResponse.ok();
    }
}
