<?php

declare(strict_types=1);

use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Models\Tag;
use App\Models\User;

uses(RefreshDatabase::class);

it('renders seo tags on public blade pages', function (): void {
    $response = $this->get('/');

    $response->assertOk();

    $html = $response->getContent();
    $this->assertNotFalse($html);
    $this->assertStringContainsString('<title>', $html);
    $this->assertStringContainsString('meta name="description"', $html);
    $this->assertStringContainsString('meta property="og:title"', $html);
    $this->assertStringContainsString('meta name="twitter:card"', $html);
});

it('renders seo tags on auth blade pages', function (): void {
    $response = $this->get('/login');

    $response->assertOk();

    $html = $response->getContent();
    $this->assertNotFalse($html);
    $this->assertStringContainsString('<title>', $html);
    $this->assertStringContainsString('meta name="description"', $html);
    $this->assertStringContainsString('meta property="og:title"', $html);
    $this->assertStringContainsString('meta name="robots" content="noindex,nofollow"', $html);
});

it('renders canonical tags on public discovery and creator pages', function (string $url, string $routeName): void {
    $response = $this->get($url);

    $response->assertOk();

    $html = $response->getContent();
    $this->assertNotFalse($html);
    $this->assertStringContainsString('<link rel="canonical" href="' . route($routeName) . '" />', $html);
})->with([
    'discover on this day' => ['/discover/on-this-day', 'discover.on-this-day'],
    'discover rising' => ['/discover/rising', 'discover.rising'],
    'discover top rated' => ['/discover/top-rated', 'discover.top-rated'],
    'discover most downloaded' => ['/discover/most-downloaded', 'discover.most-downloaded'],
    'discover fresh' => ['/discover/fresh', 'discover.fresh'],
    'discover trending' => ['/discover/trending', 'discover.trending'],
    'top creators' => ['/creators/top', 'creators.top'],
]);

it('renders a canonical tag on the register page', function (): void {
    $response = $this->get('/register');

    $response->assertOk();

    $html = $response->getContent();
    $this->assertNotFalse($html);
    $this->assertStringContainsString('<link rel="canonical" href="' . route('register') . '" />', $html);
});

it('renders canonical atom links in rss feed headers', function (): void {
    Tag::factory()->create([
        'name' => 'Abstract Composition',
        'slug' => 'abstract-composition',
        'is_active' => true,
    ]);

    $this->get('/rss')
        ->assertOk()
        ->assertSee('<atom:link href="' . route('rss.global') . '" rel="canonical" />', false);

    $this->get('/rss/tag/abstract-composition')
        ->assertOk()
        ->assertSee('<atom:link href="' . route('rss.tag', ['slug' => 'abstract-composition']) . '" rel="canonical" />', false);
});

it('renders seo tags on upload and studio pages', function (): void {
    $user = User::factory()->create();

    $uploadResponse = $this->actingAs($user)->get('/upload');
    $uploadResponse->assertOk();

    $uploadHtml = $uploadResponse->getContent();
    $this->assertNotFalse($uploadHtml);
    $this->assertStringContainsString('<title>Upload Artwork — Creator Studio</title>', $uploadHtml);
    $this->assertStringContainsString('meta name="description"', $uploadHtml);
    $this->assertStringContainsString('meta property="og:title"', $uploadHtml);
    $this->assertStringContainsString('meta name="robots" content="noindex, nofollow"', $uploadHtml);

    $studioResponse = $this->actingAs($user)->get('/studio/artworks');
    $studioResponse->assertOk();

    $studioHtml = $studioResponse->getContent();
    $this->assertNotFalse($studioHtml);
    $this->assertStringContainsString('Creator Studio', $studioHtml);
    $this->assertStringContainsString('meta name="description"', $studioHtml);
    $this->assertStringContainsString('meta property="og:title"', $studioHtml);
    $this->assertStringContainsString('meta name="robots" content="noindex, nofollow"', $studioHtml);
});
