package com.yimeng.okr.controller;

import com.yimeng.okr.dto.ApiResult;
import com.yimeng.okr.security.SecurityUtils;
import com.yimeng.okr.service.NotificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/notifications")
@RequiredArgsConstructor
public class NotificationController {

    private final NotificationService notificationService;

    @GetMapping("/unread-count")
    public ApiResult<Map<String, Long>> getUnreadCount() {
        Long userId = SecurityUtils.getCurrentUserId();
        return ApiResult.success(Map.of("count", notificationService.getUnreadCount(userId)));
    }

    @GetMapping
    public ApiResult<?> getPage(@RequestParam(defaultValue = "1") int page,
                                @RequestParam(defaultValue = "20") int size,
                                @RequestParam(required = false) String type) {
        Long userId = SecurityUtils.getCurrentUserId();
        return ApiResult.success(notificationService.getPage(userId, page, size, type));
    }

    @PutMapping("/{id}/read")
    public ApiResult<Void> markRead(@PathVariable Long id) {
        notificationService.markRead(id, SecurityUtils.getCurrentUserId());
        return ApiResult.success();
    }

    @PutMapping("/read-all")
    public ApiResult<Void> markAllRead() {
        notificationService.markAllRead(SecurityUtils.getCurrentUserId());
        return ApiResult.success();
    }
}
