package com.psg.dlsysinfo.dl_sysinfo_server.controller;

import com.psg.dlsysinfo.dl_sysinfo_server.dto.ComplianceSummaryDTO;
import com.psg.dlsysinfo.dl_sysinfo_server.dto.TopVulnerabilityDTO;
import com.psg.dlsysinfo.dl_sysinfo_server.dto.VulnerableSoftwareDTO;
import com.psg.dlsysinfo.dl_sysinfo_server.repository.ClientRepository;
import com.psg.dlsysinfo.dl_sysinfo_server.security.CurrentUser;
import com.psg.dlsysinfo.dl_sysinfo_server.service.ReportingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
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 java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/reporting")
@RequiredArgsConstructor
@Slf4j
public class ReportingController {

    private final ReportingService reportingService;
    private final ClientRepository clientRepository;

    /**
     * GET /api/reporting/compliance-summary
     * Provides high-level security compliance metrics for executive dashboard
     */
    @PreAuthorize("isAuthenticated()")
    @GetMapping("/compliance-summary")
    public ResponseEntity<?> getComplianceSummary(@AuthenticationPrincipal CurrentUser user) {
        try {
            var client = clientRepository.findByClientIdentifier(user.getClientIdentifier())
                    .orElseThrow(() -> new RuntimeException("Client not found"));

            ComplianceSummaryDTO summary = reportingService.getComplianceSummary(client.getClientId());
            return ResponseEntity.ok(summary);

        } catch (Exception e) {
            log.error("Failed to generate compliance summary", e);
            return ResponseEntity.internalServerError()
                    .body(Map.of("error", "Failed to generate compliance report", "code", 500));
        }
    }

    /**
     * GET /api/reporting/top-vulnerabilities
     * Provides list of most critical vulnerabilities for detailed reporting
     */
    @PreAuthorize("isAuthenticated()")
    @GetMapping("/top-vulnerabilities")
    public ResponseEntity<?> getTopVulnerabilities(@AuthenticationPrincipal CurrentUser user) {
        try {
            var client = clientRepository.findByClientIdentifier(user.getClientIdentifier())
                    .orElseThrow(() -> new RuntimeException("Client not found"));

            List<TopVulnerabilityDTO> vulnerabilities = reportingService.getTopVulnerabilities(client.getClientId());
            return ResponseEntity.ok(vulnerabilities);

        } catch (Exception e) {
            log.error("Failed to fetch top vulnerabilities", e);
            return ResponseEntity.internalServerError()
                    .body(Map.of("error", "Failed to generate compliance report", "code", 500));
        }
    }

    /**
     * GET /api/reporting/vulnerable-software
     * Identifies software packages posing highest security risk
     */
    @PreAuthorize("isAuthenticated()")
    @GetMapping("/vulnerable-software")
    public ResponseEntity<?> getVulnerableSoftware(@AuthenticationPrincipal CurrentUser user) {
        try {
            var client = clientRepository.findByClientIdentifier(user.getClientIdentifier())
                    .orElseThrow(() -> new RuntimeException("Client not found"));

            List<VulnerableSoftwareDTO> software = reportingService.getVulnerableSoftware(client.getClientId());
            return ResponseEntity.ok(software);

        } catch (Exception e) {
            log.error("Failed to fetch vulnerable software", e);
            return ResponseEntity.internalServerError()
                    .body(Map.of("error", "Failed to generate compliance report", "code", 500));
        }
    }
}
