// FPSMS-backend/src/main/java/com/ffii/fpsms/modules/pickOrder/web/PickExecutionIssueController.kt
package com.ffii.fpsms.modules.pickOrder.web

import com.ffii.fpsms.modules.master.web.models.MessageResponse
import com.ffii.fpsms.modules.pickOrder.entity.PickExecutionIssue
import com.ffii.fpsms.modules.pickOrder.enums.PickExecutionIssueEnum
import com.ffii.fpsms.modules.pickOrder.service.PickExecutionIssueService  //  修复导入路径
import com.ffii.fpsms.modules.pickOrder.web.models.*
import org.springframework.format.annotation.DateTimeFormat
import org.springframework.web.bind.annotation.*
import java.time.LocalDate


@RestController
@RequestMapping("/pickExecution")
class PickExecutionIssueController(
    private val pickExecutionIssueService: PickExecutionIssueService
) {

    @PostMapping("/recordIssue")
    fun recordPickExecutionIssue(@RequestBody request: PickExecutionIssueRequest): MessageResponse {
        return pickExecutionIssueService.recordPickExecutionIssue(request)
    }

    /** 无 miss/bad：仅调整 hold + SOL 标 checked，不写 pick_execution_issue（可重复提交）。 */
    @PostMapping("/applyHoldAndChecked")
    fun applyPickHoldAndMarkSolChecked(@RequestBody request: PickExecutionIssueRequest): MessageResponse {
        return pickExecutionIssueService.applyPickHoldAndMarkSolChecked(request)
    }

    @GetMapping("/issues/pickOrder/{pickOrderId}")
    fun getPickExecutionIssuesByPickOrder(@PathVariable pickOrderId: Long): List<PickExecutionIssue> {
        return pickExecutionIssueService.getPickExecutionIssuesByPickOrder(pickOrderId)
    }

    @GetMapping("/issues/pickOrderLine/{pickOrderLineId}")
    fun getPickExecutionIssuesByPickOrderLine(@PathVariable pickOrderLineId: Long): List<PickExecutionIssue> {
        return pickExecutionIssueService.getPickExecutionIssuesByPickOrderLine(pickOrderLineId)
    }
    @GetMapping("/issues/all")
    fun getAllPickExecutionIssues(
        @RequestParam(required = false) type: String?
    ): List<PickExecutionIssue> {
        println("=== Controller received type parameter: $type ===")
        return pickExecutionIssueService.getAllPickExecutionIssues(type)
    }

    @GetMapping("/badItemList")
    fun getBadItemList(
        @RequestParam(required = false) status: PickExecutionIssueEnum?
    ): List<PickExecutionIssue> {

        return pickExecutionIssueService.getBadItemList(status)
    }
    @GetMapping("/issues/missItem")
    fun getMissItemIssues(
        @RequestParam(required = false, defaultValue = "lot_issue") issueCategory: String
    ): List<StockIssueResponse> {
        return pickExecutionIssueService.getMissItemList(issueCategory)
    }
    
    @GetMapping("/issues/badItem")
    fun getBadItemIssues(
        @RequestParam(required = false, defaultValue = "lot_issue") issueCategory: String
    ): List<StockIssueResponse> {
        return pickExecutionIssueService.getBadItemList(issueCategory)
    }

@GetMapping("/issues/expiryItem")
fun getExpiryItemIssues(
    @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) expiryDate: LocalDate?,
    @RequestParam(required = false) itemCode: String?,
    @RequestParam(required = false) itemName: String?,
): List<ExpiryItemResponse> {
    return pickExecutionIssueService.getExpiryItemList(
        expiryDate = expiryDate,
        itemCode = itemCode,
        itemName = itemName,
    )
}

@PostMapping("/submitMissItem")
fun submitMissItem(@RequestBody request: SubmitIssueRequest): MessageResponse {
    return pickExecutionIssueService.submitMissItem(request)
}

@PostMapping("/batchSubmitMissItem")
fun batchSubmitMissItem(@RequestBody request: BatchSubmitIssueRequest): MessageResponse {
    return pickExecutionIssueService.batchSubmitMissItem(request)
}

@PostMapping("/submitBadItem")
fun submitBadItem(@RequestBody request: SubmitIssueRequest): MessageResponse {
    return pickExecutionIssueService.submitBadItem(request)
}

@PostMapping("/batchSubmitBadItem")
fun batchSubmitBadItem(@RequestBody request: BatchSubmitIssueRequest): MessageResponse {
    return pickExecutionIssueService.batchSubmitBadItem(request)
}

@PostMapping("/submitExpiryItem")
fun submitExpiryItem(@RequestBody request: SubmitExpiryRequest): MessageResponse {
    return pickExecutionIssueService.submitExpiryItem(request)
}

@PostMapping("/batchSubmitExpiryItem")
fun batchSubmitExpiryItem(@RequestBody request: BatchSubmitExpiryRequest): MessageResponse {
    return pickExecutionIssueService.batchSubmitExpiryItem(request)
}
@GetMapping("/lotIssueDetails")
fun getLotIssueDetails(
    @RequestParam lotId: Long,
    @RequestParam itemId: Long,
    @RequestParam issueType: String
): LotIssueDetailResponse {
    return pickExecutionIssueService.getLotIssueDetails(lotId, itemId, issueType)
}

@PostMapping("/submitIssueWithQty")
fun submitIssueWithQty(@RequestBody request: SubmitIssueWithQtyRequest): MessageResponse {
    return pickExecutionIssueService.submitIssueWithQty(request)
}
}

