package com.zioinfo.kintex.livenotice;

import com.zioinfo.kintex.auth.EventAccessGuard;
import com.zioinfo.kintex.auth.EventRole;
import com.zioinfo.kintex.auth.KintexPrincipal;
import com.zioinfo.kintex.common.ApiResponse;
import com.zioinfo.kintex.livenotice.dto.LiveNoticeDtos.LiveNoticeCreateRequest;
import com.zioinfo.kintex.livenotice.dto.LiveNoticeDtos.LiveNoticeDto;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 행사 라이브 공지(F-B6) 주최자 API — 발행/목록/보관/고정. 행사 ORGANIZER(및 관리자·홀매니저) 권한.
 * <p>발행 시 §5B 알림 모듈로 행사 멤버 in-app 팬아웃(서비스에서 처리).
 */
@RestController
@RequestMapping("/api/events/{eventId}/live-notices")
public class LiveNoticeController {

    private final LiveNoticeService service;
    private final EventAccessGuard guard;

    public LiveNoticeController(LiveNoticeService service, EventAccessGuard guard) {
        this.service = service;
        this.guard = guard;
    }

    /** GET — 주최자 목록(전체 상태, status 필터 선택). */
    @GetMapping
    public ApiResponse<List<LiveNoticeDto>> list(@AuthenticationPrincipal KintexPrincipal principal,
                                                 @PathVariable String eventId,
                                                 @RequestParam(required = false) String status,
                                                 @RequestParam(required = false, defaultValue = "100") int limit) {
        guard.requireEventAccess(principal, eventId);
        return ApiResponse.ok(service.organizerList(eventId, status, limit));
    }

    /** POST — 라이브 공지 발행(ORGANIZER/관리자/홀매니저). */
    @PostMapping
    public ApiResponse<LiveNoticeDto> publish(@AuthenticationPrincipal KintexPrincipal principal,
                                              @PathVariable String eventId,
                                              @RequestBody LiveNoticeCreateRequest req) {
        guard.requireRole(principal, eventId, EventRole.ORGANIZER, EventRole.HALL_MANAGER);
        return ApiResponse.ok(service.publish(principal, eventId, req));
    }

    /** POST /{id}/archive — 공지 보관. */
    @PostMapping("/{id}/archive")
    public ApiResponse<LiveNoticeDto> archive(@AuthenticationPrincipal KintexPrincipal principal,
                                              @PathVariable String eventId,
                                              @PathVariable String id) {
        guard.requireRole(principal, eventId, EventRole.ORGANIZER, EventRole.HALL_MANAGER);
        return ApiResponse.ok(service.archive(principal, eventId, id));
    }

    /** POST /{id}/pin — 고정 토글({pinned:true|false}). */
    @PostMapping("/{id}/pin")
    public ApiResponse<LiveNoticeDto> pin(@AuthenticationPrincipal KintexPrincipal principal,
                                          @PathVariable String eventId,
                                          @PathVariable String id,
                                          @RequestBody(required = false) Map<String, Object> body) {
        guard.requireRole(principal, eventId, EventRole.ORGANIZER, EventRole.HALL_MANAGER);
        boolean pinned = body != null && Boolean.TRUE.equals(body.get("pinned"));
        return ApiResponse.ok(service.setPinned(principal, eventId, id, pinned));
    }
}
