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

import com.ffii.fpsms.modules.stock.service.StockTakeRecordService
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.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.http.ResponseEntity
import com.ffii.fpsms.modules.stock.web.model.*
import org.slf4j.LoggerFactory
import com.ffii.core.response.RecordsRes
import java.time.LocalDate
@RestController
@RequestMapping("/stockTakeRecord")
class StockTakeRecordController(
    private val stockOutRecordService: StockTakeRecordService
) {
    private val logger = LoggerFactory.getLogger(StockTakeRecordController::class.java)
    /* 
    @GetMapping("/AllPickedStockOutRecordList")
    fun AllPickedStockOutRecordList(): List<AllPickedStockTakeListReponse> {
        return stockOutRecordService.AllPickedStockTakeList()
    }
    */
    @GetMapping("/AllPickedStockOutRecordList")
    fun allPickedStockOutRecordList(
        @RequestParam(required = false, defaultValue = "0") pageNum: Int,
        @RequestParam(required = false, defaultValue = "6") pageSize: Int
    ): RecordsRes<AllPickedStockTakeListReponse> {
        val all = stockOutRecordService.AllPickedStockTakeList()
        val total = all.size
        val fromIndex = pageNum * pageSize
        val toIndex = kotlin.math.min(fromIndex + pageSize, total)
        val pageList =
            if (fromIndex >= total) emptyList()
            else all.subList(fromIndex, toIndex)
    
        return RecordsRes<AllPickedStockTakeListReponse>(
            pageList,
            total
        )
    }
    @GetMapping("/approverInventoryLotDetailsAll")
    fun getApproverInventoryLotDetailsAll(
        @RequestParam(required = false) stockTakeId: Long?,                    // 可选：限定某个 stockTake
        @RequestParam(required = false, defaultValue = "0") pageNum: Int,     // 分页页码（从 0 开始）
        @RequestParam(required = false, defaultValue = "100") pageSize: Int   // 每页多少条，测试时可调大
    ): RecordsRes<InventoryLotDetailResponse> {
        return stockOutRecordService.getApproverInventoryLotDetailsAll(
            stockTakeId = stockTakeId,
            pageNum = pageNum,
            pageSize = pageSize
        )
    }
    @GetMapping("/AllApproverStockTakeList")
    fun AllApproverStockTakeList(): List<AllPickedStockTakeListReponse> {
        return stockOutRecordService.AllApproverStockTakeList()
    }
    @GetMapping("/inventoryLotDetailsBySectionNotMatch")
    fun getInventoryLotDetailsByStockTakeSectionNotMatch(
        @RequestParam stockTakeSection: String,
        @RequestParam(required = false) stockTakeId: Long?,
        @RequestParam(required = false, defaultValue = "0") pageNum: Int,
        @RequestParam(required = false) pageSize: Int?
    ): RecordsRes<InventoryLotDetailResponse> {
        // If pageSize is null, use a large number to return all records
        val actualPageSize = pageSize ?: Int.MAX_VALUE
        return stockOutRecordService.getInventoryLotDetailsByStockTakeSectionNotMatch(
            stockTakeSection, 
            stockTakeId, 
            pageNum, 
            actualPageSize
        )
    }
    
    @GetMapping("/inventoryLotDetails")
    fun getInventoryLotDetailsByWarehouseCode(
        @RequestParam warehouseCode: String
    ): List<InventoryLotDetailResponse> {
        return stockOutRecordService.getInventoryLotDetailsByWarehouseCode(warehouseCode)
    }
    
    @GetMapping("/inventoryLotDetailsBySection")
fun getInventoryLotDetailsByStockTakeSection(
    @RequestParam stockTakeSection: String,
    @RequestParam(required = false) stockTakeId: Long?,
    @RequestParam(required = false, defaultValue = "0") pageNum: Int,
    @RequestParam(required = false, defaultValue = "10") pageSize: Int
): RecordsRes<InventoryLotDetailResponse> {
    return stockOutRecordService.getInventoryLotDetailsByStockTakeSection(stockTakeSection, stockTakeId, pageNum, pageSize)
}
    
    @PostMapping("/saveStockTakeRecord")
    fun saveStockTakeRecord(
        @RequestBody request: SaveStockTakeRecordRequest,
        @RequestParam stockTakeId: Long,
        @RequestParam stockTakerId: Long
    ): ResponseEntity<Any> {
        return try {
            val savedRecord = stockOutRecordService.saveStockTakeRecord(request, stockTakeId, stockTakerId)
            logger.info("Successfully saved stock take record: ${savedRecord.id}")
            ResponseEntity.ok(savedRecord)
        } catch (e: IllegalArgumentException) {
            logger.warn("Validation error: ${e.message}")
            ResponseEntity.badRequest().body(mapOf(
                "error" to "VALIDATION_ERROR",
                "message" to (e.message ?: "Validation failed")
            ))
        } catch (e: Exception) {
            logger.error("Error saving stock take record", e)
            ResponseEntity.status(500).body(mapOf(
                "error" to "INTERNAL_ERROR",
                "message" to (e.message ?: "Failed to save stock take record")
            ))
        }
    }
    
    @PostMapping("/batchSaveStockTakeRecords")
    fun batchSaveStockTakeRecords(
        @RequestBody request: BatchSaveStockTakeRecordRequest
    ): ResponseEntity<Any> {
        return try {
            val result = stockOutRecordService.batchSaveStockTakeRecords(request)
            logger.info("Batch save completed: success=${result.successCount}, errors=${result.errorCount}")
            ResponseEntity.ok(result)
        } catch (e: IllegalArgumentException) {
            logger.warn("Validation error: ${e.message}")
            ResponseEntity.badRequest().body(mapOf(
                "error" to "VALIDATION_ERROR",
                "message" to (e.message ?: "Validation failed")
            ))
        } catch (e: Exception) {
            logger.error("Error batch saving stock take records", e)
            ResponseEntity.status(500).body(mapOf(
                "error" to "INTERNAL_ERROR",
                "message" to (e.message ?: "Failed to batch save stock take records")
            ))
        }
    }
    
    @GetMapping("/checkAndUpdateStockTakeStatus")
    fun checkAndUpdateStockTakeStatus(
        @RequestParam stockTakeId: Long,
        @RequestParam stockTakeSection: String
    ): ResponseEntity<Any> {
        return try {
            val result = stockOutRecordService.checkAndUpdateStockTakeStatus(stockTakeId, stockTakeSection)
            ResponseEntity.ok(result)
        } catch (e: Exception) {
            logger.error("Error checking and updating stock take status", e)
            ResponseEntity.status(500).body(
                mapOf(
                    "error" to "INTERNAL_ERROR",
                    "message" to (e.message ?: "Failed to check and update stock take status")
                )
            )
        }
    }
    
    @PostMapping("/saveApproverStockTakeRecord")
    fun saveApproverStockTakeRecord(
        @RequestBody request: SaveApproverStockTakeRecordRequest,
        @RequestParam stockTakeId: Long
    ): ResponseEntity<Any> {
        return try {
            val savedRecord = stockOutRecordService.saveApproverStockTakeRecord(request, stockTakeId)
            logger.info("Successfully saved approver stock take record: ${savedRecord.id}")
            ResponseEntity.ok(savedRecord)
        } catch (e: IllegalArgumentException) {
            logger.warn("Validation error: ${e.message}")
            ResponseEntity.badRequest().body(mapOf(
                "error" to "VALIDATION_ERROR",
                "message" to (e.message ?: "Validation failed")
            ))
        } catch (e: Exception) {
            logger.error("Error saving approver stock take record", e)
            ResponseEntity.status(500).body(mapOf(
                "error" to "INTERNAL_ERROR",
                "message" to (e.message ?: "Failed to save approver stock take record")
            ))
        }
    }

    @PostMapping("/batchSaveApproverStockTakeRecords")
    fun batchSaveApproverStockTakeRecords(
        @RequestBody request: BatchSaveApproverStockTakeRecordRequest
    ): ResponseEntity<Any> {
        return try {
            val result = stockOutRecordService.batchSaveApproverStockTakeRecords(request)
            logger.info("Batch approver save completed: success=${result.successCount}, errors=${result.errorCount}")
            ResponseEntity.ok(result)
        } catch (e: IllegalArgumentException) {
            logger.warn("Validation error: ${e.message}")
            ResponseEntity.badRequest().body(mapOf(
                "error" to "VALIDATION_ERROR",
                "message" to (e.message ?: "Validation failed")
            ))
        } catch (e: Exception) {
            logger.error("Error batch saving approver stock take records", e)
            ResponseEntity.status(500).body(mapOf(
                "error" to "INTERNAL_ERROR",
                "message" to (e.message ?: "Failed to batch save approver stock take records")
            ))
        }
    }
    @PostMapping("/updateStockTakeRecordStatusToNotMatch")
    fun updateStockTakeRecordStatusToNotMatch(
        @RequestParam stockTakeRecordId: Long
    ): ResponseEntity<Any> {
        return try {
            val result = stockOutRecordService.updateStockTakeRecordStatusToNotMatch(stockTakeRecordId)
            ResponseEntity.ok(result)
        } catch (e: Exception) {
            logger.error("Error updating stock take record status to not match", e)
            ResponseEntity.status(500).body(mapOf(
                "error" to "INTERNAL_ERROR",
                "message" to (e.message ?: "Failed to update stock take record status to not match")
            ))
        }
    }


    @GetMapping("/searchStockTransactions")
    fun searchStockTransactions(
        @RequestParam(required = false) itemCode: String?,
        @RequestParam(required = false) itemName: String?,
        @RequestParam(required = false) type: String?,
        @RequestParam(required = false) startDate: LocalDate?,
        @RequestParam(required = false) endDate: LocalDate?,
        @RequestParam(required = false, defaultValue = "0") pageNum: Int,
        @RequestParam(required = false, defaultValue = "100") pageSize: Int
    ): RecordsRes<StockTransactionResponse> {
        val request = SearchStockTransactionRequest(
            startDate = startDate,
            endDate = endDate,
            itemCode = itemCode,
            itemName = itemName,
            type = type,
            pageNum = pageNum,
            pageSize = pageSize
        )
        return stockOutRecordService.searchStockTransactions(request)
    }
}