<?php
require_once 'auth/session.php';
require_once 'includes/permissions.php';
requireLogin();

$user = getCurrentUser();
$action = $_REQUEST['action'] ?? '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $channel_id = $_POST['channel_id'] ?? 0;
    
    // Permission check: must have manage channels permission
    $stmt = db()->prepare("SELECT server_id FROM channels WHERE id = ?");
    $stmt->execute([$channel_id]);
    $chan = $stmt->fetch();
    
    if (!$chan || !Permissions::hasPermission($user['id'], $chan['server_id'], Permissions::MANAGE_CHANNELS)) {
        echo json_encode(['success' => false, 'error' => 'Unauthorized']);
        exit;
    }

    if ($action === 'add') {
        $url = $_POST['url'] ?? '';
        if (!filter_var($url, FILTER_VALIDATE_URL)) {
            echo json_encode(['success' => false, 'error' => 'Invalid URL']);
            exit;
        }
        $stmt = db()->prepare("INSERT INTO channel_rss_feeds (channel_id, url) VALUES (?, ?)");
        $stmt->execute([$channel_id, $url]);
        echo json_encode(['success' => true]);
        exit;
    }

    if ($action === 'delete') {
        $feed_id = $_POST['feed_id'] ?? 0;
        $stmt = db()->prepare("DELETE FROM channel_rss_feeds WHERE id = ? AND channel_id = ?");
        $stmt->execute([$feed_id, $channel_id]);
        echo json_encode(['success' => true]);
        exit;
    }

    if ($action === 'sync') {
        // Fetch feeds for this channel
        $stmt = db()->prepare("SELECT * FROM channel_rss_feeds WHERE channel_id = ?");
        $stmt->execute([$channel_id]);
        $feeds = $stmt->fetchAll();

        $new_items_count = 0;
        foreach ($feeds as $feed) {
            $rss_content = @file_get_contents($feed['url']);
            if (!$rss_content) continue;

            $xml = @simplexml_load_string($rss_content);
            if (!$xml) continue;

            $items = [];
            if (isset($xml->channel->item)) { // RSS 2.0
                $items = $xml->channel->item;
            } elseif (isset($xml->entry)) { // Atom
                $items = $xml->entry;
            }

            foreach ($items as $item) {
                $guid = (string)($item->guid ?? ($item->id ?? $item->link));
                $title = (string)$item->title;
                $link = (string)($item->link['href'] ?? $item->link);
                $description = strip_tags((string)($item->description ?? $item->summary));
                
                // Check if already exists
                $stmt_check = db()->prepare("SELECT id FROM messages WHERE channel_id = ? AND rss_guid = ?");
                $stmt_check->execute([$channel_id, $guid]);
                if ($stmt_check->fetch()) continue;

                // Insert as message from a special "RSS Bot" user or system
                // Let's find or create an RSS Bot user
                $stmt_bot = db()->prepare("SELECT id FROM users WHERE username = 'RSS Bot' AND is_bot = 1");
                $stmt_bot->execute();
                $bot = $stmt_bot->fetch();
                if (!$bot) {
                    $stmt_create_bot = db()->prepare("INSERT INTO users (username, is_bot, status) VALUES ('RSS Bot', 1, 'online')");
                    $stmt_create_bot->execute();
                    $bot_id = db()->lastInsertId();
                } else {
                    $bot_id = $bot['id'];
                }

                $content = "**" . $title . "**\n" . $description . "\n" . $link;
                
                // For announcements, we might want to use metadata for a rich embed
                $metadata = json_encode([
                    'title' => $title,
                    'description' => mb_substr($description, 0, 200) . (mb_strlen($description) > 200 ? '...' : ''),
                    'url' => $link,
                    'site_name' => parse_url($feed['url'], PHP_URL_HOST)
                ]);

                $stmt_msg = db()->prepare("INSERT INTO messages (channel_id, user_id, content, metadata, rss_guid) VALUES (?, ?, ?, ?, ?)");
                $stmt_msg->execute([$channel_id, $bot_id, $content, $metadata, $guid]);
                $new_items_count++;
            }

            $stmt_update_feed = db()->prepare("UPDATE channel_rss_feeds SET last_fetched_at = CURRENT_TIMESTAMP WHERE id = ?");
            $stmt_update_feed->execute([$feed['id']]);
        }

        echo json_encode(['success' => true, 'new_items' => $new_items_count]);
        exit;
    }
} else {
    // GET: List feeds
    $channel_id = $_GET['channel_id'] ?? 0;
    $stmt = db()->prepare("SELECT * FROM channel_rss_feeds WHERE channel_id = ? ORDER BY created_at DESC");
    $stmt->execute([$channel_id]);
    echo json_encode(['success' => true, 'feeds' => $stmt->fetchAll()]);
    exit;
}
