package com.psg.dlsysinfo.dl_sysinfo_server.controller;

import com.psg.dlsysinfo.dl_sysinfo_server.dto.SoftwareSummaryDTO;
import com.psg.dlsysinfo.dl_sysinfo_server.entity.Client;
import com.psg.dlsysinfo.dl_sysinfo_server.security.CurrentUser;
import com.psg.dlsysinfo.dl_sysinfo_server.service.SoftwareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.psg.dlsysinfo.dl_sysinfo_server.repository.ClientRepository;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/software")
public class SoftwareController {

    @Autowired
    private SoftwareService softwareService;

    @Autowired
    private ClientRepository clientRepository;

    @GetMapping("/summary")
    public List<SoftwareSummaryDTO> getSoftwareOverview(@AuthenticationPrincipal CurrentUser user) {
        if (user.hasRole("ADMIN")) {
            System.out.println("🔓 Admin override: fetching software for all clients");
            return softwareService.getSoftwareOverviewForAllClients(); // ⬅️ implement this
        }

        Client client = clientRepository.findByClientIdentifier(user.getClientIdentifier())
                .orElseThrow(() -> new RuntimeException("Client not found"));

        return softwareService.getSoftwareOverviewForClient(client.getClientId());
    }





}

