package com.ga.api.controller.complaint;

import com.ga.api.service.complaint.ComplaintService;
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.complaint.ComplaintResp;
import com.ga.core.vo.complaint.ComplaintSaveReq;
import com.ga.core.vo.complaint.ComplaintSearchParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "민원 관리")
@RestController
@RequestMapping("/api/complaints")
@RequiredArgsConstructor
public class ComplaintController {

    private final ComplaintService service;

    @GetMapping
    @RequirePermission(menu = "COMPLAINT", perm = "READ")
    public ApiResponse<PageResponse<ComplaintResp>> list(@ModelAttribute ComplaintSearchParam param) {
        return ApiResponse.ok(service.list(param));
    }

    @GetMapping("/{complaintId}")
    @RequirePermission(menu = "COMPLAINT", perm = "READ")
    public ApiResponse<ComplaintResp> detail(@PathVariable Long complaintId) {
        return ApiResponse.ok(service.detail(complaintId));
    }

    @PostMapping
    @RequirePermission(menu = "COMPLAINT", perm = "CREATE")
    @DataChangeLog(menu = "COMPLAINT", table = "complaint")
    public ApiResponse<Long> create(@Valid @RequestBody ComplaintSaveReq req) {
        return ApiResponse.ok(service.create(req));
    }

    @PutMapping("/{complaintId}")
    @RequirePermission(menu = "COMPLAINT", perm = "UPDATE")
    @DataChangeLog(menu = "COMPLAINT", table = "complaint")
    public ApiResponse<Void> update(@PathVariable Long complaintId,
                                    @Valid @RequestBody ComplaintSaveReq req) {
        service.update(complaintId, req);
        return ApiResponse.ok();
    }

    @PostMapping("/{complaintId}/close")
    @RequirePermission(menu = "COMPLAINT", perm = "UPDATE")
    @DataChangeLog(menu = "COMPLAINT", table = "complaint")
    public ApiResponse<Void> close(@PathVariable Long complaintId,
                                   @RequestBody Map<String, String> body) {
        service.close(complaintId, body.get("resolution"));
        return ApiResponse.ok();
    }

    @DeleteMapping("/{complaintId}")
    @RequirePermission(menu = "COMPLAINT", perm = "DELETE")
    @DataChangeLog(menu = "COMPLAINT", table = "complaint")
    public ApiResponse<Void> delete(@PathVariable Long complaintId) {
        service.delete(complaintId);
        return ApiResponse.ok();
    }
}
