package tech.easyflow.ai.node.filegeneration;

import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * HTML 文件渲染器。
 *
 * @author Codex
 * @since 2026-04-18
 */
@Component
public class HtmlFileRenderer implements DocumentRenderer {
    private final HtmlDocumentBuilder htmlDocumentBuilder;

    public HtmlFileRenderer(HtmlDocumentBuilder htmlDocumentBuilder) {
        this.htmlDocumentBuilder = htmlDocumentBuilder;
    }

    @Override
    public TargetFormat getTargetFormat() {
        return TargetFormat.HTML;
    }

    @Override
    public boolean supports(SourceFormat sourceFormat) {
        return sourceFormat == SourceFormat.PLAIN_TEXT
                || sourceFormat == SourceFormat.MARKDOWN
                || sourceFormat == SourceFormat.HTML;
    }

    @Override
    public FileGenerationResult render(FileGenerationRequest request) {
        byte[] bytes = htmlDocumentBuilder.buildDocument(request).getBytes(StandardCharsets.UTF_8);
        return new FileGenerationResult(
                request.getFileName(),
                TargetFormat.HTML.getValue(),
                TargetFormat.HTML.getContentType(),
                bytes
        );
    }
}
