<?php

namespace Tests\Feature;

use App\Models\User;
use Database\Seeders\DatabaseSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\TestCase;

class AdminPanelTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed(DatabaseSeeder::class);
    }

    public function test_guest_is_redirected_to_login(): void
    {
        $response = $this->get('/admin');

        $response->assertRedirect('/admin/login');
    }

    public function test_admin_can_login_with_seeded_credentials(): void
    {
        $attempt = auth()->attempt([
            'usr_email' => 'catherine@obvious-way.com',
            'password' => 'password',
        ]);

        $this->assertTrue($attempt);
    }

    public function test_authenticated_admin_can_access_dashboard(): void
    {
        $this->actingAs(User::first());

        $response = $this->get('/admin');

        $response->assertStatus(200);
    }

    #[DataProvider('resourceUrlProvider')]
    public function test_authenticated_admin_can_access_resource(string $url): void
    {
        $this->actingAs(User::first());

        $response = $this->get($url);

        $response->assertStatus(200);
    }

    public static function resourceUrlProvider(): array
    {
        return [
            'stats' => ['/admin/stats'],
            'territoires' => ['/admin/territoires'],
            'service-phases' => ['/admin/service-phases'],
            'programme-sessions' => ['/admin/programme-sessions'],
            'articles' => ['/admin/articles'],
            'temoignages' => ['/admin/temoignages'],
            'soumissions' => ['/admin/soumissions'],
            'reglage-hero' => ['/admin/reglage-hero'],
            'reglage-about' => ['/admin/reglage-about'],
            'reglage-contact' => ['/admin/reglage-contact'],
            'reglage-footer' => ['/admin/reglage-footer'],
        ];
    }
}
