package org.raddatz.familienarchiv.dashboard;

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.RequiredArgsConstructor;
import org.raddatz.familienarchiv.audit.AuditKind;
import org.raddatz.familienarchiv.security.Permission;
import org.raddatz.familienarchiv.security.RequirePermission;
import org.raddatz.familienarchiv.security.SecurityUtils;
import org.raddatz.familienarchiv.user.UserService;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.UUID;

@RestController
@RequestMapping("/api/dashboard")
@RequirePermission(Permission.READ_ALL)
@RequiredArgsConstructor
public class DashboardController {

    private final DashboardService dashboardService;
    private final UserService userService;

    @GetMapping("/resume")
    public DashboardResumeDTO getResume(Authentication authentication) {
        UUID userId = SecurityUtils.requireUserId(authentication, userService);
        return dashboardService.getResume(userId);
    }

    @GetMapping("/pulse")
    public DashboardPulseDTO getPulse(Authentication authentication) {
        UUID userId = SecurityUtils.requireUserId(authentication, userService);
        return dashboardService.getPulse(userId);
    }

    @GetMapping("/activity")
    public List<ActivityFeedItemDTO> getActivity(
            Authentication authentication,
            @RequestParam(defaultValue = "7") int limit,
            @Parameter(description = "Filter by audit kinds; omit for all rollup-eligible kinds",
                    array = @ArraySchema(schema = @Schema(implementation = AuditKind.class)))
            @RequestParam(required = false) Set<AuditKind> kinds) {
        UUID userId = SecurityUtils.requireUserId(authentication, userService);
        Set<AuditKind> effectiveKinds = (kinds == null || kinds.isEmpty()) ? AuditKind.ROLLUP_ELIGIBLE : kinds;
        return dashboardService.getActivity(userId, Math.min(limit, 40), effectiveKinds);
    }
}
