<?php

namespace Tests\Feature;

use App\Agent\Choices;
use App\Agent\Workspace;
use App\Http\Controllers\ChatController;
use App\Models\Campaign;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use ReflectionMethod;
use Tests\TestCase;

class ChatNeverLeavesTheUserWithoutAnActionTest extends TestCase
{
    use RefreshDatabase;

    public function test_a_step_that_could_not_load_offers_retry_and_manual_entry(): void
    {
        $controller = app(ChatController::class);

        (new ReflectionMethod($controller, 'offerRecovery'))->invoke($controller, [
            'key' => 'ad_account', 'label' => 'ad account', 'tool' => 'list_ad_accounts', 'offers' => true,
        ]);

        $choice = app(Choices::class)->all()[0];

        $this->assertSame('retry_ad_account', $choice['field']);
        $this->assertSame(['retry', 'manual'], array_column($choice['options'], 'value'));
    }

    public function test_the_publish_dialog_brief_names_the_platform_and_the_basics(): void
    {
        $campaign = Campaign::create([
            'user_id' => User::factory()->create()->id,
            'name' => 'Winter Sale',
            'platform' => 'meta',
            'status' => 'draft',
            'objective' => 'OUTCOME_TRAFFIC',
            'budget' => 20,
            'budget_mode' => 'daily',
            'currency' => 'USD',
            'locations' => ['countries' => ['US']],
        ]);

        $controller = app(ChatController::class);
        $brief = (new ReflectionMethod($controller, 'publishBrief'))->invoke($controller, $campaign);

        $this->assertSame('Winter Sale', $brief['name']);
        $this->assertSame('Meta', $brief['platform']);
        $this->assertContains('objective Traffic', $brief['lines']);
        $this->assertContains('budget USD 20.00 a day', $brief['lines']);
        $this->assertContains('in US', $brief['lines']);
    }

    public function test_a_finished_draft_offers_next_actions(): void
    {
        $controller = app(ChatController::class);

        (new ReflectionMethod($controller, 'offerFollowUps'))->invoke($controller);

        $choice = app(Choices::class)->all()[0];

        $this->assertSame('next_action', $choice['field']);
        $this->assertSame('The ad copy is ready. What would you like to do next?', $choice['question']);
        $this->assertSame(['review', 'edit_copy', 'creatives'], array_column($choice['options'], 'value'));
        $this->assertSame('Check what is still needed to publish', $choice['options'][0]['label']);
    }

    public function test_a_finished_meta_draft_offers_optional_audience_refinements(): void
    {
        $campaign = Campaign::create([
            'user_id' => User::factory()->create()->id,
            'name' => 'Winter Sale',
            'platform' => 'meta',
            'status' => 'draft',
            'special_ad_categories' => ['NONE'],
        ]);
        app(Workspace::class)->bindTo('optional-meta-audience');
        app(Workspace::class)->focusOn($campaign);

        (new ReflectionMethod(app(ChatController::class), 'offerFollowUps'))->invoke(app(ChatController::class));

        $choice = app(Choices::class)->all()[0];

        $this->assertSame(['review', 'edit_copy', 'creatives', 'saved_audience'], array_column($choice['options'], 'value'));
        $savedAudience = collect($choice['options'])->firstWhere('value', 'saved_audience');
        $this->assertSame('Use a saved website audience.', $savedAudience['sends']);
    }

    public function test_a_restricted_meta_draft_does_not_offer_audience_refinements(): void
    {
        $campaign = Campaign::create([
            'user_id' => User::factory()->create()->id,
            'name' => 'Housing Campaign',
            'platform' => 'meta',
            'status' => 'draft',
            'special_ad_categories' => ['HOUSING'],
        ]);
        app(Workspace::class)->bindTo('restricted-meta-audience');
        app(Workspace::class)->focusOn($campaign);

        (new ReflectionMethod(app(ChatController::class), 'offerFollowUps'))->invoke(app(ChatController::class));

        $choice = app(Choices::class)->all()[0];

        $this->assertSame(['review', 'edit_copy', 'creatives'], array_column($choice['options'], 'value'));
    }
}
