<?php

namespace Tests\Unit\DTOs;

use App\DTOs\LandingPageAnalysis;
use PHPUnit\Framework\TestCase;

class LandingPageAnalysisTest extends TestCase
{
    /** @param list<string> $headings */
    private function analysis(string $title, array $headings): LandingPageAnalysis
    {
        return new LandingPageAnalysis(
            url: 'https://example.com',
            title: $title,
            description: '',
            headings: $headings,
            text: '',
            keywords: [],
        );
    }

    public function test_the_brand_comes_from_the_title_rather_than_a_form_label(): void
    {
        $analysis = $this->analysis('AdMedia | Premier Advertising Network', ['You are?*', 'Your Add Gaols*']);

        $this->assertSame('AdMedia', $analysis->businessName());
    }

    public function test_a_generic_title_falls_back_to_the_first_real_heading(): void
    {
        $analysis = $this->analysis('Home | Site', ['Email*', 'Acme Plumbing']);

        $this->assertSame('Acme Plumbing', $analysis->businessName());
    }

    /**
     * The campaign name comes from the site, and the page is the fallback.
     *
     * This asserted "New campaign" for a page that says nothing about itself.
     * The name now starts with the site, which the landing URL always knows,
     * so the neutral placeholder is only reached when there is no usable URL
     * either. Asked for directly: "make sure the campaign name starts with the
     * project name or site name." See ACampaignNameStartsWithTheSiteTest.
     */
    public function test_a_page_with_no_usable_name_is_named_after_its_site(): void
    {
        $analysis = $this->analysis('', ['Name?*']);

        $this->assertSame('example', $analysis->campaignName());
    }

    /** With no site to read either, the name stays a placeholder. */
    public function test_a_page_with_no_site_and_no_name_gets_a_neutral_campaign_name(): void
    {
        $analysis = new LandingPageAnalysis(
            url: '',
            title: '',
            description: '',
            headings: ['Name?*'],
            text: '',
            keywords: [],
        );

        $this->assertSame('New campaign', $analysis->campaignName());
    }
}
