package com.zioinfo.kintex.work.opinion;

import com.zioinfo.kintex.auth.EventAccessGuard;
import com.zioinfo.kintex.auth.KintexPrincipal;
import com.zioinfo.kintex.common.ApiResponse;
import com.zioinfo.kintex.common.PageResponse;
import com.zioinfo.kintex.common.audit.Audited;
import com.zioinfo.kintex.system.SystemAccessGuard;
import jakarta.validation.Valid;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

/** 의견 API (/api/work/opinions). 조회/등록=인증, 상태변경/답변=매니저. */
@RestController
@RequestMapping("/api/work/opinions")
public class OpinionController {

    private final OpinionService service;
    private final EventAccessGuard authGuard;
    private final SystemAccessGuard mgrGuard;

    public OpinionController(OpinionService service, EventAccessGuard authGuard, SystemAccessGuard mgrGuard) {
        this.service = service;
        this.authGuard = authGuard;
        this.mgrGuard = mgrGuard;
    }

    @GetMapping
    public ApiResponse<PageResponse<OpinionDto>> list(@AuthenticationPrincipal KintexPrincipal principal,
                                                      @RequestParam(required = false) String eventId,
                                                      @RequestParam(required = false) String status,
                                                      @RequestParam(defaultValue = "0") int page,
                                                      @RequestParam(defaultValue = "20") int size) {
        authGuard.require(principal);
        return ApiResponse.ok(service.list(eventId, status, page, size));
    }

    @GetMapping("/{id}")
    public ApiResponse<OpinionDetail> get(@AuthenticationPrincipal KintexPrincipal principal,
                                          @PathVariable String id) {
        authGuard.require(principal);
        return ApiResponse.ok(service.get(principal, id));
    }

    @PostMapping
    public ApiResponse<OpinionDto> create(@AuthenticationPrincipal KintexPrincipal principal,
                                          @Valid @RequestBody OpinionSaveRequest req) {
        authGuard.require(principal);
        return ApiResponse.ok(service.create(principal, req));
    }

    @Audited(action = "OPINION_STATUS", targetType = "opinion")
    @PatchMapping("/{id}/status")
    public ApiResponse<Void> status(@AuthenticationPrincipal KintexPrincipal principal,
                                    @PathVariable String id, @RequestParam String value) {
        mgrGuard.requireManager(principal);
        service.changeStatus(id, value);
        return ApiResponse.ok(null);
    }

    @Audited(action = "OPINION_COMMENT", targetType = "opinion_comment")
    @PostMapping("/{id}/comments")
    public ApiResponse<OpinionCommentDto> comment(@AuthenticationPrincipal KintexPrincipal principal,
                                                  @PathVariable String id,
                                                  @Valid @RequestBody OpinionCommentRequest req) {
        mgrGuard.requireManager(principal);
        return ApiResponse.ok(service.addComment(principal, id, req.content()));
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@AuthenticationPrincipal KintexPrincipal principal,
                                    @PathVariable String id) {
        authGuard.require(principal);
        service.delete(principal, id);
        return ApiResponse.ok(null);
    }
}
