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

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

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

    @GetMapping("/print-semi-fg-production-analysis")
    fun generateSemiFGProductionAnalysisReport(
        @RequestParam(required = false) stockCategory: String?,
        @RequestParam(required = false) stockSubCategory: String?,
        @RequestParam(required = false) itemCode: String?,
        @RequestParam(required = false) year: String?,
        @RequestParam(required = false) lastOutDateStart: String?,
        @RequestParam(required = false) lastOutDateEnd: 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["year"] = year ?: LocalDate.now().year.toString()
        parameters["reportDate"] = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
        parameters["reportTime"] = LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"))
        parameters["lastOutDateStart"] = lastOutDateStart ?: ""
        parameters["lastOutDateEnd"] = lastOutDateEnd ?: ""
        parameters["deliveryPeriodStart"] = ""
        parameters["deliveryPeriodEnd"] = ""

        // Query the DB to get a list of data
        val dbData = semiFGProductionAnalysisReportService.searchSemiFGProductionAnalysisReport(
            stockCategory,
            stockSubCategory,
            itemCode,
            year,
            lastOutDateStart,
            lastOutDateEnd
        )

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

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

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

    @GetMapping("/semi-fg-item-codes")
    fun getSemiFGItemCodes(
        @RequestParam(required = false) stockCategory: String?
    ): ResponseEntity<List<Map<String, String>>> {
        val itemCodes = semiFGProductionAnalysisReportService.getSemiFGItemCodes(stockCategory)
        return ResponseEntity(itemCodes, HttpStatus.OK)
    }

    @GetMapping("/semi-fg-item-codes-with-category")
    fun getSemiFGItemCodesWithCategory(
        @RequestParam(required = false) stockCategory: String?
    ): ResponseEntity<List<Map<String, String>>> {
        val itemCodesWithCategory = semiFGProductionAnalysisReportService.getSemiFGItemCodesWithCategory(stockCategory)
        return ResponseEntity(itemCodesWithCategory, HttpStatus.OK)
    }
}
