package com.zioinfo.kintex.work.meeting;

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/meetings). 인증 사용자. 편집·회의록·삭제는 주최자(서비스에서 검증). */
@RestController
@RequestMapping("/api/work/meetings")
public class MeetingController {

    private final MeetingService service;
    private final EventAccessGuard guard;

    public MeetingController(MeetingService service, EventAccessGuard guard) {
        this.service = service;
        this.guard = guard;
    }

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

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

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

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

    @PutMapping("/{id}/minutes")
    public ApiResponse<Void> minutes(@AuthenticationPrincipal KintexPrincipal principal,
                                     @PathVariable String id, @RequestBody MinutesRequest req) {
        guard.require(principal);
        service.saveMinutes(id, req.minutes());
        return ApiResponse.ok(null);
    }

    @PostMapping("/{id}/actions")
    public ApiResponse<MeetingActionDto> addAction(@AuthenticationPrincipal KintexPrincipal principal,
                                                   @PathVariable String id,
                                                   @Valid @RequestBody MeetingActionRequest req) {
        guard.require(principal);
        return ApiResponse.ok(service.addAction(id, req));
    }

    @PatchMapping("/actions/{actionId}/status")
    public ApiResponse<Void> actionStatus(@AuthenticationPrincipal KintexPrincipal principal,
                                          @PathVariable String actionId, @RequestParam String value) {
        guard.require(principal);
        service.updateActionStatus(actionId, value);
        return ApiResponse.ok(null);
    }

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