<?php

namespace App\Actions;

use App\Models\Article;
use App\Models\Feed;
use App\Services\Log\LogSaver;
use Illuminate\Support\Collection;

class FetchFeedArticlesAction
{
    public function __construct(
        private LogSaver $logSaver,
        private FetchRssArticlesAction $fetchRssArticles,
        private FetchWebsiteArticlesAction $fetchWebsiteArticles,
    ) {}

    /**
     * @return Collection<int, Article>
     */
    public function execute(Feed $feed): Collection
    {
        if ($feed->type === 'rss') {
            return $this->fetchRssArticles->execute($feed);
        } elseif ($feed->type === 'website') {
            return $this->fetchWebsiteArticles->execute($feed);
        }

        $this->logSaver->warning('Unsupported feed type', null, [
            'feed_id' => $feed->id,
            'feed_type' => $feed->type,
        ]);

        return collect();
    }
}
