<?php

declare(strict_types=1);

use App\Models\Artwork;
use App\Models\ArtworkComment;
use App\Models\DashboardPreference;
use App\Models\Notification;
use App\Models\User;
use Illuminate\Support\Facades\DB;

it('embeds dashboard overview counts for the authenticated user', function () {
    $user = User::factory()->create([
        'email_verified_at' => now(),
    ]);
    $follower = User::factory()->create();
    $followed = User::factory()->create();
    $commenter = User::factory()->create();

    $artwork = Artwork::factory()->create([
        'user_id' => $user->id,
        'is_approved' => true,
        'is_public' => true,
        'published_at' => now()->subDay(),
    ]);

    DB::table('user_followers')->insert([
        [
            'user_id' => $user->id,
            'follower_id' => $follower->id,
            'created_at' => now(),
        ],
        [
            'user_id' => $followed->id,
            'follower_id' => $user->id,
            'created_at' => now(),
        ],
    ]);

    DB::table('artwork_favourites')->insert([
        'user_id' => $user->id,
        'artwork_id' => $artwork->id,
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    Notification::query()->create([
        'user_id' => $user->id,
        'type' => 'comment',
        'data' => [
            'message' => 'Unread dashboard notification',
            'url' => '/dashboard/notifications',
        ],
        'read_at' => null,
    ]);

    ArtworkComment::factory()->create([
        'artwork_id' => $artwork->id,
        'user_id' => $commenter->id,
        'content' => 'Unread dashboard comment',
        'raw_content' => 'Unread dashboard comment',
        'rendered_content' => '<p>Unread dashboard comment</p>',
        'is_approved' => true,
    ]);

    $response = $this->actingAs($user)->get('/dashboard');

    $response->assertOk()->assertSee('data-overview=', false);

    $overview = $response->viewData('dashboard_overview');
    $preferences = $response->viewData('dashboard_preferences');

    expect($overview)->toMatchArray([
        'artworks' => 1,
        'stories' => 0,
        'followers' => 1,
        'following' => 1,
        'favorites' => 1,
        'notifications' => $user->unreadNotifications()->count(),
        'received_comments' => 1,
    ]);

    expect($preferences)->toMatchArray([
        'pinned_spaces' => [],
    ]);
});

it('embeds saved pinned dashboard spaces for the authenticated user', function () {
    $user = User::factory()->create([
        'email_verified_at' => now(),
    ]);

    DashboardPreference::query()->create([
        'user_id' => $user->id,
        'pinned_spaces' => [
            '/dashboard/notifications',
            '/studio',
        ],
    ]);

    $response = $this->actingAs($user)->get('/dashboard');

    $response->assertOk();

    expect($response->viewData('dashboard_preferences'))->toMatchArray([
        'pinned_spaces' => [
            '/dashboard/notifications',
            '/studio',
        ],
    ]);
});
