<?php

namespace ZenoChatbot\Support;

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Formats JSON resource arrays into AI-friendly text
 *
 * Takes arrays with {title, link, context} structure and converts
 * them into readable numbered lists for LLM consumption.
 */
class ResourceFormatter
{
    /**
     * Format a JSON resource array into readable text
     *
     * Input: [{"title": "...", "link": "...", "context": "..."}, ...]
     * Output: Numbered list with title, context, and link
     *
     * @param array $items Array of resource items
     * @return string Formatted text
     */
    public static function format($items)
    {
        if (empty($items)) {
            return '';
        }

        $lines = [];
        $index = 1;

        foreach ($items as $item) {

            if (!is_array($item)) {
                continue;
            }

            $title = trim($item['title'] ?? '');
            $context = trim($item['context'] ?? '');
            $link = trim($item['link'] ?? '');

            if (empty($title) && empty($context)) {
                continue;
            }

            $entry = static::formatEntry($index, $title, $context, $link);

            if ($entry !== '') {
                $lines[] = $entry;
                $index++;
            }
        }

        return implode("\n\n", $lines);
    }

    /**
     * Format a single entry
     *
     * @param int $index Entry number
     * @param string $title Entry title
     * @param string $context Entry context/description
     * @param string $link Entry URL
     * @return string Formatted entry
     */
    private static function formatEntry($index, $title, $context, $link)
    {
        $parts = [];

        // Title line with number
        if ($title !== '') {
            $parts[] = "{$index}. **{$title}**";
        } else {
            $parts[] = "{$index}.";
        }

        // Context on the next line, indented
        if ($context !== '') {
            $parts[] = "   {$context}";
        }

        // Link on the next line, indented
        if ($link !== '') {
            $parts[] = "   Link: {$link}";
        }

        return implode("\n", $parts);
    }

    /**
     * Format from JSON string
     *
     * @param string $json JSON string
     * @return string Formatted text, empty string on invalid JSON
     */
    public static function fromJson($json)
    {
        if (trim($json) === '') {
            return '';
        }

        $items = json_decode($json, true);

        if (!is_array($items)) {
            return '';
        }

        return static::format($items);
    }

    /**
     * Format from a file path
     *
     * @param string $filePath Path to JSON file
     * @return string Formatted text, empty string if file doesn't exist or is invalid
     */
    public static function fromFile($filePath)
    {
        if (!file_exists($filePath) || !is_readable($filePath)) {
            return '';
        }

        $content = file_get_contents($filePath);

        if ($content === false) {
            return '';
        }

        return static::fromJson($content);
    }
}

