<?php

namespace NewsletterBridge\Services\Feed;

/**
 * Responsabilità: Formattazione RSS
 * Single Responsibility: Solo logica di formattazione RSS
 */
class RSSFormatter {
    
    /**
     * Genera il contenuto RSS per una lista di post
     */
    public function formatRSS(array $posts): string {
        ob_start();
        
        echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
        ?>
        <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"
                   xmlns:wfw="http://wellformedweb.org/CommentAPI/" 
                   xmlns:dc="http://purl.org/dc/elements/1.1/" 
                   xmlns:media="http://search.yahoo.com/mrss/"
                   xmlns:atom="http://www.w3.org/2005/Atom"
                   xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
                   xmlns:slash="http://purl.org/rss/1.0/modules/slash/" 
                   <?php do_action('rss2_ns'); ?>>
            <channel>
                <title><?php bloginfo_rss('name'); ?> - Feed</title>
                <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
                <link><?php bloginfo_rss('url') ?></link>
                <description><?php bloginfo_rss('description') ?></description>
                <lastBuildDate><?= date('D, d M Y H:i:s +0000') ?></lastBuildDate>
                <language><?php echo get_option('rss_language'); ?></language>
                <sy:updatePeriod><?php echo apply_filters('rss_update_period', 'hourly'); ?></sy:updatePeriod>
                <sy:updateFrequency><?php echo apply_filters('rss_update_frequency', '1'); ?></sy:updateFrequency>
                <?php foreach($posts as $item) { 
                    $this->itemMarkup($item);
                } ?>                
            </channel>
        </rss>
        <?php 
        
        return ob_get_clean();
    }
    
    /**
     * Genera il markup per un singolo item RSS
     */
    private function itemMarkup($item): void {
        $link = $item->link ?? '';
        // Aggiungi parametri UTM al link
        $link = $this->addUtmParameters($link);
        ?>
        <item>
            <title><?= htmlspecialchars($item->title) ?></title>
            <link><?= esc_url($link) ?></link>
            <pubDate><?= $item->date ?></pubDate>
            <dc:creator><![CDATA[<?= htmlspecialchars($item->author) ?>]]></dc:creator>
            <guid isPermaLink="false"><?= esc_url($link) ?></guid>
            <description><![CDATA[<?= htmlspecialchars($item->description ?? '') ?>]]></description>
            <?php if (!empty($item->image)): ?>
            <media:content url="<?= esc_url($item->image) ?>" medium="image" />
            <?php endif; ?>
        </item>
        <?php
    }
    
    /**
     * Aggiunge i parametri UTM a un link
     */
    private function addUtmParameters(string $link): string {
        if (empty($link)) {
            return $link;
        }
        
        // Verifica se il link ha già parametri query
        $separator = strpos($link, '?') !== false ? '&' : '?';
        
        // Aggiungi i parametri UTM
        $utm_params = 'utm_source=newsletter&utm_medium=email';
        
        return $link . $separator . $utm_params;
    }
}
