package com.ffii.fpsms.modules.stock.web

import com.ffii.core.response.RecordsRes
import com.ffii.fpsms.modules.master.web.models.MessageResponse
import com.ffii.fpsms.modules.stock.service.StockIssueService
import com.ffii.fpsms.modules.stock.web.model.HandleBadItemRequest
import com.ffii.fpsms.modules.stock.web.model.SearchStockIssueRecordRequest
import com.ffii.fpsms.modules.stock.web.model.StockIssueHandleRecordResponse
import org.springframework.format.annotation.DateTimeFormat
import org.springframework.web.bind.annotation.*
import java.time.LocalDate

@RestController
@RequestMapping("/stockIssue")
class StockIssueController(
    private val stockIssueService: StockIssueService,
) {

    @PostMapping("/handleBadItem")
    fun handleBadItem(@RequestBody request: HandleBadItemRequest): MessageResponse {
        return stockIssueService.handleBadItem(request)
    }

    @GetMapping("/badItemRecords")
    fun getBadItemRecords(
        @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) startDate: LocalDate?,
        @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) endDate: LocalDate?,
        @RequestParam(required = false) itemCode: String?,
        @RequestParam(required = false) itemName: String?,
        @RequestParam(required = false) lotNo: String?,
        @RequestParam(defaultValue = "0") pageNum: Int,
        @RequestParam(defaultValue = "20") pageSize: Int,
    ): RecordsRes<StockIssueHandleRecordResponse> {
        return stockIssueService.getBadItemHandleRecords(
            SearchStockIssueRecordRequest(
                startDate = startDate,
                endDate = endDate,
                itemCode = itemCode,
                itemName = itemName,
                lotNo = lotNo,
                pageNum = pageNum,
                pageSize = pageSize,
            ),
        )
    }

    @GetMapping("/expiryItemRecords")
    fun getExpiryItemRecords(
        @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) startDate: LocalDate?,
        @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) endDate: LocalDate?,
        @RequestParam(required = false) itemCode: String?,
        @RequestParam(required = false) itemName: String?,
        @RequestParam(required = false) lotNo: String?,
        @RequestParam(defaultValue = "0") pageNum: Int,
        @RequestParam(defaultValue = "20") pageSize: Int,
    ): RecordsRes<StockIssueHandleRecordResponse> {
        return stockIssueService.getExpiryItemHandleRecords(
            SearchStockIssueRecordRequest(
                startDate = startDate,
                endDate = endDate,
                itemCode = itemCode,
                itemName = itemName,
                lotNo = lotNo,
                pageNum = pageNum,
                pageSize = pageSize,
            ),
        )
    }
}
