<?php

namespace Tests\Feature;

use App\DTOs\LandingPageAnalysis;
use App\Models\User;
use App\Support\GeneratedNames;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

/**
 * "Make sure the campaign name starts with the project name or site name.
 * When I launched for stockprices, it says trade smarter."
 *
 * The name's first part came from the page's own title, read up to the first
 * separator. stockprices.com/landing is titled "Trade Smarter - ..." so the
 * campaign published as trade-smarter_mohsin-khan-u5_GOOGLE_2026-10-22, and
 * nothing in it said which site it was for. iblockads only looked right by
 * luck: that page's title happens to start with its own brand.
 *
 * The site is in the landing URL and is not a guess, so the name starts there,
 * and what the page is about follows it: site alone would name every campaign
 * on a site the same thing, and stockprices.com/landing and
 * stockprices.com/offers are two campaigns a buyer runs separately. The page
 * title alone is the fallback for a URL that cannot be parsed.
 */
class ACampaignNameStartsWithTheSiteTest extends TestCase
{
    use RefreshDatabase;

    private function analysis(string $url, string $title): LandingPageAnalysis
    {
        return new LandingPageAnalysis(
            url: $url,
            title: $title,
            description: '',
            headings: [],
            text: '',
            keywords: [],
        );
    }

    /** The reported case. */
    public function test_the_name_starts_with_the_site_not_the_page_title(): void
    {
        $this->assertSame(
            'stockprices-trade-smarter',
            $this->analysis('https://stockprices.com/landing', 'Trade Smarter - Stock Analysis')->campaignName(),
        );
    }

    /**
     * And the one that looked right before, which still does.
     *
     * The page is named after the site, so the site is not said twice.
     */
    public function test_a_page_whose_title_is_its_brand_is_unchanged(): void
    {
        $this->assertSame(
            'iblockads',
            $this->analysis('https://iblockads.org/extension/landing', 'iBlockAds - Block Ads')->campaignName(),
        );
    }

    /** Two pages on one site are still two names. */
    public function test_two_pages_on_one_site_are_told_apart(): void
    {
        $landing = $this->analysis('https://stockprices.com/landing', 'Trade Smarter')->campaignName();
        $offers = $this->analysis('https://stockprices.com/offers', 'Half Price Year One')->campaignName();

        $this->assertNotSame($landing, $offers);
        $this->assertStringStartsWith('stockprices-', $landing);
        $this->assertStringStartsWith('stockprices-', $offers);
    }

    /** www is not what anyone calls the project. */
    public function test_a_www_host_names_the_site(): void
    {
        $this->assertSame(
            'stockprices-trade-smarter',
            $this->analysis('https://www.stockprices.com/landing', 'Trade Smarter')->campaignName(),
        );
    }

    /** A real subdomain is part of what the buyer calls the project. */
    public function test_a_meaningful_subdomain_is_kept(): void
    {
        $this->assertSame(
            'offers-stockprices-trade-smarter',
            $this->analysis('https://offers.stockprices.com/landing', 'Trade Smarter')->campaignName(),
        );
    }

    /** A multi-part suffix is not mistaken for the name. */
    public function test_a_co_uk_host_names_the_site(): void
    {
        $this->assertSame(
            'bargains-today-only',
            $this->analysis('https://www.bargains.co.uk/deals', 'Today Only')->campaignName(),
        );
    }

    /** With no usable URL the page's own words still answer. */
    public function test_the_page_title_is_the_fallback(): void
    {
        $this->assertSame(
            'Trade Smarter',
            $this->analysis('not a url', 'Trade Smarter - Stock Analysis')->campaignName(),
        );
    }

    /** And with neither, the name stays a placeholder rather than empty. */
    public function test_neither_leaves_the_placeholder(): void
    {
        $this->assertSame('New campaign', $this->analysis('', '')->campaignName());
    }

    /** The whole published name, which is what he reads in Google Ads. */
    public function test_the_published_name_reads_site_first(): void
    {
        $user = User::factory()->create(['name' => 'Mohsin Khan']);

        $this->assertSame(
            'stockprices-trade-smarter_mohsin-khan-u'.$user->getKey().'_GOOGLE_'.now()->format('Y-m-d'),
            GeneratedNames::campaign(
                $this->analysis('https://stockprices.com/landing', 'Trade Smarter')->campaignName(),
                'google',
                $user,
            ),
        );
    }
}
