package com.ffii.fpsms.modules.report.web

import net.sf.jasperreports.engine.*
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource
import org.springframework.http.*
import org.springframework.web.bind.annotation.*
import java.io.InputStream
import java.time.LocalDate
import java.time.LocalTime
import java.time.format.DateTimeFormatter
import com.ffii.fpsms.modules.report.service.ReportService

@RestController
@RequestMapping("/report")
class ReportController(
     private val reportService: ReportService,
) {

    @GetMapping("/print-report1")
    fun generateReport1(
        @RequestParam fromDate: String,
        @RequestParam toDate: String,
        //this is an exampe of optional param
        @RequestParam(required = false) itemType: String?
    ): ResponseEntity<ByteArray> {
        val parameters = mutableMapOf<String, Any>()
        parameters["fromDate"] = fromDate
        parameters["toDate"] = toDate
        // you may put more params to show
        parameters["param1"] = "param1Value"
        parameters["param2"] = "param2Value"

        // Query the DB to get a list of data
        val dbData = reportService.searchReport1(fromDate, toDate, itemType)

        val pdfBytes = reportService.createPdfResponse(
            "/jasper/report1.jrxml", 
            parameters, 
            dbData
        )

        val headers = HttpHeaders().apply {
            contentType = MediaType.APPLICATION_PDF
            setContentDispositionFormData("attachment", "Report1.pdf")
            set("filename", "Report1.pdf") 
        }

        return ResponseEntity(pdfBytes, headers, HttpStatus.OK)
    }

    @GetMapping("/print-report3")
    fun generateReport3(
        //@RequestParam fromDate: String,
       // @RequestParam toDate: String,
        //this is an exampe of optional param
        @RequestParam(required = false) itemType: String?
    ): ResponseEntity<ByteArray> {
        val parameters = mutableMapOf<String, Any>()
        //parameters["lastInDateStart"] = fromDate
        //parameters["lastInDateEnd"] = toDate
        // you may put more params to show
        parameters["lastOutDateStart"] = "param1Value"
        parameters["lastOutDateEnd"] = "param2Value"

        // Query the DB to get a list of data
        val dbData = reportService.searchReport3(itemType)

        val pdfBytes = reportService.createPdfResponse(
            "/jasper/StockBalanceReport.jrxml", 
            parameters, 
            dbData
        )

        val headers = HttpHeaders().apply {
            contentType = MediaType.APPLICATION_PDF
            setContentDispositionFormData("attachment", "Report1.pdf")
            set("filename", "Report1.pdf") 
        }

        return ResponseEntity(pdfBytes, headers, HttpStatus.OK)
    }

    @GetMapping("/print-stock-in-traceability")
    fun generateStockInTraceabilityReport(
        @RequestParam(required = false) stockCategory: String?,
        @RequestParam(required = false) stockSubCategory: String?,
        @RequestParam(required = false) itemCode: String?,
        @RequestParam(required = false) lastInDateStart: String?,
        @RequestParam(required = false) lastInDateEnd: String?
    ): ResponseEntity<ByteArray> {
        val parameters = mutableMapOf<String, Any>()
        
        // Set report header parameters
        parameters["stockCategory"] = stockCategory ?: "All"
        parameters["stockSubCategory"] = stockSubCategory ?: "All"
        parameters["itemNo"] = itemCode ?: "All"
        parameters["reportDate"] = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
        parameters["reportTime"] = LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"))
        parameters["lastInDateStart"] = lastInDateStart ?: ""
        parameters["lastInDateEnd"] = lastInDateEnd ?: ""

        // Query the DB to get a list of data
        val dbData = reportService.searchStockInTraceabilityReport(
            stockCategory,
            stockSubCategory,
            itemCode,
            lastInDateStart,
            lastInDateEnd
        )

        val pdfBytes = reportService.createPdfResponse(
            "/jasper/StockInTraceabilityReport.jrxml", 
            parameters, 
            dbData
        )

        val headers = HttpHeaders().apply {
            contentType = MediaType.APPLICATION_PDF
            setContentDispositionFormData("attachment", "StockInTraceabilityReport.pdf")
            set("filename", "StockInTraceabilityReport.pdf") 
        }

        return ResponseEntity(pdfBytes, headers, HttpStatus.OK)
    }
}