<?php

use App\Models\Artwork;
use App\Models\ArtworkMetricSnapshotHourly;
use Illuminate\Support\Facades\Cache;

beforeEach(function (): void {
    Cache::flush();
});

it('GET /rss/discover/rising returns 200', function (): void {
    $this->get('/rss/discover/rising')
        ->assertOk();
});

it('uses the low-signal fallback ordering in the RSS rising feed', function (): void {
    $olderActiveArtwork = Artwork::withoutEvents(fn () => Artwork::factory()->create([
        'title' => 'RSS Older Active Artwork',
        'is_public' => true,
        'is_approved' => true,
        'published_at' => now()->subDays(3),
        'created_at' => now()->subDays(3),
    ]));

    $newerQuietArtwork = Artwork::withoutEvents(fn () => Artwork::factory()->create([
        'title' => 'RSS Newer Quiet Artwork',
        'is_public' => true,
        'is_approved' => true,
        'published_at' => now()->subHour(),
        'created_at' => now()->subHour(),
    ]));

    $previousHour = now()->startOfHour()->subHour();
    $currentHour = now()->startOfHour();

    ArtworkMetricSnapshotHourly::create([
        'artwork_id' => $olderActiveArtwork->id,
        'bucket_hour' => $previousHour,
        'views_count' => 10,
        'downloads_count' => 0,
        'favourites_count' => 0,
        'comments_count' => 0,
        'shares_count' => 0,
    ]);

    ArtworkMetricSnapshotHourly::create([
        'artwork_id' => $olderActiveArtwork->id,
        'bucket_hour' => $currentHour,
        'views_count' => 45,
        'downloads_count' => 2,
        'favourites_count' => 1,
        'comments_count' => 0,
        'shares_count' => 0,
    ]);

    $response = $this->get('/rss/discover/rising');

    $response->assertOk();
    $response->assertSeeInOrder([
        'RSS Older Active Artwork',
        'RSS Newer Quiet Artwork',
    ], false);

    expect($newerQuietArtwork->id)->not->toBe($olderActiveArtwork->id);
});