package com.ffii.fpsms.modules.master.web

import com.ffii.fpsms.modules.master.entity.Bom
import com.ffii.fpsms.modules.master.entity.BomRepository
import com.ffii.fpsms.modules.master.entity.projections.BomCombo
import com.ffii.fpsms.modules.master.service.BomService
import org.springframework.core.io.ByteArrayResource
import org.springframework.core.io.Resource
import org.springframework.http.HttpHeaders
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.time.LocalDate

@RestController
@RequestMapping("/bom")
class BomController (
    val bomService: BomService, private val bomRepository: BomRepository,
) {
    @GetMapping
    fun getBoms(): List<Bom> {
        return bomService.findAll()
    }

    @GetMapping("/combo")
    fun getCombo(): List<BomCombo> {
        return bomRepository.findBomComboByDeletedIsFalse();
    }

    @PostMapping("/import-bom")
    fun importBom(): ResponseEntity<Resource> {
        val reportResult = bomService.importBOM()
        val filename = "bom_excel_issue_log_${LocalDate.now()}.xlsx"

        return ResponseEntity.ok()
            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"$filename\"")
            .header(HttpHeaders.CONTENT_TYPE, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            .body(ByteArrayResource(reportResult))
    }
//    @PostMapping("/export-problematic-bom")
//    fun exportProblematicBom() {
//        return bomService.importBOM()
//    }
}