001/*
002 * Copyright 2025 devteam@scivicslab.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing,
011 * software distributed under the License is distributed on an
012 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied.  See the License for the
014 * specific language governing permissions and limitations
015 * under the License.
016 */
017
018package com.scivicslab.actoriac.report.sections.basic;
019
020import com.scivicslab.actoriac.report.SectionBuilder;
021
022/**
023 * POJO section builder that outputs an actor's JsonState in YAML format.
024 *
025 * <p>Pure business logic - no {@code CallableByActionName}.
026 * Use {@link JsonStateSectionIIAR} to expose as an actor.</p>
027 *
028 * <p>Any IIActorRef can hold JsonState data. This section builder
029 * retrieves that data and outputs it in human-readable YAML format.</p>
030 *
031 * <h2>Output example:</h2>
032 * <pre>
033 * [JsonState: nodeGroup]
034 * cluster:
035 *   name: production
036 *   nodes:
037 *     - hostname: server1
038 *       cpu: 8
039 *       memory: 32Gi
040 *     - hostname: server2
041 *       cpu: 16
042 *       memory: 64Gi
043 * </pre>
044 *
045 * @author devteam@scivicslab.com
046 * @since 2.16.0
047 */
048public class JsonStateSection implements SectionBuilder {
049
050    private String actorName;
051    private String yamlContent;
052    private String jsonPath;
053
054    /**
055     * Sets the actor name (for display in title).
056     *
057     * @param actorName the actor name
058     */
059    public void setActorName(String actorName) {
060        this.actorName = actorName;
061    }
062
063    /**
064     * Sets the YAML content to output.
065     *
066     * @param yamlContent the YAML-formatted JsonState content
067     */
068    public void setYamlContent(String yamlContent) {
069        this.yamlContent = yamlContent;
070    }
071
072    /**
073     * Sets the JSON path filter (optional).
074     *
075     * <p>If set, only the specified path within the JsonState is output.
076     * For example, "cluster.nodes" would output only the nodes array.</p>
077     *
078     * @param jsonPath the JSON path filter, or null for entire state
079     */
080    public void setJsonPath(String jsonPath) {
081        this.jsonPath = jsonPath;
082    }
083
084    @Override
085    public String generate() {
086        if (yamlContent == null || yamlContent.isEmpty()) {
087            return "";  // No content, skip this section
088        }
089
090        StringBuilder sb = new StringBuilder();
091        String title = "[JsonState: " + (actorName != null ? actorName : "unknown") + "]";
092        if (jsonPath != null && !jsonPath.isEmpty()) {
093            title += " (path: " + jsonPath + ")";
094        }
095        sb.append(title).append("\n");
096        sb.append(yamlContent);
097        if (!yamlContent.endsWith("\n")) {
098            sb.append("\n");
099        }
100        return sb.toString();
101    }
102
103    @Override
104    public String getTitle() {
105        return null;  // Title is embedded in content
106    }
107}