<?php

namespace Tests\Feature;

use App\Agent\Answers;
use App\Agent\Workspace;
use App\Models\Campaign;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;

/**
 * A click on a Google question is applied here, not read back out of a sentence.
 *
 * Answers only ever looked in the `campaign` namespace, so every Google
 * question had to round trip through the model: the click put a sentence in the
 * composer and the model had to turn it back into an argument. That is the exact
 * round trip this class was written to remove for Meta, where it was silently
 * dropping answers and re-asking questions that had already been answered.
 */
class GoogleAnswersApplyTest extends TestCase
{
    use RefreshDatabase;

    private User $user;

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

        $this->user = User::factory()->create();
        $this->actingAs($this->user);

        config([
            'platforms.guardrails.allowed_ad_accounts' => ['1234567890', '9876543210'],
            'services.google_ads.refresh_token' => 'refresh-token',
            'services.google_ads.customer_id' => '123-456-7890',
        ]);
    }

    /**
     * Google's conversion action is applied without the model reading it back.
     *
     * There is no customer_id entry any more. Choosing a Google account was a
     * question with one answer once Google became one common internal account,
     * so the tool that owned it is gone and so is the mapping.
     */
    public function test_answers_owns_the_google_conversion_action_question(): void
    {
        $answers = app(Answers::class);

        $this->assertTrue($answers->handles('conversion_action_resource_name'));
        $this->assertFalse($answers->handles('customer_id'));
    }

    /**
     * A conversion action lands on the campaign's pixel_id.
     *
     * Google's equivalent of a pixel, stored in the same column, so the publish
     * gate and the panel ask one question whatever the platform.
     */
    public function test_clicking_a_conversion_action_stores_it_on_the_campaign(): void
    {
        $resource = 'customers/1234567890/conversionActions/555';

        Http::fake([
            'oauth2.googleapis.com/*' => Http::response(['access_token' => 'token', 'expires_in' => 3600]),
            'googleads.googleapis.com/*' => Http::response([[
                'results' => [[
                    'conversionAction' => [
                        'resourceName' => $resource,
                        'name' => 'Purchase',
                        'category' => 'PURCHASE',
                        'type' => 'WEBPAGE',
                    ],
                ]],
            ]]),
        ]);

        $campaign = Campaign::create([
            'user_id' => $this->user->id,
            'name' => 'Google one',
            'platform' => 'google',
            'status' => 'draft',
            'ad_account_id' => '1234567890',
            'objective' => 'SALES',
        ]);

        app(Workspace::class)->focusOn($campaign);

        app(Answers::class)->applyAll([
            ['field' => 'conversion_action_resource_name', 'value' => $resource],
        ]);

        $this->assertSame($resource, $campaign->fresh()->pixel_id);
    }

    /** Meta's questions still route to Meta's tools, which is what makes the above safe. */
    public function test_a_meta_question_still_reaches_the_campaign_namespace(): void
    {
        $campaign = Campaign::create([
            'user_id' => $this->user->id,
            'name' => 'Meta one',
            'platform' => 'meta',
            'status' => 'draft',
        ]);

        app(Workspace::class)->focusOn($campaign);

        app(Answers::class)->applyAll([
            ['field' => 'objective', 'value' => 'OUTCOME_LEADS'],
        ]);

        $this->assertSame('OUTCOME_LEADS', $campaign->fresh()->objective);
    }
}
