package com.zioinfo.kintex.work.report;

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 jakarta.validation.Valid;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

/** 보고서 API (/api/work/reports). 인증 사용자. */
@RestController
@RequestMapping("/api/work/reports")
public class ReportController {

    private final ReportService service;
    private final EventAccessGuard guard;

    public ReportController(ReportService service, EventAccessGuard guard) {
        this.service = service;
        this.guard = guard;
    }

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

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

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

    @PutMapping("/{id}")
    public ApiResponse<ReportDto> update(@AuthenticationPrincipal KintexPrincipal principal,
                                         @PathVariable String id,
                                         @Valid @RequestBody ReportSaveRequest req) {
        guard.require(principal);
        return ApiResponse.ok(service.update(principal, id, req));
    }

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