<?php

namespace Tests\Feature;

use App\Models\ContactMessage;
use Database\Seeders\InitialSiteContentSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\TestCase;

class PublicPagesTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();

        Cache::flush();
        $this->seed(InitialSiteContentSeeder::class);
    }

    /**
     * @return array<string, array{0: string}>
     */
    public static function publicRoutes(): array
    {
        return [
            'home' => ['/'],
            'about' => ['/a-propos'],
            'services' => ['/services'],
            'gallery' => ['/galerie'],
            'contact' => ['/contact'],
            'admin.login' => ['/admin/login'],
        ];
    }

    #[DataProvider('publicRoutes')]
    public function test_public_route_returns_success(string $uri): void
    {
        $this->get($uri)->assertStatus(200);
    }

    public function test_official_logo_asset_exists(): void
    {
        $this->assertFileExists(public_path('assets/images/branding/logo-nh-trans.jpg'));
    }

    public function test_no_register_route_exists(): void
    {
        $this->assertFalse(Route::has('register'));
        $this->get('/register')->assertStatus(404);
    }

    public function test_admin_login_post_route_exists_and_validates(): void
    {
        $this->post('/admin/login', [])->assertSessionHasErrors(['email', 'password']);
    }

    public function test_contact_post_route_exists_and_validates(): void
    {
        $this->post('/contact', [])->assertSessionHasErrors(['name', 'email', 'subject', 'message', 'consent']);
    }

    public function test_contact_post_persists_valid_message(): void
    {
        $this->post('/contact', [
            'name' => 'Client Public',
            'email' => 'public@example.test',
            'subject' => 'Demande publique',
            'message' => 'Message public assez long pour etre accepte par la validation.',
            'consent' => '1',
            'website' => '',
        ])->assertRedirect();

        $this->assertSame(1, ContactMessage::count());
    }

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

        $response->assertOk();
        $response->assertSee('Accueil');
        $response->assertSee('Services');
        $response->assertSee('Galerie');
        $response->assertSee('Contact');
        $response->assertSee('Connexion administrateur');
        $response->assertDontSee('Equipe', false);
    }

    public function test_home_page_has_distinct_partners_and_clients_sections(): void
    {
        $this->get('/')->assertOk()->assertSeeInOrder(['Nos partenaires', 'Ils nous font confiance']);
    }

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

        $response->assertOk();
        foreach (config('nh_trans.services') as $service) {
            $response->assertSee($service['title']);
        }
    }

    public function test_gallery_shows_institutional_empty_state_when_no_items(): void
    {
        $this->get('/galerie')
            ->assertOk()
            ->assertSee('Réalisations à venir');
    }

    public function test_pages_expose_specific_title_and_single_h1(): void
    {
        foreach (self::publicRoutes() as [$uri]) {
            $content = $this->get($uri)->getContent();

            $this->assertSame(1, substr_count($content, '<h1'), "La page {$uri} doit contenir un unique <h1>.");
            $this->assertMatchesRegularExpression('/<title>[^<]{5,}<\/title>/', $content);
        }
    }
}
