<?php

namespace Tests\Feature;

use App\Agent\Workspace;
use App\Campaigns\LaunchFlow;
use App\Campaigns\Platforms\MetaRules;
use App\Campaigns\Platforms\Platforms;
use App\Campaigns\Targeting\SearchKeywordSelections;
use App\Campaigns\Targeting\TargetingSelections;
use App\Models\Campaign;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Mockery;
use Tests\TestCase;

/**
 * The order a campaign gets built in.
 *
 * Clickable options made each answer easy, and the agent still waited to be
 * asked what came next, so the user was driving a flow they were meant to be
 * led through. This derives the next step from what the campaign holds, so the
 * answer is the same every turn and survives the user doing things out of order.
 */
class LaunchFlowTest extends TestCase
{
    use RefreshDatabase;

    private function campaign(array $attributes = []): Campaign
    {
        return Campaign::create(array_merge([
            'user_id' => User::factory()->create()->id,
            'name' => 'Winter Sale',
            'platform' => 'meta',
            'status' => 'draft',
        ], $attributes));
    }

    /**
     * The first question is what is being advertised.
     *
     * It used to be the ad account, which asked the buyer to do administration
     * before they had said what they wanted. The landing page is the one thing
     * they arrive knowing, and reading it is what lets every later step be a
     * recommendation rather than a blank question.
     */
    public function test_an_empty_campaign_starts_at_the_landing_page(): void
    {
        $next = app(LaunchFlow::class)->next($this->campaign());

        $this->assertSame('landing_url', $next['key']);
        $this->assertSame('analyze_landing_page', $next['tool']);
    }

    public function test_an_open_daily_schedule_choice_is_visible_in_progress(): void
    {
        $campaign = $this->campaign(['budget' => 20, 'budget_mode' => 'daily']);
        app(Workspace::class)->bindTo('conv-daily-schedule');
        app(Workspace::class)->focusOn($campaign);
        app(Workspace::class)->rememberChoices([[
            'field' => 'ends_at', 'focus_id' => $campaign->getKey(), 'focus_type' => Campaign::class,
            'options' => [['value' => '', 'label' => 'Continuous']],
        ]]);

        $progress = collect(app(LaunchFlow::class)->progress($campaign))->keyBy('key');

        $this->assertTrue($progress->has('schedule'));
        $this->assertFalse($progress['schedule']['done']);
    }

    public function test_progress_reads_pending_schedule_choices_only_once(): void
    {
        $campaign = $this->campaign(['budget' => 20, 'budget_mode' => 'daily']);
        $workspace = Mockery::mock(Workspace::class, [null])->makePartial();
        $workspace->shouldReceive('pendingChoices')->once()->andReturn([]);
        $flow = new LaunchFlow(app(Platforms::class), $workspace, app(TargetingSelections::class), app(SearchKeywordSelections::class));

        $progress = $flow->progress($campaign);

        $this->assertNotContains('schedule', array_column($progress, 'key'));
    }

    public function test_unrecognized_platform_steps_are_preserved_and_remain_outstanding(): void
    {
        $campaign = $this->campaign();
        $meta = app(MetaRules::class);
        $rules = Mockery::mock(MetaRules::class)->makePartial();
        $rules->shouldReceive('steps')->andReturn([
            ...$meta->steps(),
            ['key' => 'new_platform_setting', 'label' => 'new platform setting', 'tool' => 'choose_page', 'offers' => true],
        ]);
        $platforms = Mockery::mock(Platforms::class);
        $platforms->shouldReceive('for')->with($campaign)->andReturn($rules);
        $flow = new LaunchFlow($platforms, app(Workspace::class), app(TargetingSelections::class), app(SearchKeywordSelections::class));

        $this->assertContains('new_platform_setting', array_column($flow->steps($campaign), 'key'));
        $this->assertFalse(collect($flow->progress($campaign))->firstWhere('key', 'new_platform_setting')['done']);
    }

    /** Budget follows the account and targeting so its currency is known. */
    public function test_the_budget_is_asked_after_account_and_targeting(): void
    {
        $keys = array_column(app(LaunchFlow::class)->progress($this->campaign()), 'key');

        $this->assertSame('landing_url', $keys[0]);
        $this->assertLessThan(
            array_search('budget', $keys, true),
            array_search('ad_account', $keys, true),
        );
        $this->assertLessThan(
            array_search('budget', $keys, true),
            array_search('countries', $keys, true),
        );
        $this->assertContains('creatives', $keys);
    }

