<?php

use App\Models\ContactSubmission;
use App\Models\User;

test('guests are redirected to the login page for all admin pages', function (string $routeName) {
    $contactSubmission = ContactSubmission::factory()->create();

    $route = $routeName === 'admin.contacts.show'
        ? route($routeName, $contactSubmission)
        : route($routeName);

    $this->get($route)->assertRedirect(route('login'));
})->with([
    'admin.contacts',
    'admin.contacts.show',
    'admin.subscriptions',
]);

test('non admin users receive a 403 for all admin pages', function (string $routeName) {
    $user = User::factory()->create(['is_admin' => false]);
    $contactSubmission = ContactSubmission::factory()->create();

    $route = $routeName === 'admin.contacts.show'
        ? route($routeName, $contactSubmission)
        : route($routeName);

    $this->actingAs($user)->get($route)->assertForbidden();
})->with([
    'admin.contacts',
    'admin.contacts.show',
    'admin.subscriptions',
]);

test('admins can access all admin pages', function (string $routeName) {
    $admin = User::factory()->admin()->create();
    $contactSubmission = ContactSubmission::factory()->create();

    $route = $routeName === 'admin.contacts.show'
        ? route($routeName, $contactSubmission)
        : route($routeName);

    $this->actingAs($admin)->get($route)->assertOk();
})->with([
    'admin.contacts',
    'admin.contacts.show',
    'admin.subscriptions',
]);

test('a non admin cannot reach the contact detail page', function () {
    $user = User::factory()->create(['is_admin' => false]);
    $contactSubmission = ContactSubmission::factory()->create();

    $this->actingAs($user)
        ->get(route('admin.contacts.show', $contactSubmission))
        ->assertForbidden();
});
