<?php

namespace Tests\Feature;

use App\Campaigns\CampaignImporter;
use App\Campaigns\Platforms\LinkedInRules;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use PHPUnit\Framework\Attributes\TestWith;
use Tests\TestCase;

/**
 * An adopted LinkedIn campaign keeps an objective this application knows.
 *
 * LinkedIn's objectiveType enum is singular and the internal names are plural,
 * so each singular form has to be translated on the way in. Two were missing -
 * JOB_APPLICANT and WEBSITE_CONVERSION - and a campaign adopted on either kept
 * LinkedIn's own spelling, which is not one of LinkedInRules::objectives(). The
 * publish gate then refused the campaign for having an objective LinkedIn does
 * not support, naming the objective LinkedIn had just reported for it.
 */
class LinkedInImportReadsItsOwnObjectiveNamesTest extends TestCase
{
    use RefreshDatabase;

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

        config([
            'platforms.linkedin.token' => 'test-token',
            'platforms.guardrails.allowed_ad_accounts' => ['556447014'],
        ]);
    }

    #[TestWith(['WEBSITE_VISIT', 'WEBSITE_VISITS'])]
    #[TestWith(['VIDEO_VIEW', 'VIDEO_VIEWS'])]
    #[TestWith(['JOB_APPLICANT', 'JOB_APPLY'])]
    #[TestWith(['WEBSITE_CONVERSION', 'WEBSITE_CONVERSIONS'])]
    #[TestWith(['LEAD_GENERATION', 'LEAD_GENERATION'])]
    #[TestWith(['BRAND_AWARENESS', 'BRAND_AWARENESS'])]
    #[TestWith(['ENGAGEMENT', 'ENGAGEMENT'])]
    public function test_an_adopted_campaign_keeps_an_objective_this_application_offers(string $reported, string $stored): void
    {
        Http::fake(['https://api.linkedin.com/*' => Http::response([
            'id' => 917112356,
            'account' => 'urn:li:sponsoredAccount:556447014',
            'name' => 'Adopted campaign',
            'objectiveType' => $reported,
            'status' => 'PAUSED',
            'dailyBudget' => ['amount' => '50', 'currencyCode' => 'USD'],
        ])]);

        $campaign = app(CampaignImporter::class)
            ->adoptLinkedIn('917112356', '556447014', User::factory()->create());

        $this->assertSame($stored, $campaign->objective);
        $this->assertContains($campaign->objective, (new LinkedInRules)->objectives());
    }
}
