package com.goldenchart.controller;

import com.goldenchart.service.DashboardService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/dashboard")
@RequiredArgsConstructor
public class DashboardController {

    private final DashboardService dashboardService;

    @GetMapping("/summary")
    public ResponseEntity<Map<String, Object>> summary(@RequestHeader Map<String, String> h) {
        Long userId = null;
        String uid = h.get("x-user-id");
        if (uid != null && !uid.isBlank()) {
            try { userId = Long.parseLong(uid); } catch (NumberFormatException ignored) { }
        }
        String deviceId = h.getOrDefault("x-device-id", "anonymous");
        return ResponseEntity.ok(dashboardService.buildSummary(userId, deviceId));
    }
}
