<?php

/**
 * @return array<int, string>
 */
function extractLocalImagePaths(string $html): array
{
    preg_match_all('/\/images\/[^"\'()\s]+/', $html, $matches);

    return array_unique($matches[0]);
}

test('services page returns ok and contains no unsplash hotlinks', function () {
    $response = $this->get(route('services'));

    $response->assertOk();
    $response->assertDontSee('images.unsplash.com');
});

test('contact page returns ok and contains no unsplash hotlinks', function () {
    $response = $this->get(route('contact'));

    $response->assertOk();
    $response->assertDontSee('images.unsplash.com');
});

test('services page showcase grid renders sixteen local images that all exist on disk', function () {
    $response = $this->get(route('services'));

    $response->assertOk();

    $html = $response->getContent();

    expect(substr_count($html, 'showcase-item'))->toBe(16);

    $paths = extractLocalImagePaths($html);
    $paths = array_values(array_filter($paths, fn (string $path): bool => str_contains($path, '/images/services/')));

    expect($paths)->toHaveCount(16);

    expect($paths)->not->toBeEmpty();

    foreach ($paths as $path) {
        expect(file_exists(public_path($path)))->toBeTrue("Missing local image file for path: {$path}");
    }
});

test('contact page hero background references a local image that exists on disk', function () {
    $response = $this->get(route('contact'));

    $response->assertOk();

    $html = $response->getContent();

    $paths = extractLocalImagePaths($html);

    expect($paths)->not->toBeEmpty();

    foreach ($paths as $path) {
        expect(file_exists(public_path($path)))->toBeTrue("Missing local image file for path: {$path}");
    }
});
