import type { OrderItemStatus, TransferHandlerMode, LogisticsStepType, ProductionWorkflowSystemRole } from "../enums.js"
import type { OrderItemCustomizationDto } from "./ingredient.dto.js"
import type { ProductConfigurationSnapshotDto } from "./product-configuration.dto.js"

export type LifecycleEventType = "ACKNOWLEDGED" | "PRODUCTION_STEP_CHANGED" | "READY" | "PICKED_UP" | "DELIVERED" | "CANCELLED" | "REVERTED"

export interface StationProductionWorkflowStepDto {
    id?: string
    code: string
    label: string
    color: string | null
    position: number
    systemRole: ProductionWorkflowSystemRole
    active: boolean
}

export interface OrderItemProductionStepDto {
    index: number | null
    code: string | null
    label: string | null
    workflowRevision: number | null
}

/** Résumé d'une étape de route logistique (inline dans RunnerQueueItemDto) */
export interface LogisticsRouteStepSummary {
    id: string
    /** Station de production (null pour TRANSPORT) */
    stationId: string | null
    stationName: string | null
    /** Station de destination (TRANSPORT uniquement) */
    destinationStationId: string | null
    destinationStationName: string | null
    stepType: LogisticsStepType
    position: number
    label: string | null
    handlerMode: TransferHandlerMode | null
    assignedRunnerId: string | null
    assignedRunnerName: string | null
}

export interface OrderItemLifecycleEventDto {
    id: string
    eventType: LifecycleEventType
    fromStatus: OrderItemStatus
    toStatus: OrderItemStatus
    fromProductionStepIndex?: number | null
    toProductionStepIndex?: number | null
    fromProductionStepCode?: string | null
    toProductionStepCode?: string | null
    actorId: string | null
    actorName: string | null
    stationId: string | null
    occurredAt: string
}

export interface OrderItemStockCoverageDto {
    totalUnits: number
    coveredUnits: number
    remainingUnits: number
}

export interface OrderItemDto {
    id: string
    name: string
    productId: string
    quantity: number
    unitPrice: number
    status: OrderItemStatus
    orderId: string
    stationId?: string | null
    stationName?: string | null
    createdAt: string
    takenByName?: string | null
    pickedUpByName?: string | null
    recipeId?: string | null
    recipeName?: string | null
    configurationLabel?: string | null
    configurationSnapshot?: ProductConfigurationSnapshotDto | null
    productionConfigurationSnapshot?: ProductConfigurationSnapshotDto | null
    stockCoverage?: OrderItemStockCoverageDto | null
    customizations?: OrderItemCustomizationDto[]
    lifecycleEvents?: OrderItemLifecycleEventDto[]

    /** Route logistique multi-étapes (résolu au runtime) */
    logisticsRouteId?: string | null
    /** Position de l'étape courante dans la route logistique */
    currentStepPosition?: number | null
    /** Station de l'étape courante (null = transport/en transit) */
    currentStepStationId?: string | null
    /** Nom du transporteur (snapshot au PICKED_UP) */
    transportedByName?: string | null
    productionStep?: OrderItemProductionStepDto | null
    productionWorkflowSnapshot?: StationProductionWorkflowStepDto[] | null
}

/**
 * Item enrichi pour la vue KDS (Production Station)
 * Groupé par commande source, avec info station d'origine
 */
export interface ProductionQueueItemDto {
    id: string
    name: string
    productId: string
    quantity: number
    unitPrice: number
    status: OrderItemStatus
    orderId: string
    orderShortId: string
    createdByName?: string | null
    sourceStationId: string | null
    sourceStationName: string | null
    createdAt: string
    takenByName: string | null
    pickedUpByName: string | null
    /** Mode de transport applicable à l'étape courante / suivante */
    currentHandlerMode?: TransferHandlerMode | null
    /** Progression globale de la commande (tous items actifs confondus) */
    orderTotalActiveQuantity?: number
    orderReadyQuantity?: number
    orderPreparingQuantity?: number
    orderPendingQuantity?: number
    orderPickedUpQuantity?: number
    recipeId?: string | null
    recipeName?: string | null
    configurationLabel?: string | null
    configurationSnapshot?: ProductConfigurationSnapshotDto | null
    productionConfigurationSnapshot?: ProductConfigurationSnapshotDto | null
    stockCoverage?: OrderItemStockCoverageDto | null
    customizations?: OrderItemCustomizationDto[]
    lifecycleEvents?: OrderItemLifecycleEventDto[]
    productionStep?: OrderItemProductionStepDto | null
    productionWorkflowSnapshot?: StationProductionWorkflowStepDto[] | null
}

export interface ProductionQueueGroupDto {
    orderId: string
    orderShortId: string
    createdByName?: string | null
    sourceStationId: string | null
    sourceStationName: string | null
    createdAt: string
    items: ProductionQueueItemDto[]
}

/**
 * Item enrichi pour la vue Runner
 * Inclut les infos de route logistique
 */
export interface RunnerQueueItemDto {
    id: string
    name: string
    productId: string
    quantity: number
    unitPrice: number
    status: OrderItemStatus
    orderId: string
    orderShortId: string
    productionStationId: string | null
    productionStationName: string
    destinationStationId: string | null
    destinationStationName: string
    createdAt: string
    takenByName: string | null
    pickedUpByName: string | null

    /** Nom du transporteur effectif (snapshot, rempli au PICKED_UP) */
    transportedByName: string | null

    /** Progression globale de la commande (tous items actifs confondus) */
    orderTotalActiveQuantity?: number
    orderReadyQuantity?: number
    orderPreparingQuantity?: number
    orderPendingQuantity?: number
    orderPickedUpQuantity?: number

    /** Route logistique multi-étapes */
    logisticsRouteId: string | null
    currentStepPosition: number | null
    logisticsRouteSteps?: LogisticsRouteStepSummary[]

    configurationLabel?: string | null
    configurationSnapshot?: ProductConfigurationSnapshotDto | null
    productionConfigurationSnapshot?: ProductConfigurationSnapshotDto | null
    stockCoverage?: OrderItemStockCoverageDto | null
    customizations?: OrderItemCustomizationDto[]
    lifecycleEvents?: OrderItemLifecycleEventDto[]
}

export interface RunnerQueueGroupDto {
    stationId: string | null
    stationName: string
    items: RunnerQueueItemDto[]
}
