package com.ffii.fpsms.modules.jobOrder.web

import com.ffii.core.response.RecordsRes
import com.ffii.fpsms.modules.jobOrder.entity.projections.JobOrderDetail
import com.ffii.fpsms.modules.jobOrder.entity.projections.JobOrderInfo
import com.ffii.fpsms.modules.jobOrder.service.JobOrderBomMaterialService
import com.ffii.fpsms.modules.jobOrder.service.JobOrderProcessService
import com.ffii.fpsms.modules.jobOrder.service.JobOrderService
import com.ffii.fpsms.modules.jobOrder.web.model.CreateJobOrderRequest
import com.ffii.fpsms.modules.jobOrder.web.model.JobOrderReleaseRequest
import com.ffii.fpsms.modules.jobOrder.web.model.SearchJobOrderInfoRequest
import com.ffii.fpsms.modules.master.web.models.MessageResponse
import jakarta.validation.Valid
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.ModelAttribute
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import com.ffii.fpsms.modules.jobOrder.service.JoPickOrderService
import com.ffii.fpsms.modules.jobOrder.web.model.SecondScanSubmitRequest
import com.ffii.fpsms.modules.jobOrder.web.model.SecondScanIssueRequest
@RestController
@RequestMapping("/jo")
class JobOrderController(
    private val jobOrderService: JobOrderService,
    private val jobOrderBomMaterialService: JobOrderBomMaterialService,
    private val jobOrderProcessService: JobOrderProcessService,
    private val joPickOrderService: JoPickOrderService
) {

    @GetMapping("/getRecordByPage")
    fun allJobOrdersByPage(@ModelAttribute request: SearchJobOrderInfoRequest): RecordsRes<JobOrderInfo> {
        return jobOrderService.allJobOrdersByPage(request);
    }

    @GetMapping("/detail/{id}")
    fun jobOrderDetail(@PathVariable id: Long): JobOrderDetail {
        return jobOrderService.jobOrderDetail(id);
    }

    @GetMapping("/detailByCode/{code}")
    fun jobOrderDetailByCode(@PathVariable code: String): JobOrderDetail {
        return jobOrderService.jobOrderDetailByCode(code);
    }

    @PostMapping("/release")
    fun releaseJobOrder(@Valid @RequestBody request: JobOrderReleaseRequest): MessageResponse {
        return jobOrderService.releaseJobOrder(request)
    }

    @PostMapping("/manualCreate")
    fun manualCreateJobOrder(@Valid @RequestBody request: CreateJobOrderRequest): MessageResponse {
        val jo = jobOrderService.createJobOrder(request)
        if (jo.id == null) {
            throw NoSuchElementException()
        }
        jobOrderBomMaterialService.createJobOrderBomMaterialsByJoId(jo.id)
        jobOrderProcessService.createJobOrderProcessesByJoId(jo.id)

        return jo
    }
    @GetMapping("/all-lots-hierarchical/{userId}")
    fun getAllJobOrderLotsHierarchical(@PathVariable userId: Long): Map<String, Any?> {
        return joPickOrderService.getAllJobOrderLotsWithDetailsHierarchical(userId)
    }

    @GetMapping("/completed-job-order-pick-orders/{userId}")
    fun getCompletedJobOrderLotsHierarchical(@PathVariable userId: Long): Map<String, Any?> {
        return joPickOrderService.getCompletedJobOrderLotsHierarchical(userId)
    }

    @GetMapping("/completed-job-order-pick-order-records/{userId}")
    fun getCompletedJobOrderPickOrderRecords(@PathVariable userId: Long): List<Map<String, Any?>> {
        return joPickOrderService.getCompletedJobOrderPickOrderRecords(userId)
    }
    @GetMapping("/unassigned-job-order-pick-orders")
    fun getUnassignedJobOrderPickOrders(): List<Map<String, Any?>> {
        return joPickOrderService.getUnassignedJobOrderPickOrders()
    }

    @PostMapping("/assign-job-order-pick-order/{pickOrderId}/{userId}")
    fun assignJobOrderPickOrderToUser(
        @PathVariable pickOrderId: Long,
        @PathVariable userId: Long
    ): MessageResponse {
        return joPickOrderService.assignJobOrderPickOrderToUser(pickOrderId, userId)
    }

    @PostMapping("/second-scan-qr/{pickOrderId}/{itemId}")
    fun updateSecondQrScanStatus(
        @PathVariable pickOrderId: Long,
        @PathVariable itemId: Long
    ): MessageResponse {
        return joPickOrderService.updateSecondQrScanStatus(pickOrderId, itemId)
    }

    @PostMapping("/second-scan-submit/{pickOrderId}/{itemId}")
    fun submitSecondScanQuantity(
        @PathVariable pickOrderId: Long,
        @PathVariable itemId: Long,
        @RequestBody data: Map<String, Any>
    ): MessageResponse {
        val request = SecondScanSubmitRequest(
            pickOrderId = pickOrderId, // ✅ Use path variable
            itemId = itemId, // ✅ Use path variable
            qty = (data["qty"] as Number).toDouble(),
            isMissing = data["isMissing"] as? Boolean ?: false,
            isBad = data["isBad"] as? Boolean ?: false
        )
        return joPickOrderService.submitSecondScanQty(request)
    }
    
    @PostMapping("/second-scan-issue/{pickOrderId}/{itemId}")
    fun recordSecondScanIssue(
        @PathVariable pickOrderId: Long,
        @PathVariable itemId: Long,
        @RequestBody data: Map<String, Any>
    ): MessageResponse {
        val request = SecondScanIssueRequest(
            pickOrderId = pickOrderId, // ✅ Use path variable
            itemId = itemId, // ✅ Use path variable
            qty = (data["qty"] as Number).toDouble(),
            isMissing = data["isMissing"] as Boolean,
            isBad = data["isBad"] as Boolean,
            reason = data["reason"] as String,
            createdBy = (data["createdBy"] as Number).toLong()
        )
        return joPickOrderService.recordSecondScanIssue(request)
    }
    @GetMapping("/completed-job-order-pick-orders-with-completed-second-scan/{userId}")
    fun getCompletedJobOrderPickOrdersWithCompletedSecondScan(@PathVariable userId: Long): List<Map<String, Any?>> {
        return joPickOrderService.getCompletedJobOrderPickOrdersWithCompletedSecondScan(userId)
    }
    @GetMapping("/completed-job-order-pick-order-lot-details/{pickOrderId}")
    fun getCompletedJobOrderPickOrderLotDetails(@PathVariable pickOrderId: Long): List<Map<String, Any?>> {
        return joPickOrderService.getCompletedJobOrderPickOrderLotDetails(pickOrderId)
    }
}