package com.zioinfo.kintex.work.notice;

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/notices). 조회=인증, 쓰기=매니저 이상. */
@RestController
@RequestMapping("/api/work/notices")
public class NoticeController {

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

    public NoticeController(NoticeService service, EventAccessGuard authGuard, SystemAccessGuard mgrGuard) {
        this.service = service;
        this.authGuard = authGuard;
        this.mgrGuard = mgrGuard;
    }

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

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

    @Audited(action = "NOTICE_CREATE", targetType = "notice")
    @PostMapping
    public ApiResponse<NoticeDto> create(@AuthenticationPrincipal KintexPrincipal principal,
                                         @Valid @RequestBody NoticeSaveRequest req) {
        mgrGuard.requireManager(principal);
        return ApiResponse.ok(service.create(principal, req));
    }

    @Audited(action = "NOTICE_UPDATE", targetType = "notice")
    @PutMapping("/{id}")
    public ApiResponse<NoticeDto> update(@AuthenticationPrincipal KintexPrincipal principal,
                                         @PathVariable String id,
                                         @Valid @RequestBody NoticeSaveRequest req) {
        mgrGuard.requireManager(principal);
        return ApiResponse.ok(service.update(id, req));
    }

    @Audited(action = "NOTICE_DELETE", targetType = "notice")
    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@AuthenticationPrincipal KintexPrincipal principal,
                                    @PathVariable String id) {
        mgrGuard.requireManager(principal);
        service.delete(id);
        return ApiResponse.ok(null);
    }
}
