package com.ffii.fpsms.modules.deliveryOrder.web.models

import java.time.LocalDateTime
import java.time.LocalDate
import com.fasterxml.jackson.annotation.JsonFormat
import java.time.LocalTime
data class DoDetailResponse(
    val id: Long,
    val code: String,
    val supplierCode: String?,
    val shopCode: String?,
    val shopName: String?,
    val currencyCode: String?,
    @JsonFormat(pattern = "yyyy-MM-dd")
    val orderDate: LocalDateTime?,
    @JsonFormat(pattern = "yyyy-MM-dd")
    val estimatedArrivalDate: LocalDateTime?,
    @JsonFormat(pattern = "yyyy-MM-dd")
    val completeDate: LocalDateTime?,
    val status: String?,
    /** 加單 DO（M18 加單專用同步） */
    val isExtra: Boolean = false,
    /** 揀貨員名稱（來源：delivery_order_pick_order.handlerName） */
    val handlerName: String? = null,
    /** 來源 DO 車線（do_pick_order / delivery_order_pick_order） */
    val truckLaneCode: String? = null,
    val deliveryOrderLines: List<DoDetailLineResponse>
)

data class DoDetailLineResponse(
    val id: Long,
    val itemNo: String?,
    val qty: java.math.BigDecimal?,
    /** Sum of stock_out_line.qty for the linked pick order line; falls back to [qty] when unavailable. */
    val actualShippedQty: java.math.BigDecimal?,
    val price: java.math.BigDecimal?,
    val status: String?,
    val itemName: String?,
    val uom: String?,
    val uomCode: String?,
    val shortUom: String?,
    /** Sum of (inQty - outQty - holdQty) on AVAILABLE lot lines for this item. */
    val stockQty: java.math.BigDecimal?,
    /** `available` when stockQty >= qty, else `insufficient`. */
    val availableStatus: String?,
)
data class StoreLaneSummary(
    val storeId: String,
    val rows: List<LaneRow>,
    val defaultTruckCount: Int? = null
)

data class LaneRow(
    val truckDepartureTime: String,
    val lanes: List<LaneBtn>
)

data class LaneBtn(
    val truckLanceCode: String,
    val loadingSequence: Int? = null,
    val unassigned: Int,
    val total: Int,
    // 同一 truckLanceCode + loadingSequence 的 handler 去重后逗号拼接
    val handlerName: String? = null,
    /** Workbench Etra lane: `delivery_order_pick_order.storeId` (2/F, 4/F, …) for assign / modal scope */
    val storeId: String? = null,
    /** Workbench Etra / lane row: `truckDepartureTime` as ISO local time string for assign-by-lane */
    val truckDepartureTime: String? = null,
)

/** Etra family tickets (`isExtra` / `isExtrabatch` / `isExtrasingle`) for a day, grouped by shop then truck. */
data class WorkbenchEtraShopLaneGroup(
    val shopCode: String?,
    val shopName: String?,
    val lanes: List<LaneBtn>,
)
data class AssignByLaneRequest(
    val userId: Long,
    val storeId: String,
    val truckDepartureTime: String?,  // 可选：限定出车时间
    val truckLanceCode: String ,
    val loadingSequence: Int? = null,
    val requiredDate: LocalDate?,       // 必填：车道编号
    /** When `isExtra`, assignment candidates are limited to `releaseType = isExtra` rows. */
    val releaseType: String? = null,
)
data class DoPickOrderSummaryItem(
    @JsonFormat(pattern = "HH:mm")
    val truckDepartureTime: java.time.LocalTime?,
    val truckLanceCode: String?,
    val loadingSequence: Int?,
    val handledBy: Long?,
    // 来源：delivery_order_pick_order.handlerName
    val handlerName: String? = null
)
data class DoSearchRow(
        val id: Long,
        val code: String?,
        val shopName: String?,
        val status: String,
        val orderDate: LocalDateTime,
        val estimatedArrivalDate: LocalDateTime,
        val completeDate: LocalDateTime,
        val supplierName: String,
        val shopCode: String,
        val supplierCode: String,
    )
    data class DoSearchRowResponse(
    val id: Long,
    val code: String,
    val shopName: String?,
    val supplierName: String?,
    val orderDate: LocalDateTime?,
    val estimatedArrivalDate: LocalDateTime?,
    val status: String?,
    val deliveryOrderLineCount: Int
)
interface DoSearchRowProjection {
    val id: Long
    val code: String
    val shopName: String?
    val supplierName: String?
    val orderDate: LocalDateTime?
    val estimatedArrivalDate: LocalDateTime?
    val status: String?
    val deliveryOrderLineCount: Long
}
data class ReleasedDoPickOrderListItem(
    val id: Long,                          // doPickOrderId，用於 assign
    @JsonFormat(pattern = "yyyy-MM-dd")
    val requiredDeliveryDate: LocalDate?,  // Date 欄
    val shopCode: String?,                 // Shop
    val shopName: String?,                 // Shop
    val storeId: String?,                  // 2/F or 4/F
    val truckLanceCode: String?,           // Truck (Lane)
    @JsonFormat(pattern = "HH:mm")
    val truckDepartureTime: LocalTime?,    // Truck 時間
    val deliveryOrderCodes: List<String>   // 多個 DO code，前端換行顯示
)

// Assign 用的 request
data class AssignByDoPickOrderIdRequest(
    val userId: Long,
    val doPickOrderId: Long
)

/** Workbench: assign a `delivery_order_pick_order` ticket + its linked pick orders. */
data class AssignByDeliveryOrderPickOrderIdRequest(
    val userId: Long,
    val deliveryOrderPickOrderId: Long,
)