<?php

declare(strict_types=1);

use App\Models\User;
use App\Services\ArtworkService;
use App\Services\HomepageService;
use Illuminate\Pagination\LengthAwarePaginator;

beforeEach(function () {
    // Use null Scout driver — Meilisearch calls return empty results gracefully
    config(['scout.driver' => 'null']);

    // ArtworkService is not final so it can be mocked
    $artworksMock = Mockery::mock(ArtworkService::class);
    $artworksMock->shouldReceive('getFeaturedArtworks')
        ->andReturn(new LengthAwarePaginator(collect(), 0, 1))
        ->byDefault();
    app()->instance(ArtworkService::class, $artworksMock);
});

// ── Route integration ─────────────────────────────────────────────────────────

it('home page renders 200 for guests', function () {
    $this->get('/')->assertStatus(200);
});

it('home page renders 200 for authenticated users', function () {
    $this->actingAs(User::factory()->create())
        ->get('/')
        ->assertStatus(200);
});

// ── HomepageService section shape ─────────────────────────────────────────────

it('guest homepage has expected sections but no from_following', function () {
    $sections = app(HomepageService::class)->all();

    expect($sections)->toHaveKeys(['hero', 'trending', 'fresh', 'tags', 'creators', 'news']);
    expect($sections)->not->toHaveKey('from_following');
    expect($sections)->not->toHaveKey('by_tags');
    expect($sections)->not->toHaveKey('by_categories');
});

it('authenticated homepage contains all personalised sections', function () {
    $user     = User::factory()->create();
    $sections = app(HomepageService::class)->allForUser($user);

    expect($sections)->toHaveKeys([
        'hero',
        'from_following',
        'trending',
        'by_tags',
        'by_categories',
        'tags',
        'creators',
        'news',
        'preferences',
    ]);
});

it('preferences section exposes top_tags and top_categories arrays', function () {
    $user     = User::factory()->create();
    $sections = app(HomepageService::class)->allForUser($user);

    expect($sections['preferences'])->toHaveKeys(['top_tags', 'top_categories']);
    expect($sections['preferences']['top_tags'])->toBeArray();
    expect($sections['preferences']['top_categories'])->toBeArray();
});

it('guest and auth homepages have different key sets', function () {
    $user = User::factory()->create();

    $guest = array_keys(app(HomepageService::class)->all());
    $auth  = array_keys(app(HomepageService::class)->allForUser($user));

    expect($guest)->not->toEqual($auth);
    expect(in_array('from_following', $auth))->toBeTrue();
    expect(in_array('from_following', $guest))->toBeFalse();
});
