package com.writeoff.module.notification.controller;

import com.writeoff.common.api.ApiResponse;
import com.writeoff.common.api.PageResult;
import com.writeoff.module.notification.model.InAppNotificationInfo;
import com.writeoff.module.notification.service.InAppNotificationService;
import com.writeoff.security.DataScopeType;
import com.writeoff.security.RequirePermission;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/in-app-notifications")
public class InAppNotificationController {
    private final InAppNotificationService inAppNotificationService;

    public InAppNotificationController(InAppNotificationService inAppNotificationService) {
        this.inAppNotificationService = inAppNotificationService;
    }

    @GetMapping
    @RequirePermission(value = "notification.inapp.read", dataScope = DataScopeType.TENANT, auditAction = "IN_APP_NOTIFICATION_LIST")
    public ApiResponse<PageResult<InAppNotificationInfo>> listMine() {
        return ApiResponse.success(inAppNotificationService.listMine());
    }

    @PostMapping("/{id}/read")
    @RequirePermission(value = "notification.inapp.mark-read", dataScope = DataScopeType.TENANT, auditAction = "IN_APP_NOTIFICATION_MARK_READ")
    public ApiResponse<Void> markRead(@PathVariable("id") Long id) {
        inAppNotificationService.markRead(id);
        return ApiResponse.success(null);
    }

    @PostMapping("/read-all")
    @RequirePermission(value = "notification.inapp.mark-read", dataScope = DataScopeType.TENANT, auditAction = "IN_APP_NOTIFICATION_MARK_ALL_READ")
    public ApiResponse<Map<String, Object>> markAllRead() {
        int affected = inAppNotificationService.markAllRead();
        Map<String, Object> data = new LinkedHashMap<String, Object>();
        data.put("affected", affected);
        return ApiResponse.success(data);
    }
}
