<?php

namespace Tests\Unit;

use App\Campaigns\Platforms\LinkedInRules;
use App\Models\Campaign;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class LinkedInRulesTest extends TestCase
{
    use RefreshDatabase;

    private LinkedInRules $rules;

    protected function setUp(): void
    {
        parent::setUp();
        $this->rules = new LinkedInRules;
    }

    public function test_key_and_name(): void
    {
        $this->assertSame('linkedin', $this->rules->key());
        $this->assertSame('LinkedIn', $this->rules->name());
    }

    public function test_objectives_and_minimum_budget(): void
    {
        $objectives = $this->rules->objectives();
        $this->assertContains('WEBSITE_VISITS', $objectives);
        $this->assertContains('LEAD_GENERATION', $objectives);
        $this->assertSame(10.00, $this->rules->minimumDailyBudget());
    }

    public function test_copy_limits(): void
    {
        $limits = $this->rules->copyLimits();
        $this->assertSame(70, $limits['headline']);
        $this->assertSame(100, $limits['description']);
        $this->assertSame(600, $limits['introductory_text']);
    }

    public function test_optimization_and_conversions(): void
    {
        $this->assertSame('CLICKS', $this->rules->optimizationGoal('WEBSITE_VISITS'));
        $this->assertSame('LEAD_GENERATION', $this->rules->optimizationGoal('LEAD_GENERATION'));
        $this->assertSame('PAGE_VIEW', $this->rules->conversionEvent('WEBSITE_VISITS'));
        $this->assertSame('LEAD', $this->rules->conversionEvent('LEAD_GENERATION'));
    }

    public function test_additional_problems_detects_invalid_schedule(): void
    {
        $campaign = new Campaign;
        $campaign->starts_at = now()->addDays(5);
        $campaign->ends_at = now()->addDays(2);

        $problems = $this->rules->additionalProblems($campaign);
        $this->assertContains('end date must be after the start date', $problems);
    }

    /**
     * Target cost is kept off screen for the objectives LinkedIn sells no
     * target cost for, so the buyer cannot pick a strategy that the API
     * refuses once the campaign group has already been created.
     */
    public function test_target_cost_is_not_offered_for_objectives_linkedin_has_no_target_for(): void
    {
        foreach (['LEAD_GENERATION', 'WEBSITE_CONVERSIONS', 'JOB_APPLY'] as $objective) {
            $this->assertNotContains('TARGET_COST', $this->rules->bidStrategies($objective), $objective);
            $this->assertNotContains(
                'TARGET_COST',
                array_column($this->rules->bidStrategyOptions($objective), 'value'),
                $objective,
            );
            $this->assertNull($this->rules->bidStrategyFor('TARGET_COST', $objective), $objective);
        }
    }

    public function test_target_cost_is_still_offered_for_the_objectives_that_take_it(): void
    {
        foreach (['WEBSITE_VISITS', 'ENGAGEMENT', 'BRAND_AWARENESS', 'VIDEO_VIEWS'] as $objective) {
            $this->assertContains('TARGET_COST', $this->rules->bidStrategies($objective), $objective);
            $this->assertSame('TARGET_COST', $this->rules->bidStrategyFor('TARGET_COST', $objective), $objective);
        }

        $this->assertContains('TARGET_COST', $this->rules->bidStrategies());
    }

    /** Every objective keeps the two strategies that work everywhere. */
    public function test_automated_and_maximum_bidding_survive_every_objective(): void
    {
        foreach ($this->rules->objectives() as $objective) {
            $strategies = $this->rules->bidStrategies($objective);
            $this->assertContains('AUTOMATED_BID', $strategies, $objective);
            $this->assertContains('MAXIMUM_BID', $strategies, $objective);
        }
    }
}
