package com.zioinfo.kintex.visitor.notification;

import com.zioinfo.kintex.auth.KintexPrincipal;
import com.zioinfo.kintex.common.ApiResponse;
import com.zioinfo.kintex.visitor.VisitorAuth;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * F-C5 관람객 인앱 알림함 API — 인증(본인 소유분만).
 */
@RestController
@RequestMapping("/api/visitor/notifications")
public class VisitorNotificationController {

    private final VisitorNotificationService service;

    public VisitorNotificationController(VisitorNotificationService service) {
        this.service = service;
    }

    /** GET /api/visitor/notifications — 내 알림 목록. */
    @GetMapping
    public ApiResponse<List<Map<String, Object>>> list(@AuthenticationPrincipal KintexPrincipal principal) {
        return ApiResponse.ok(service.list(VisitorAuth.requireUserId(principal)));
    }

    /** GET /api/visitor/notifications/unread-count — 미읽음 수. */
    @GetMapping("/unread-count")
    public ApiResponse<Map<String, Integer>> unread(@AuthenticationPrincipal KintexPrincipal principal) {
        return ApiResponse.ok(Map.of("count", service.unreadCount(VisitorAuth.requireUserId(principal))));
    }

    /** POST /api/visitor/notifications/{id}/read — 단건 읽음. */
    @PostMapping("/{id}/read")
    public ApiResponse<Void> read(@AuthenticationPrincipal KintexPrincipal principal, @PathVariable String id) {
        service.markRead(VisitorAuth.requireUserId(principal), id);
        return ApiResponse.ok(null);
    }

    /** POST /api/visitor/notifications/read-all — 전체 읽음. */
    @PostMapping("/read-all")
    public ApiResponse<Void> readAll(@AuthenticationPrincipal KintexPrincipal principal) {
        service.markAllRead(VisitorAuth.requireUserId(principal));
        return ApiResponse.ok(null);
    }
}
