<?php

declare(strict_types=1);

use App\Models\User;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use cPad\Plugins\News\Models\NewsArticle;
use cPad\Plugins\News\Models\NewsCategory;
use cPad\Plugins\News\Models\NewsTag;

function newsCategory(array $attributes = []): NewsCategory
{
    return NewsCategory::query()->create(array_merge([
        'name' => 'Platform Updates',
        'slug' => 'platform-updates-' . Str::lower(Str::random(6)),
        'description' => 'Skinbase platform updates.',
        'position' => 0,
        'is_active' => true,
    ], $attributes));
}

function newsTag(array $attributes = []): NewsTag
{
    return NewsTag::query()->create(array_merge([
        'name' => 'Launch',
        'slug' => 'launch-' . Str::lower(Str::random(6)),
    ], $attributes));
}

function publishedNewsArticle(User $author, NewsCategory $category, array $attributes = []): NewsArticle
{
    $article = NewsArticle::query()->create(array_merge([
        'title' => 'Nova newsroom launch',
        'slug' => 'nova-newsroom-launch-' . Str::lower(Str::random(6)),
        'excerpt' => 'A first-party newsroom for launches, tutorials, and editorials.',
        'content' => "# Nova newsroom\n\nThis is the first newsroom article.",
        'author_id' => $author->id,
        'category_id' => $category->id,
        'type' => NewsArticle::TYPE_PLATFORM_UPDATE,
        'status' => 'published',
        'editorial_status' => NewsArticle::EDITORIAL_STATUS_PUBLISHED,
        'published_at' => now()->subHour(),
        'is_featured' => true,
        'is_pinned' => true,
    ], $attributes));

    return $article;
}

it('renders published news across public discovery routes', function (): void {
    $author = User::factory()->create([
        'username' => 'newsauthor',
        'name' => 'News Author',
    ]);
    $category = newsCategory([
        'name' => 'Announcements',
        'slug' => 'announcements',
    ]);
    $tag = newsTag([
        'name' => 'Release',
        'slug' => 'release',
    ]);
    $article = publishedNewsArticle($author, $category, [
        'title' => 'Skinbase Nova Newsroom',
        'slug' => 'skinbase-nova-newsroom',
    ]);
    $article->tags()->sync([$tag->id]);

    publishedNewsArticle($author, $category, [
        'title' => 'Hidden Draft',
        'slug' => 'hidden-draft',
        'status' => 'draft',
        'editorial_status' => NewsArticle::EDITORIAL_STATUS_DRAFT,
        'published_at' => null,
        'is_featured' => false,
        'is_pinned' => false,
    ]);

    $this->get(route('news.index'))
        ->assertOk()
        ->assertSee('Skinbase Nova Newsroom')
        ->assertDontSee('Hidden Draft');

    $this->get(route('news.show', ['slug' => $article->slug]))
        ->assertOk()
        ->assertSee('Skinbase Nova Newsroom')
        ->assertSee('News Author');

    $this->get(route('news.category', ['slug' => $category->slug]))
        ->assertOk()
        ->assertSee('Skinbase Nova Newsroom');

    $this->get(route('news.tag', ['slug' => $tag->slug]))
        ->assertOk()
        ->assertSee('Skinbase Nova Newsroom');

    $this->get(route('news.archive', ['year' => $article->published_at->year, 'month' => $article->published_at->month]))
        ->assertOk()
        ->assertSee('Skinbase Nova Newsroom');

    $this->get(route('news.author', ['username' => $author->username]))
        ->assertOk()
        ->assertSee('Skinbase Nova Newsroom');
});