<?php

use App\Models\CtsSyncLog;
use App\Models\User;

test('shared props include auth user with expected fields when authenticated', function () {
    $user = User::factory()->create([
        'name' => 'Max Mustermann',
        'email' => 'max@example.de',
        'avatar' => 'https://example.de/avatar.jpg',
    ]);

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

    $response->assertOk();
    $response->assertInertia(
        fn ($page) => $page
            ->has('auth.user')
            ->where('auth.user.id', $user->id)
            ->where('auth.user.name', 'Max Mustermann')
            ->where('auth.user.email', 'max@example.de')
            ->where('auth.user.avatar', 'https://example.de/avatar.jpg')
            ->missing('auth.user.password')
            ->missing('auth.user.remember_token')
    );
});

test('shared props include null auth user when not logged in', function () {
    // Guest accessing /login should get Inertia response with null auth user
    $response = $this->get('/login');

    $response->assertOk();
    $response->assertInertia(
        fn ($page) => $page
            ->where('auth.user', null)
    );
});

test('shared props include flash success message', function () {
    $user = User::factory()->create();

    $response = $this->actingAs($user)
        ->withSession(['success' => 'Erfolgreich gespeichert!'])
        ->get('/dashboard');

    $response->assertOk();
    $response->assertInertia(
        fn ($page) => $page
            ->has('flash')
            ->where('flash.success', 'Erfolgreich gespeichert!')
    );
});

test('shared props include flash error message', function () {
    $user = User::factory()->create();

    $response = $this->actingAs($user)
        ->withSession(['error' => 'Ein Fehler ist aufgetreten.'])
        ->get('/dashboard');

    $response->assertOk();
    $response->assertInertia(
        fn ($page) => $page
            ->has('flash')
            ->where('flash.error', 'Ein Fehler ist aufgetreten.')
    );
});

test('shared props include last_synced_at from latest sync log', function () {
    $user = User::factory()->create();

    $syncTime = now()->subMinutes(5)->startOfSecond();
    CtsSyncLog::create([
        'synced_at' => $syncTime,
        'events_count' => 10,
        'songs_count' => 5,
        'status' => 'success',
    ]);

    // Create an older one to confirm "latest" is used
    CtsSyncLog::create([
        'synced_at' => now()->subHours(2),
        'events_count' => 8,
        'songs_count' => 3,
        'status' => 'success',
    ]);

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

    $response->assertOk();
    $response->assertInertia(
        fn ($page) => $page
            ->has('last_synced_at')
            ->where('last_synced_at', $syncTime->toJSON())
    );
});

test('shared props include null last_synced_at when no sync log exists', function () {
    $user = User::factory()->create();

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

    $response->assertOk();
    $response->assertInertia(
        fn ($page) => $page
            ->where('last_synced_at', null)
    );
});

test('shared props include app_name from config', function () {
    $user = User::factory()->create();

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

    $response->assertOk();
    $response->assertInertia(
        fn ($page) => $page
            ->has('app_name')
    );
});