    public function test_it_moves_on_as_things_are_answered(): void
    {
        $campaign = $this->campaign(['landing_url' => 'https://example.com']);

        $this->assertSame('objective', app(LaunchFlow::class)->next($campaign)['key']);

        $campaign->update(['objective' => 'OUTCOME_TRAFFIC']);

        $this->assertSame('special_ad_categories', app(LaunchFlow::class)->next($campaign->fresh())['key']);

        $campaign->update(['special_ad_categories' => []]);

        $this->assertSame('ad_account', app(LaunchFlow::class)->next($campaign->fresh())['key']);

        $campaign->update(['ad_account_id' => 'act_777']);

        $this->assertSame('placements', app(LaunchFlow::class)->next($campaign->fresh())['key']);
    }

    /**
     * Declared-as-none is an answer; never-asked is not.
     *
     * The whole reason the column is nullable, and the flow has to respect the
     * same distinction or it would skip the question.
     */
    public function test_an_undeclared_category_is_not_treated_as_answered(): void
    {
        $campaign = $this->campaign([
            'landing_url' => 'https://example.com',
            'budget' => 20,
            'budget_mode' => 'daily',
            'ad_account_id' => 'act_777',
            'objective' => 'OUTCOME_TRAFFIC',
        ]);

        $this->assertSame('special_ad_categories', app(LaunchFlow::class)->next($campaign)['key']);

        $campaign->update(['special_ad_categories' => ['NONE']]);

        $this->assertNotSame('special_ad_categories', app(LaunchFlow::class)->next($campaign->fresh())['key']);
    }

    /**
     * A step that does not apply is not an outstanding step.
     *
     * Traffic needs no pixel, so asking for one would be inventing work.
     */
    public function test_a_traffic_campaign_is_never_asked_for_a_pixel(): void
    {
        $campaign = $this->campaign([
            'landing_url' => 'https://example.com',
            'budget' => 20,
            'budget_mode' => 'daily',
            'ad_account_id' => 'act_777',
            'objective' => 'OUTCOME_TRAFFIC',
            'special_ad_categories' => ['NONE'],
            'page_id' => 'page-1',
        ]);

        $keys = array_column(app(LaunchFlow::class)->progress($campaign), 'key');

        $this->assertNotContains('pixel', $keys);
        $this->assertSame('placements', app(LaunchFlow::class)->next($campaign)['key']);
    }

    public function test_a_leads_campaign_is_asked_for_a_pixel(): void
    {
        $campaign = $this->campaign([
            'landing_url' => 'https://example.com',
            'budget' => 20,
            'budget_mode' => 'daily',
            'ad_account_id' => 'act_777',
            'objective' => 'OUTCOME_LEADS',
            'special_ad_categories' => ['NONE'],
            'page_id' => 'page-1',
            'placements' => ['facebook'],
        ]);

        $this->assertSame('pixel', app(LaunchFlow::class)->next($campaign)['key']);
    }

    /** Progress is what a person is shown, so it has to count only real steps. */
    public function test_progress_reports_what_is_settled(): void
    {
        $campaign = $this->campaign([
            'ad_account_id' => 'act_777',
            'objective' => 'OUTCOME_TRAFFIC',
        ]);

        $progress = app(LaunchFlow::class)->progress($campaign);

        $this->assertTrue(collect($progress)->firstWhere('key', 'ad_account')['done']);
        $this->assertFalse(collect($progress)->firstWhere('key', 'budget')['done']);
    }

    public function test_campaign_with_copy_still_needs_location_scope_choice(): void
    {
        $campaign = $this->campaign([
            'ad_account_id' => 'act_777',
            'objective' => 'OUTCOME_TRAFFIC',
            'special_ad_categories' => ['NONE'],
            'landing_url' => 'https://example.com',
            'page_id' => 'page-1',
            'budget' => 20,
            'bid_strategy' => 'LOWEST_COST_WITHOUT_CAP',
            'locations' => ['countries' => ['US']],
            'placements' => ['facebook'],
            'audience' => ['devices' => ['mobile', 'desktop'], 'meta_audience_mode' => 'broad'],
            'utm' => '',
        ]);

        $campaign->ads()->create([
            'headline' => 'Winter Sale',
            'primary_text' => 'Half price until Sunday.',
            'cta' => 'SHOP_NOW',
            'asset_id' => null,
        ]);

        // Country selection now needs an explicit whole-country or city/state decision.
        $this->assertSame('location_scope', app(LaunchFlow::class)->next($campaign->fresh())['key']);
    }

