package de.effigenix.application.production;

import de.effigenix.application.production.command.TraceBatchForwardCommand;
import de.effigenix.domain.production.*;
import de.effigenix.shared.common.Result;
import de.effigenix.shared.security.ActorId;
import de.effigenix.shared.security.AuthorizationPort;

import java.util.List;

public class TraceBatchForward {

    private final BatchTraceabilityService traceabilityService;
    private final AuthorizationPort authorizationPort;

    public TraceBatchForward(BatchTraceabilityService traceabilityService, AuthorizationPort authorizationPort) {
        this.traceabilityService = traceabilityService;
        this.authorizationPort = authorizationPort;
    }

    public Result<BatchError, List<TracedBatch>> execute(TraceBatchForwardCommand command, ActorId performedBy) {
        if (!authorizationPort.can(performedBy, ProductionAction.BATCH_READ)) {
            return Result.failure(new BatchError.Unauthorized("Not authorized to read batches"));
        }

        return traceabilityService.traceForward(BatchId.of(command.batchId()));
    }
}
