package space.nstart.pcp_tgu_service.service

import io.camunda.client.CamundaClient
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.util.UUID

/**
 * Adapter старта короткого BPMN-процесса updateAcceptedPlanSlots.
 */
@Service
class AcceptedPlanSlotsUpdateProcessStarter(
    private val camundaClient: CamundaClient
) {
    private val log = LoggerFactory.getLogger(javaClass)

    /** Стартует process с единственной нужной worker'у переменной satellitePlanId. */
    fun start(planId: UUID): Long {
        val processInstance = camundaClient
            .newCreateInstanceCommand()
            .bpmnProcessId(UPDATE_ACCEPTED_PLAN_SLOTS_PROCESS_ID)
            .latestVersion()
            .variables(mapOf("satellitePlanId" to planId.toString()))
            .send()
            .join()

        log.info(
            "Started accepted plan slots update process. processInstanceKey={}, planId={}",
            processInstance.processInstanceKey,
            planId
        )

        return processInstance.processInstanceKey
    }

    companion object {
        const val UPDATE_ACCEPTED_PLAN_SLOTS_PROCESS_ID = "updateAcceptedPlanSlots"
    }
}
