/*
 * File: src/main/java/reports/controller/InventoryController.java
 * Description: This controller handles requests related to inventory reports in the ERP application.
 * It exposes endpoints for generating various types of inventory reports based on sales data.
 *
 * Key Components:
 *
 * - **@RestController**: Indicates that this class is a Spring MVC controller where every method returns a domain object
 *   instead of a view. It combines **@Controller** and **@ResponseBody**.
 *
 * - **@RequestMapping**: Specifies the base URI for all endpoints in this controller. Here, it is set to "/api/reports/inventory".
 *
 * - **@Autowired**: Automatically injects an instance of InventoryService, which contains the business logic for generating reports.
 *
 * - **@PostMapping**: Maps HTTP POST requests to the `getSupplierReport` method, allowing the generation of inventory reports.
 *
 * - **getSupplierReport**: This method processes incoming requests to generate inventory reports based on provided parameters.
 *   It accepts a `SalesRequest` object in the request body and a required `reportType` as a query parameter.
*/

package com.nebula.erp.reports.controller;

import com.nebula.erp.reports.documents.InventorySwagger;
import com.nebula.erp.reports.requestmodel.SalesRequest;
import com.nebula.erp.reports.service.*;
import com.nebula.erp.reports.utility.CreateLogger;
import com.nebula.erp.reports.utility.PermissionHelper;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.nebula.erp.reports.utility.ApiResponseStructure;
import java.util.Map;

@RestController
@RequestMapping("/api/reports/inventory")  // Base URI for inventory report endpoints
@Tag(name="Inventory Reports APIs", description = "APIs for managing Inventory Reports")
@InventorySwagger.GlobalErrorResponse
public class InventoryController {

    @Autowired
    private InventoryService inventoryService;

    @Autowired
    private PermissionHelper permissionHelper;

    @Autowired
    private CreateLogger createLogger;

    private static final String path = "/reports/inventory";

    @InventorySwagger.GetSupplierReportOperation
    @PostMapping
    public ResponseEntity<ApiResponseStructure<Map<String, Object>>> getSupplierReport(
            @RequestBody SalesRequest salesRequest,
            @RequestParam(value = "reportType", required = true) String reportType) {  // Required report type parameter

        // Check user permissions
        if (!permissionHelper.hasPermission("create-report")) {
            createLogger.createLogger("error", path, "POST", "Forbidden: You do not have the required permission. Please contact the administration.", "validation");
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new ApiResponseStructure<>("error", HttpStatus.FORBIDDEN.value(), "Forbidden: You do not have the required permission. Please contact the administration.", null));
        }

        // Calls the inventory service to generate the report
        ApiResponseStructure<Map<String, Object>> response = inventoryService.getInventoryReport(salesRequest, reportType);
        createLogger.createLogger("application", path, "POST", "Data retrieved.", "");
        return ResponseEntity.ok(response);
    }

}