    // ------------------------------------------------- shown to the buyer

    /**
     * The panel has to say where they are.
     *
     * LaunchFlow has always computed this to decide the next question, and
     * nothing rendered it, so the product could walk somebody through fourteen
     * steps without ever saying how many there were or how far along they got.
     */
    public function test_the_panel_shows_how_far_along_the_campaign_is(): void
    {
        $user = User::factory()->create();
        $this->actingAs($user);

        $conversation = 'conv-progress-001';
        DB::table('agent_conversations')->insert([
            'id' => $conversation,
            'participant_type' => $user->getMorphClass(),
            'participant_id' => $user->getKey(),
            'title' => 'Test conversation',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $workspace = app(Workspace::class);
        $workspace->bindTo($conversation);
        $workspace->ownedBy($user);

        $campaign = $this->campaign([
            'ad_account_id' => 'act_777',
            'objective' => 'OUTCOME_TRAFFIC',
            'special_ad_categories' => ['NONE'],
        ]);
        $workspace->produced($campaign);
        $workspace->focusOn($campaign);

        $html = $this->get(route('chat.show', ['conversation' => $conversation]))
            ->assertOk()
            ->getContent();

        // Three of the steps are answered, and a traffic campaign has no pixel
        // step at all, so it is not counted against them.
        $this->assertStringContainsString('3 of', $html);
        $this->assertStringContainsString('Next:', $html);
        $this->assertStringContainsString('mb-progress-step', $html);
        $this->assertStringNotContainsString('>pixel<', $html);
    }

    /**
     * Bidding was a tool nothing ever reached.
     *
     * SetBidding has existed since the start and the flow never asked, so every
     * campaign was published on whatever the default happened to be, with the
     * panel showing a strategy nobody chose.
     */
    public function test_the_flow_asks_how_it_should_bid(): void
    {
        $campaign = $this->campaign([
            'ad_account_id' => 'act_777',
            'objective' => 'OUTCOME_TRAFFIC',
            'special_ad_categories' => ['NONE'],
            'landing_url' => 'https://example.com',
            'page_id' => 'page-1',
            'budget' => 20,
            'placements' => ['facebook'],
            'audience' => ['devices' => ['mobile', 'desktop'], 'meta_audience_mode' => 'broad'],
            'locations' => ['countries' => ['US']],
        ]);

        app(TargetingSelections::class)->chooseWholeCountries($campaign);

        $this->assertSame('bidding', app(LaunchFlow::class)->next($campaign)['key']);
    }

    /**
     * A campaign that runs until someone stops it needs no end date, so asking
     * for one would be a step that does not exist. A lifetime budget is the
     * total to spend by a date, so there it does.
     */
    public function test_an_end_date_is_only_asked_for_when_the_budget_is_a_lifetime_one(): void
    {
        $settled = [
            'ad_account_id' => 'act_777',
            'objective' => 'OUTCOME_TRAFFIC',
            'special_ad_categories' => ['NONE'],
            'landing_url' => 'https://example.com',
            'page_id' => 'page-1',
            'budget' => 20,
            'bid_strategy' => 'LOWEST_COST_WITHOUT_CAP',
            'placements' => ['facebook'],
            'audience' => ['devices' => ['mobile', 'desktop'], 'meta_audience_mode' => 'broad'],
            'locations' => ['countries' => ['US']],
        ];

        $daily = $this->campaign($settled);
        app(TargetingSelections::class)->chooseWholeCountries($daily);

        $this->assertNotContains('schedule', array_column(app(LaunchFlow::class)->progress($daily), 'key'));
        $this->assertSame('creatives', app(LaunchFlow::class)->next($daily)['key']);

        $lifetime = $this->campaign([...$settled, 'budget_mode' => 'lifetime']);
        app(TargetingSelections::class)->chooseWholeCountries($lifetime);

        $this->assertSame('schedule', app(LaunchFlow::class)->next($lifetime)['key']);
    }
}
