package de.effigenix.application.production;

import de.effigenix.application.production.command.TraceBatchBackwardCommand;
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 TraceBatchBackward {

    private final BatchTraceabilityService traceabilityService;
    private final AuthorizationPort authorizationPort;

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

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

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