<?php

declare(strict_types=1);

namespace App\Http\Controllers\RSS;

use App\Http\Controllers\Controller;
use App\Models\Artwork;
use App\Services\RSS\RSSFeedBuilder;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache;

/**
 * DiscoverFeedController
 *
 * Powers the /rss/discover/* feeds (spec §3.2).
 *
 * GET /rss/discover            → fresh/latest (default)
 * GET /rss/discover/trending   → trending by trending_score_7d
 * GET /rss/discover/fresh      → latest published
 * GET /rss/discover/rising     → rising by heat_score
 */
final class DiscoverFeedController extends Controller
{
    public function __construct(private readonly RSSFeedBuilder $builder) {}

    /** /rss/discover → redirect to fresh */
    public function index(): Response
    {
        return $this->fresh();
    }

    /** /rss/discover/trending */
    public function trending(): Response
    {
        $feedUrl  = url('/rss/discover/trending');
        $artworks = Cache::remember('rss:discover:trending', 600, fn () =>
            Artwork::public()->published()
                ->with(['user:id,username', 'categories:id,name,slug,content_type_id'])
                ->leftJoin('artwork_stats', 'artwork_stats.artwork_id', '=', 'artworks.id')
                ->orderByDesc('artwork_stats.trending_score_7d')
                ->orderByDesc('artworks.published_at')
                ->select('artworks.*')
                ->limit(RSSFeedBuilder::FEED_LIMIT)
                ->get()
        );

        return $this->builder->buildFromArtworks(
            'Trending Artworks',
            'The most-viewed and trending artworks on Skinbase over the past 7 days.',
            $feedUrl,
            $artworks,
        );
    }

    /** /rss/discover/fresh */
    public function fresh(): Response
    {
        $feedUrl  = url('/rss/discover/fresh');
        $artworks = Cache::remember('rss:discover:fresh', 300, fn () =>
            Artwork::public()->published()
                ->with(['user:id,username', 'categories:id,name,slug,content_type_id'])
                ->latest('published_at')
                ->limit(RSSFeedBuilder::FEED_LIMIT)
                ->get()
        );

        return $this->builder->buildFromArtworks(
            'Fresh Uploads',
            'The latest artworks just published on Skinbase.',
            $feedUrl,
            $artworks,
        );
    }

    /** /rss/discover/rising */
    public function rising(): Response
    {
        $feedUrl  = url('/rss/discover/rising');
        $artworks = Cache::remember('rss:discover:rising', 600, fn () =>
            Artwork::public()->published()
                ->with(['user:id,username', 'categories:id,name,slug,content_type_id'])
                ->leftJoin('artwork_stats', 'artwork_stats.artwork_id', '=', 'artworks.id')
                ->orderByDesc('artwork_stats.heat_score')
                ->orderByDesc('artworks.published_at')
                ->select('artworks.*')
                ->limit(RSSFeedBuilder::FEED_LIMIT)
                ->get()
        );

        return $this->builder->buildFromArtworks(
            'Rising Artworks',
            'Fastest-growing artworks gaining momentum on Skinbase right now.',
            $feedUrl,
            $artworks,
        );
    }
}
