<?php

namespace Tests\Feature;

use App\Agent\Instructions;
use App\Agent\Workspace;
use App\Campaigns\Briefs;
use App\DTOs\LandingPageAnalysis;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

/**
 * The landing page is read once and then used, not read once and forgotten.
 *
 * The analysis has always held the business name, the page's own words, its
 * headlines and a reading of what it is for. Two tools reached in for one field
 * each and nothing else ever saw it, so the model asked its way through a list
 * of questions the page had already answered and wrote ad copy from nothing.
 *
 * Surfacing it is the whole of the change. Nothing is applied behind the
 * buyer's back, and the outcome in particular stays a suggestion: it is a guess
 * made from phrases on a page, and it decides whether a pixel is needed and how
 * the platform bids, so it is the one worth being slow about.
 */
class WhatThePageAlreadyAnsweredTest extends TestCase
{
    use RefreshDatabase;

    private function promptAfterReading(?LandingPageAnalysis $analysis): string
    {
        $user = User::factory()->create();
        $this->actingAs($user);

        $workspace = app(Workspace::class);
        $workspace->bindTo('conv-already-known');
        $workspace->ownedBy($user);

        if ($analysis !== null) {
            app(Briefs::class)->remember($analysis);
        }

        return app(Instructions::class)->build();
    }

    private function leadPage(): LandingPageAnalysis
    {
        return new LandingPageAnalysis(
            url: 'https://attestpath.ai/',
            title: 'AttestPath - Compliance automation',
            description: 'Book a call to automate SOC 2 evidence collection.',
            headings: ['AttestPath', 'Automate your evidence collection'],
            text: 'Book a call with our team to see how AttestPath works.',
            keywords: ['compliance', 'evidence', 'automation'],
        );
    }

    public function test_the_page_is_surfaced_to_the_model(): void
    {
        $text = $this->promptAfterReading($this->leadPage());

        $this->assertStringContainsString('ALREADY KNOWN FROM THE LANDING PAGE', $text);
        $this->assertStringContainsString('who is advertising: AttestPath', $text);
        $this->assertStringContainsString('compliance, evidence, automation', $text);
        $this->assertStringContainsString('Automate your evidence collection', $text);
    }

    public function test_it_says_not_to_ask_for_what_the_page_answers(): void
    {
        $this->assertStringContainsString(
            'never ask the user for anything this already answers',
            $this->promptAfterReading($this->leadPage()),
        );
    }

    /** The copy has something to be written from, which was the other half. */
    public function test_the_copy_is_told_to_start_from_the_page(): void
    {
        $this->assertStringContainsString(
            'Write the ad copy from them rather than from nothing',
            $this->promptAfterReading($this->leadPage()),
        );
    }

    /**
     * The outcome arrives as a reading with its reason, never as a decision.
     *
     * Applying it silently would settle what the campaign is for out of two
     * words on a page, and the panel would show it as though somebody chose it.
     */
    public function test_the_outcome_stays_a_suggestion_with_its_reason(): void
    {
        $text = $this->promptAfterReading($this->leadPage());

        $this->assertStringContainsString('leads, because the page says "book a call"', $text);
        $this->assertStringContainsString('A reading, not an answer', $text);
    }

    /** A page that signals nothing suggests nothing, rather than guessing. */
    public function test_a_page_with_no_signal_offers_no_outcome(): void
    {
        $text = $this->promptAfterReading(new LandingPageAnalysis(
            url: 'https://example.com/',
            title: 'Example',
            description: 'An article about things.',
            headings: ['Example'],
            text: 'Words about a subject, with nothing to click.',
            keywords: ['things'],
        ));

        $this->assertStringContainsString('ALREADY KNOWN FROM THE LANDING PAGE', $text);
        $this->assertStringNotContainsString('what the page looks like it is for', $text);
    }

    /** And with no page read yet the section is absent, not empty. */
    public function test_nothing_is_added_before_a_page_has_been_read(): void
    {
        $this->assertStringNotContainsString(
            'ALREADY KNOWN FROM THE LANDING PAGE',
            $this->promptAfterReading(null),
        );
    }
}
