/*
 * File: src/main/java/reports/controller/PatientPrescriptionController.java
 * Description: This controller handles requests related to patient prescription reports
 * in the ERP application. It provides an endpoint for generating reports based on patient
 * prescriptions, utilizing the specified report type.
 *
 * 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, which is set to "/api/reports/patient-prescription".
 *
 * - **@Autowired**: Automatically injects an instance of the `PatientPrescriptionService`, which handles the business logic
 *   for patient prescription reports, allowing this controller to delegate the work to this service.
 *
 * - **@PostMapping**: Maps HTTP POST requests to the `getPatientPrescriptionReport` method, allowing for the creation
 *   or processing of reports based on the provided request body and parameters.
*/

package com.nebula.erp.reports.controller;

import com.nebula.erp.reports.documents.PatientPrescriptionSwagger;
import com.nebula.erp.reports.requestmodel.SalesRequest;
import com.nebula.erp.reports.service.PatientPrescriptionService;
import com.nebula.erp.reports.utility.ApiResponseStructure;
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 java.util.Map;

@RestController
@RequestMapping("/api/reports/patient-prescription")  // Base URI for patient prescription report-related endpoints
@Tag(name="Patient and Prescription Reports APIs", description = "APIs for managing Patient and Prescription Reports")
@PatientPrescriptionSwagger.GlobalErrorResponse
public class PatientPrescriptionController {

    @Autowired
    private PatientPrescriptionService patientPrescriptionService;

    @Autowired
    private PermissionHelper permissionHelper;

    @Autowired
    private CreateLogger createLogger;

    private static final String path = "/reports/patient-prescription";

    @PatientPrescriptionSwagger.GetPatientPrescriptionReportOperation
    @PostMapping
    public ResponseEntity<ApiResponseStructure<Map<String, Object>>> getPatientPrescriptionReport(
            @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 patientPrescription service to generate the report
        ApiResponseStructure<Map<String, Object>> response = patientPrescriptionService.getPatientPrescriptionReport(salesRequest, reportType);
        createLogger.createLogger("application", path, "POST", "Data retrieved.", "");
        return ResponseEntity.ok(response);
    }

}