package p.packer;

import com.fasterxml.jackson.databind.ObjectMapper;
import p.packer.messages.PackWorkspaceRequest;
import p.packer.messages.PackerOperationStatus;
import p.packer.messages.PackerProjectContext;
import p.studio.utilities.PConstants;

import java.nio.file.Path;

public final class PackMain {
    private PackMain() {
    }

    public static void main(String[] args) {
        final Path projectRoot = args.length == 0
                ? Path.of("test-projects/%s".formatted(PConstants.PROJECT))
                : Path.of(args[0]);
        final String projectId = projectRoot.getFileName() == null
                ? "project"
                : projectRoot.getFileName().toString();

        try (var packer = Packer.bootstrap(new ObjectMapper(), event -> { })) {
            final var result = packer.workspaceService().packWorkspace(
                    new PackWorkspaceRequest(new PackerProjectContext(projectId, projectRoot)));
            System.out.println(result.summary());
            if (result.status() != PackerOperationStatus.SUCCESS) {
                throw new IllegalStateException("pack failed with status: " + result.status());
            }
        }
    }
}
