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

import com.ffii.fpsms.modules.master.entity.Warehouse
import com.ffii.fpsms.modules.master.entity.projections.WarehouseCombo
import com.ffii.fpsms.modules.master.service.WarehouseService
import jakarta.servlet.http.HttpServletRequest
import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.ServletRequestBindingException
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.multipart.MultipartHttpServletRequest

@RestController
@RequestMapping("/warehouse")
class WarehouseController(
    private val warehouseService: WarehouseService
) {
    @GetMapping
    fun getWarehouses(): List<Warehouse> {
        return warehouseService.getWarehouses()
    }

    @GetMapping("/combo")
    fun findCombo(): List<WarehouseCombo> {
        return warehouseService.findCombo()
    }

    @PostMapping("/import")
    @Throws(ServletRequestBindingException::class)
    fun importExcel(request: HttpServletRequest): ResponseEntity<*> {
        var workbook: Workbook? = null;

        try {
            val multipartFile = (request as MultipartHttpServletRequest).getFile("multipartFileList")
            workbook = XSSFWorkbook(multipartFile?.inputStream)
        } catch (e: Exception) {
            println(e)
        }

        return ResponseEntity.ok(warehouseService.importExcel(workbook))
    }
}