package p.studio.workspaces.debug;

import p.studio.execution.StudioExecutionLogEntry;
import p.studio.execution.StudioExecutionSnapshot;
import p.studio.execution.StudioExecutionState;

import java.util.List;
import java.util.Objects;

public record DebugWorkspaceProjection(
        StudioExecutionState state,
        List<String> renderedLogs) {

    public DebugWorkspaceProjection {
        Objects.requireNonNull(state, "state");
        renderedLogs = List.copyOf(Objects.requireNonNull(renderedLogs, "renderedLogs"));
    }

    public static DebugWorkspaceProjection from(final StudioExecutionSnapshot snapshot) {
        final StudioExecutionSnapshot safeSnapshot = Objects.requireNonNull(snapshot, "snapshot");
        return new DebugWorkspaceProjection(
                safeSnapshot.state(),
                safeSnapshot.logs().stream().map(DebugWorkspaceProjection::renderLog).toList());
    }

    private static String renderLog(final StudioExecutionLogEntry entry) {
        return "[%s][%s] %s".formatted(
                entry.source().name(),
                entry.severity().name(),
                entry.message());
    }
}
