<?php

namespace Tests\Feature;

use App\Services\Meta\GraphApi;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;

class CreativeImageRefreshTest extends TestCase
{
    use RefreshDatabase;

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

        config([
            'platforms.meta.token' => 'test-token',
            'platforms.meta.version' => 'v21.0',
            'platforms.guardrails.allowed_ad_accounts' => ['act_42'],
        ]);
    }

    public function test_graph_returns_the_current_creative_image(): void
    {
        Http::fake([
            'https://graph.facebook.com/*' => Http::response([
                'image_url' => 'https://scontent.xx.fbcdn.net/v/fresh.jpg',
                'thumbnail_url' => 'https://scontent.xx.fbcdn.net/v/thumb.jpg',
            ]),
        ]);

        $url = app(GraphApi::class)->creativeImageUrl('925887686918149', 'act_42');

        $this->assertSame('https://scontent.xx.fbcdn.net/v/fresh.jpg', $url);
        Http::assertSent(function ($request): bool {
            return str_contains($request->url(), '/v21.0/925887686918149')
                && str_contains(urldecode($request->url()), 'image_url');
        });
    }

    public function test_a_creative_id_that_is_not_numeric_is_not_requested(): void
    {
        Http::fake();

        $this->assertNull(app(GraphApi::class)->creativeImageUrl('not-an-id', 'act_42'));
        Http::assertNothingSent();
    }
}
