<?php

use App\Models\Client;
use App\Models\Declaration;
use App\Models\User;
use App\Models\Workspace;
use App\Notifications\DeclarationMentionNotification;
use Illuminate\Support\Facades\Notification;

function setupMentionScenario(string $role = 'owner'): array
{
    $sender = User::factory()->create();
    $target = User::factory()->create();
    $workspace = Workspace::factory()->create();
    $workspace->users()->attach($sender, ['role' => $role]);
    $workspace->users()->attach($target, ['role' => 'worker']);

    $client = Client::factory()->create(['workspace_id' => $workspace->id]);
    $declaration = Declaration::factory()->create([
        'workspace_id' => $workspace->id,
        'client_id' => $client->id,
    ]);

    return [$sender, $target, $workspace, $declaration];
}

test('owner can mention a workspace user', function () {
    Notification::fake();
    [$sender, $target, $workspace, $declaration] = setupMentionScenario('owner');
    session(['current_workspace_id' => $workspace->id]);

    $response = $this->actingAs($sender)->post(route('declarations.mentions.store', $declaration), [
        'user_id' => $target->id,
        'message' => 'Please check this declaration.',
    ]);

    $response->assertRedirect();
    Notification::assertSentTo($target, DeclarationMentionNotification::class);
});

test('manager can mention a workspace user', function () {
    Notification::fake();
    [$sender, $target, $workspace, $declaration] = setupMentionScenario('manager');
    session(['current_workspace_id' => $workspace->id]);

    $response = $this->actingAs($sender)->post(route('declarations.mentions.store', $declaration), [
        'user_id' => $target->id,
        'message' => 'Please check this declaration.',
    ]);

    $response->assertRedirect();
    Notification::assertSentTo($target, DeclarationMentionNotification::class);
});

test('worker cannot mention a workspace user', function () {
    Notification::fake();
    [$sender, $target, $workspace, $declaration] = setupMentionScenario('worker');
    session(['current_workspace_id' => $workspace->id]);

    $response = $this->actingAs($sender)->post(route('declarations.mentions.store', $declaration), [
        'user_id' => $target->id,
        'message' => 'Please check this declaration.',
    ]);

    $response->assertForbidden();
    Notification::assertNothingSent();
});

test('cannot mention user from another workspace', function () {
    Notification::fake();
    [$sender, , $workspace, $declaration] = setupMentionScenario('owner');
    $outsider = User::factory()->create();
    session(['current_workspace_id' => $workspace->id]);

    $response = $this->actingAs($sender)->post(route('declarations.mentions.store', $declaration), [
        'user_id' => $outsider->id,
        'message' => 'Hello',
    ]);

    $response->assertSessionHasErrors('user_id');
    Notification::assertNothingSent();
});

test('notification is persisted in database', function () {
    [$sender, $target, $workspace, $declaration] = setupMentionScenario('owner');
    session(['current_workspace_id' => $workspace->id]);

    $this->actingAs($sender)->post(route('declarations.mentions.store', $declaration), [
        'user_id' => $target->id,
        'message' => 'Check this.',
    ]);

    expect($target->notifications()->count())->toBe(1);
    $notif = $target->notifications()->first();
    expect($notif->data['declaration_id'])->toBe($declaration->id);
    expect($notif->data['message'])->toBe('Check this.');
});
