<?php

namespace App\Campaigns\Platforms;

use App\Campaigns\Creatives\DisplayImageShapes;
use App\Campaigns\Targeting\GoogleUserListSelections;
use App\Campaigns\Targeting\SearchKeywordSelections;
use App\Models\AllowedAdAccount;
use App\Models\Campaign;
use App\Models\GoogleAdsConnection;
use App\Services\Platforms\Support\Guardrails;
use Illuminate\Support\Facades\Storage;
use RuntimeException;

/**
 * Google Ads, as the API actually enforces it.
 *
 * Google disagrees with Meta about almost everything a campaign is made of, and
 * until this class existed those disagreements were invisible: Google inherited
 * UnsupportedPlatformRules, which answers every question with Meta's answer or
 * with nothing. A Google campaign was therefore offered Meta's objectives, Meta's
 * bid strategies and Meta's placements, and the publish gate asked it for a
 * Facebook Page.
 *
 * The vocabulary here is Google's own, not a translation of Meta's. The brief
 * keeps the outcome in plain words and {@see objectiveFor()} is what turns it
 * into this platform's spelling, which is the only place the two vocabularies
 * are allowed to meet.
 */
class GoogleRules implements PlatformRules
{
    public function __construct(
        private readonly SearchKeywordSelections $searchKeywords,
        private readonly GoogleUserListSelections $userLists,
    ) {}

    /**
     * What a responsive search ad holds, as Google enforces it.
     *
     * The minimums were already checked below. The maximums were not, and
     * publishing campaign 47 on 23 Sep failed with "Request contains an invalid
     * argument. (TOO_MANY)": three ads, each contributing its description and
     * its primary text, made six descriptions where Google takes four. The
     * error names no field, so nothing in it pointed at the copy.
     *
     * These are not a block. The assets of a responsive search ad are
     * alternatives that Google mixes per auction rather than a fixed set, so
     * sending the first four is fitting the format, not dropping a message.
     */
    /**
     * The fewest a responsive search ad will take.
     *
     * Checked when the copy is written rather than only at the gate, so a
     * single variation is refused while the model still has the brief in
     * hand instead of at publish, when the campaign already exists.
     */
    public const MIN_HEADLINES = 3;

    public const MAX_HEADLINES = 15;

    /**
     * And the fewest descriptions, which Google enforces the same way.
     *
     * All four bounds were asked of the API rather than read off a page: an ad
     * with two headlines is refused TOO_FEW, one description TOO_FEW, sixteen
     * headlines TOO_MANY and five descriptions TOO_MANY. The last one matters
     * because the feedback asked for five.
     */
    public const MIN_DESCRIPTIONS = 2;

    public const MAX_DESCRIPTIONS = 4;

    /** What Google refuses rather than truncates, per line. */
    public const HEADLINE_LIMIT = 30;

    public const DESCRIPTION_LIMIT = 90;

    public function key(): string
    {
        return 'google';
    }

    public function name(): string
    {
        return 'Google Ads';
    }

    /**
     * The campaign goals Google offers for the channel types this product builds.
     *
     * App promotion and local store visits are deliberately absent, for the same
     * reason OUTCOME_APP_PROMOTION is absent on Meta: both need objects this
     * product has no field for anywhere, and a goal that cannot reach a
     * publishable campaign is worse as a button than as an absence.
     *
     * @return list<string>
     */
    public function objectives(): array
    {
        return ['SALES', 'LEADS', 'WEBSITE_TRAFFIC', 'BRAND_AWARENESS_AND_REACH'];
    }

    /**
     * The brief's plain word in Google's spelling.
     *
     * Only sales and leads are suggested from a landing page today, so the rest
     * are here to keep the mapping total rather than to be guessed at.
     */
    public function objectiveFor(string $outcome): ?string
    {
        return match (strtolower($outcome)) {
            'sales' => 'SALES',
            'leads' => 'LEADS',
            'traffic' => 'WEBSITE_TRAFFIC',
            'awareness' => 'BRAND_AWARENESS_AND_REACH',
            default => null,
        };
    }

    /** @return array{label: string, hint: string} */
    public function describeObjective(string $objective): array
    {
        return match ($objective) {
            'SALES' => ['label' => 'Sales', 'hint' => 'Optimise for purchases, counted by a conversion action.'],
            'LEADS' => ['label' => 'Leads', 'hint' => 'Optimise for form fills and enquiries, counted by a conversion action.'],
            'WEBSITE_TRAFFIC' => ['label' => 'Website traffic', 'hint' => 'Buy clicks to the page. No conversion tracking required.'],
            'BRAND_AWARENESS_AND_REACH' => ['label' => 'Awareness and reach', 'hint' => 'Buy impressions rather than clicks or conversions.'],
            default => ['label' => $objective, 'hint' => ''],
        };
    }

    /**
     * The campaign types this product can actually build on Google.
     *
     * Search first only because it is the cheapest to answer: it needs nothing
     * but words, all of which the shared flow already collects. Which one is
     * recommended is not this list's job, it is suggestedCampaignType()'s, and
     * it depends on the campaign.
     *
     * Shopping is absent: it needs a Merchant Center feed this product has no
     * concept of. Video is not a type here, it is a Performance Max campaign
     * carrying a YouTube asset, which is how Google sells it.
     *
     * @return list<string>
     */
    public function campaignTypes(): array
    {
        return ['search', 'display', 'performance_max'];
    }

    /** @return array{label: string, hint: string} */
    public function describeCampaignType(string $type): array
    {
        return match ($type) {
            'search' => [
                'label' => 'Search',
                'hint' => 'Text ads against what people search for. Built from your page, so nothing else is needed.',
            ],
            'display' => [
                'label' => 'Display',
                'hint' => 'Image ads across Google\'s network. Needs a 1.91:1 and a 1:1 image; 1200x628 and 1200x1200 are the recommended sizes.',
            ],
            'performance_max' => [
                'label' => 'Performance Max',
                'hint' => 'Google places across every network at once. Needs the same images as Display, and gives up most targeting control.',
            ],
            default => ['label' => $type, 'hint' => ''],
        };
    }

    /**
     * Which type suits this campaign, read from what it is already trying to do.
     *
     * Search was suggested unconditionally, which is a default rather than a
     * recommendation, and a wrong one often enough to matter: an awareness
     * campaign was being pointed at the search network, which sells intent and
     * has no impression inventory to buy reach against.
     *
     * Nothing here suggests a type the campaign could not actually publish.
     * Display and Performance Max both need images at two exact sizes, so
     * neither is recommended until those images are attached; recommending one
     * and then refusing it at the gate is worse than not recommending it.
     *
     * @return array{type: string, because: string}|null
     */
    public function suggestedCampaignType(?Campaign $campaign = null): ?array
    {
        if ($campaign === null) {
            return null;
        }

        $hasImages = $this->hasImageShaped($campaign, 'landscape')
            && $this->hasImageShaped($campaign, 'square');

        // Reach is bought on impressions, and the search network does not sell
        // them: nobody searches in order to be made aware of something.
        if ($campaign->objective === 'BRAND_AWARENESS_AND_REACH') {
            return $hasImages
                ? ['type' => 'display', 'because' => 'awareness is bought on impressions, which the search network does not sell, and the creatives for it are already attached']
                : ['type' => 'display', 'because' => 'awareness is bought on impressions, which the search network does not sell. It needs a 1.91:1 and a 1:1 image'];
        }

        // Everything a Performance Max campaign eats is already here: the copy,
        // the images at both sizes, and something to count. Below that bar it is
        // a campaign that hands Google control in exchange for less to work with.
        if ($hasImages
            && filled($campaign->pixel_id)
            && in_array($campaign->objective, ['SALES', 'LEADS'], true)) {
            return [
                'type' => 'performance_max',
                'because' => 'the copy, the images at both sizes and a conversion action are all in place, which is everything it needs to place across every network',
            ];
        }

        return [
            'type' => 'search',
            'because' => 'the landing page gives the keywords and the copy, which is everything a search campaign needs',
        ];
    }

    /**
     * Google's Smart Bidding strategies, plus manual CPC.
     *
     * Named as the API names them, because these are the values sent on the
     * campaign. Portfolio strategies are absent: they are account level objects
     * shared between campaigns, and this product creates campaigns.
     *
     * @return list<string>
     */
    /**
     * Google is the reason this takes an objective.
     *
     * "Bid automatically" means maximise clicks on a traffic campaign and
     * maximise conversions on a conversion one, and sending the wrong one buys
     * the wrong thing at full price. The others are unambiguous: a target cost
     * is a target CPA and a bid cap is manual CPC, whatever the campaign is
     * for.
     */
    public function bidStrategyFor(string $intent, ?string $objective = null): ?string
    {
        $wantsConversions = in_array((string) $objective, ['WEBSITE_CONVERSIONS', 'SALES', 'LEADS'], true);

        return match (strtoupper($intent)) {
            'AUTOMATIC' => $wantsConversions ? 'MAXIMIZE_CONVERSIONS' : 'MAXIMIZE_CLICKS',
            'TARGET_COST' => 'TARGET_CPA',
            'BID_CAP' => 'MANUAL_CPC',
            default => null,
        };
    }

    /**
     * Performance Max buys conversions or conversion value and nothing else.
     *
     * Google refuses target_spend and manual_cpc on a Performance Max campaign
     * with OPERATION_NOT_PERMITTED_FOR_CONTEXT, the same refusal class a buyer
     * met on a Search campaign's target CPA. Offering the two here built a
     * campaign that answered every question and could never be published, and
     * the buyer found out from Google. Verified against account 346-085-5874.
     */
    public function bidStrategies(?string $objective = null, ?string $campaignType = null): array
    {
        $strategies = ['MAXIMIZE_CONVERSIONS', 'TARGET_CPA', 'MAXIMIZE_CONVERSION_VALUE', 'TARGET_ROAS', 'MAXIMIZE_CLICKS', 'MANUAL_CPC'];

        if (in_array(strtolower(trim((string) $campaignType)), ['performance_max', 'video'], true)) {
            $strategies = array_values(array_diff($strategies, ['MAXIMIZE_CLICKS', 'MANUAL_CPC']));
        }

        return $strategies;
    }

    /** @return list<array{value: string, label: string, hint: string, sends: string}> */
    public function bidStrategyOptions(?string $objective = null, ?string $campaignType = null): array
    {
        $allowed = $this->bidStrategies($objective, $campaignType);

        return array_values(array_filter(
            $this->allBidStrategyOptions(),
            static fn (array $option): bool => in_array($option['value'], $allowed, true),
        ));
    }

    /** @return list<array{value: string, label: string, hint: string, sends: string}> */
    private function allBidStrategyOptions(): array
    {
        return [
            [
                'value' => 'MAXIMIZE_CONVERSIONS',
                'label' => 'Maximise conversions',
                'hint' => 'Spend the budget for as many conversions as possible, at whatever they cost.',
                'sends' => 'Bid to maximise conversions',
            ],
            [
                'value' => 'TARGET_CPA',
                'label' => 'Target cost per action',
                'hint' => 'Hold the average cost per conversion near a number you name.',
                'sends' => 'Use a target cost per action',
            ],
            [
                'value' => 'MAXIMIZE_CONVERSION_VALUE',
                'label' => 'Maximise conversion value',
                'hint' => 'Chase revenue rather than conversion count. Needs values on your conversion action.',
                'sends' => 'Bid to maximise conversion value',
            ],
            [
                'value' => 'TARGET_ROAS',
                'label' => 'Target return on ad spend',
                'hint' => 'Aim for a return you name, as a multiple of spend. Needs conversion values.',
                'sends' => 'Use a target return on ad spend',
            ],
            [
                'value' => 'MAXIMIZE_CLICKS',
                'label' => 'Maximise clicks',
                'hint' => 'Buy as many clicks as the budget allows. Sensible before conversions are tracked.',
                'sends' => 'Bid to maximise clicks',
            ],
            [
                'value' => 'MANUAL_CPC',
                'label' => 'Manual CPC',
                'hint' => 'Set the bid yourself. Rarely the right answer once Smart Bidding has data.',
                'sends' => 'Use manual CPC',
            ],
        ];
    }

    /**
     * The two "target" strategies are defined by their number and are rejected
     * without one. The maximise strategies take the budget as their only input,
     * and manual CPC carries its bid on the ad group rather than the campaign.
     */
    public function bidStrategyNeedsTarget(string $strategy): bool
    {
        return in_array($strategy, ['TARGET_CPA', 'TARGET_ROAS'], true);
    }

    /**
     * Google calls these networks rather than placements.
     *
     * The same idea as Meta's: where the ad is allowed to appear, named on the
     * campaign. Stored in the same column because it answers the same question,
     * and translated to Google's network flags by the adapter.
     *
     * @return list<string>
     */
    public function placements(): array
    {
        return ['search', 'search_partners', 'display'];
    }

    /**
     * The networks this campaign can run on, which its channel decides.
     *
     * Not the objective. This filtered on the objective alone, so a Display
     * campaign with a Sales goal was offered search, search partners and
     * display, and a buyer picked search: "I started with display and it was
     * building for search, and later it switched to display after generating
     * headlines and descriptions." The copy step read the network rather than
     * the campaign type, wrote a responsive search ad for a Display campaign,
     * and then asked him mid-flow which format he wanted.
     *
     * A Display campaign buys the Display network and nothing else: there is
     * no such thing as a Display campaign on the search network, and sending
     * targetGoogleSearch on a DISPLAY channel is not a preference Google
     * honours.
     *
     * A Search campaign keeps all three. Display there is Google's own Display
     * Network expansion - targetContentNetwork on a SEARCH campaign - which is
     * a real setting the publisher already sends, so narrowing Search to its
     * two search networks would have taken a feature away. The first version
     * of this fix did exactly that and PlacementsMatchThePlatformTest caught
     * it.
     *
     * Performance Max gets nothing to choose: Google runs it across everything
     * it owns, so a network question there has no answer. An empty list is how
     * LaunchFlow knows the step does not apply rather than leaving it
     * outstanding forever.
     *
     * The objective still narrows what is left, because awareness is bought on
     * impressions and the search network does not sell them.
     *
     * YouTube is not in any of these lists: it is not an independent network
     * selection this publisher supports, and a campaign wanting it is a
     * different campaign type.
     *
     * @return list<string>
     */
    public function placementsFor(string $objective, ?string $campaignType = null): array
    {
        $networks = match ($campaignType) {
            'display' => ['display'],
            'performance_max' => [],
            // Search, and anything not yet chosen: everything this publisher
            // supports, which is what the question offered in every case.
            default => $this->placements(),
        };

        if ($objective === 'BRAND_AWARENESS_AND_REACH') {
            return array_values(array_intersect($networks, ['display']));
        }

        return $networks;
    }

    /**
     * Responsive search ads have no call to action field.
     *
     * Google writes the button itself from the final URL and the copy. Returning
     * an empty list is how the publish gate knows not to ask, rather than
     * blocking on a field that cannot be filled.
     *
     * @return list<string>
     */
    public function callsToAction(): array
    {
        return [];
    }

    /**
     * Not modelled for Google.
     *
     * Google does restrict employment, housing and credit advertising, but it
     * enforces it through account level policy and verification rather than a
     * field on the campaign. Declaring categories we cannot send would be a
     * question whose answer goes nowhere.
     *
     * @return list<string>
     */
    public function specialAdCategories(): array
    {
        return [];
    }

    /**
     * Google enforces no daily minimum, so neither does this.
     *
     * It will take any budget above zero and deliver proportionally less, and
     * it may spend up to twice the daily amount on a given day while holding
     * the month to 30.4 times it.
     *
     * This returned 5.00 until 21 Sep, which was Meta's floor applied to Google
     * because Meta was once the only platform. It was not merely conservative,
     * it was stated as Google's: the split tool said "Google Ads needs at least
     * 5.00 a day" and the publish gate said "below Google Ads's 5.00 minimum",
     * both of which are claims about Google that Google does not make. A buyer
     * cannot argue with a platform rule, which is what made inventing one worse
     * than having none.
     *
     * Nothing takes a zero budget: the publish gate refuses a campaign with no
     * budget before it ever reaches a floor, and SplitBudget refuses a share
     * that is not positive, both independently of this.
     */
    /**
     * Google's ValueTrack parameters, in single braces.
     *
     * Google substitutes these at click time the way Meta substitutes its own,
     * and understands none of Meta's: {{campaign.name}} would arrive at the
     * landing page as those literal characters. The ids rather than the names
     * because that is what ValueTrack gives, and {network} is the closest thing
     * Google has to a placement, distinguishing Search from a search partner or
     * the Display network.
     */
    public function trackingOptions(): array
    {
        // The tagging the media buyers asked for, by name:
        // utm_content carries the ad group and utm_term the keyword.
        //
        // It used to put {creative} in utm_content and leave utm_term out
        // entirely, so a report could tell you which ad was clicked and never
        // which ad group or which keyword - and on Search the keyword is the
        // thing being bought. Recording the keyword was offered as a separate
        // option nobody picked, because this one is marked Suggested and is
        // what StartCampaign writes at creation.
        $standard = 'utm_source=google&utm_medium=cpc&utm_campaign={campaignid}&utm_content={adgroupid}&utm_term={keyword}';

        return [
            [
                'value' => $standard,
                'label' => 'Standard tagging',
                'hint' => 'Source, medium, campaign, ad group and keyword. Google fills in the ids.',
                'badge' => 'Suggested',
                'sends' => 'Use the standard UTM tagging',
            ],
            [
                // The ad id, which used to sit in utm_content. Kept reachable
                // rather than dropped: it is the only way to tell two ads in
                // one ad group apart.
                'value' => $standard.'&utm_creative={creative}',
                'label' => 'Standard, plus ad',
                'hint' => 'Also records which ad in the ad group was clicked.',
                'sends' => 'Use the standard UTM tagging plus ad',
            ],
            [
                // utm_network rather than utm_term, which now carries the
                // keyword. Overwriting it here would have quietly cost the
                // buyer the keyword to record the network.
                'value' => $standard.'&utm_network={network}',
                'label' => 'Standard, plus network',
                'hint' => 'Also records whether the click came from Search, a search partner or Display.',
                'sends' => 'Use the standard UTM tagging plus network',
            ],
        ];
    }

    /**
     * The configured account, but only when there is nothing to choose between.
     *
     * Google was rebuilt to run on a single internal customer id rather than a
     * per-user OAuth connection, and the account step was dropped because
     * childAccountOptions() read available_customer_ids - a column only the
     * OAuth flow ever filled - so the flow asked which account with nothing on
     * screen and stopped there. That was a resolver bug, and it is fixed: the
     * children of the configured manager are read live, with their names.
     *
     * So the step is back, and this is what keeps it from being a question with
     * one answer. A Google login is a manager with child accounts under it, and
     * the account a campaign is bought on is one of those children. Where the
     * allow list leaves exactly one of them permitted there is no choice to
     * make and the id is filled in here, which is the behaviour this has always
     * had. Where it leaves two or more, or a wildcard that could stand for
     * many, filling one in would answer on the buyer's behalf and the step
     * would arrive already ticked - so it returns null and the step asks.
     *
     * Decided from the allow list rather than from the manager, because this is
     * called while a campaign is being created: a network read here would make
     * creating any campaign, on any platform, wait on Google being reachable.
     * The live read happens in list_ad_accounts, once the question is asked.
     */
    public function defaultAdAccountId(): ?string
    {
        $customerId = str_replace('-', '', (string) GoogleAdsConnection::shared()?->customer_id);

        if ($customerId === '') {
            return null;
        }

        return $this->accountChoiceExists() ? null : $customerId;
    }

    /**
     * Whether the allow list leaves a buyer more than one account to pick from.
     *
     * A wildcard counts as a choice: it stands for every child under the
     * manager, which is a list nobody here has seen. An empty list is not a
     * choice - nothing can be published at all - and is deliberately left to
     * the existing administrator refusal in list_ad_accounts rather than turned
     * into a question, because no answer the buyer gives would help.
     */
    public function accountChoiceExists(): bool
    {
        // No manager, no children, so nothing to choose between whatever the
        // allow list says. A wildcard cannot stand for a list that cannot be
        // read: with GOOGLE_ADS_LOGIN_CUSTOMER_ID unset the only account this
        // instance knows of is the configured one, and treating the wildcard as
        // a choice sent the flow to ask a question that could only answer
        // "Select a manager customer before loading child accounts".
        if (blank(GoogleAdsConnection::shared()?->manager_customer_id)) {
            return false;
        }

        $permitted = app(Guardrails::class)->permittedAccounts('google');

        return in_array(AllowedAdAccount::WILDCARD, $permitted, true) || count($permitted) > 1;
    }

    public function minimumDailyBudget(): float
    {
        return 0.00;
    }

    /**
     * One, because Google has one.
     *
     * A Google budget is a campaignBudget resource with an amountMicros, held
     * by the campaign and shared by its ad groups. There is no ad-group budget
     * to choose instead, no lifetime amount, and no Advantage campaign budget,
     * which is a Meta feature that was being offered here by name. Of the four
     * options a Google buyer used to see, three could not be published, and
     * GoogleAdsService sends the daily amount whichever was picked.
     *
     * Returning one entry means the question is never asked.
     */
    public function budgetModes(): array
    {
        return [
            [
                'value' => 'campaign:daily',
                'label' => 'Daily',
                'hint' => 'Google spends this much a day across the campaign, and may go over on a '
                    .'busy day and under on a quiet one.',
                'sends' => 'Use a daily campaign budget',
            ],
        ];
    }

    /**
     * Responsive search ad limits, which Google enforces on create.
     *
     * Unlike Meta, Google refuses the ad rather than truncating it, so these are
     * the difference between a campaign that exists and an API error. Primary
     * text is our field for the body copy and maps to a description.
     *
     * @return array<string, int>
     */
    /**
     * One responsive search ad, built from the headlines of every variation.
     *
     * Which is why fifteen headlines is one ad here and not fifteen.
     */
    public function buildsOneAdFromEveryVariation(): bool
    {
        return true;
    }

    public function copyLimits(): array
    {
        return [
            'headline' => self::HEADLINE_LIMIT,
            'description' => self::DESCRIPTION_LIMIT,
            'primary_text' => self::DESCRIPTION_LIMIT,
        ];
    }

    /**
     * Google's equivalent of a pixel is a conversion action, and a campaign that
     * bids toward conversions cannot be measured without one.
     */
    public function requiresPixel(string $objective): bool
    {
        return in_array($objective, ['SALES', 'LEADS'], true);
    }

    /** Google ads run from the account, not from a page that has to be chosen. */
    public function requiresPage(): bool
    {
        return false;
    }

    /**
     * A responsive search ad is text: headlines, descriptions and a URL.
     * Display and Performance Max are pictures.
     *
     * So on Google this is a property of the campaign type rather than of the
     * platform, which is why the contract takes a campaign. With no campaign to
     * ask about, the answer is the Search one, because that is what an
     * unanswered type falls back to.
     */
    public function requiresCreativeAsset(?Campaign $campaign = null): bool
    {
        return in_array($campaign?->campaign_type, ['display', 'performance_max'], true);
    }

    /** GooglePublisher creates the image assets inside the publish, from the file. */
    public function uploadsCreativeBeforePublish(): bool
    {
        return false;
    }

    /**
     * The fewest headlines and descriptions this channel's ad can run with.
     *
     * A responsive search ad needs three and two; Google refuses it outright
     * below that rather than truncating the way Meta does. A responsive
     * display ad is a different ad and one of each is a complete one -
     * confirmed by publishing one to 346-085-5874 with a single headline and a
     * single description, which Google created as a RESPONSIVE_DISPLAY_AD
     * while the gate was refusing it twice over.
     *
     * Performance Max keeps the higher floor: its asset groups are held to it,
     * and nothing here has published one end to end, so the number stands
     * rather than being loosened on a guess.
     *
     * @return array{headlines: int, descriptions: int}
     */
    public function copyFloor(string $campaignType): array
    {
        return $campaignType === 'display'
            ? ['headlines' => 1, 'descriptions' => 1]
            : ['headlines' => 3, 'descriptions' => 2];
    }

    public function optimizationGoal(string $objective): ?string
    {
        return match ($objective) {
            'SALES', 'LEADS' => 'CONVERSIONS',
            'WEBSITE_TRAFFIC' => 'CLICKS',
            'BRAND_AWARENESS_AND_REACH' => 'IMPRESSIONS',
            default => null,
        };
    }

    /**
     * Google counts a named conversion action rather than a fixed event list, so
     * the event is the account's own and cannot be derived from the objective.
     */
    public function conversionEvent(string $objective): ?string
    {
        return null;
    }

    /**
     * What Google needs before a campaign can be built, beyond the shared checks.
     *
     * The "not connected yet" line this used to open with is gone: there is a
     * GooglePublisher now, and leaving a permanent blocker in place would make
     * it unreachable. What replaces it is the set of things that are genuinely
     * required to build a Search campaign and that the shared flow cannot know
     * to ask for.
     *
     * @return list<string>
     */
    public function additionalProblems(Campaign $campaign): array
    {
        $problems = [];

        // Meta's macros in a Google campaign's tracking.
        //
        // Google substitutes {campaignid} and {creative}; Meta's double-brace
        // macros mean nothing to it and arrive at the landing page as those
        // literal characters, so every click is misattributed and the URL is
        // visibly broken. Checked here because the campaigns that already carry
        // it were built before the tagging options were made per-platform, and
        // the gate passed them: a wrong value looks exactly like a right one.
        if (str_contains((string) $campaign->utm, '{{')) {
            $problems[] = 'the click tracking uses Meta macros, which Google does not replace: '
                .'re-answer the tracking question';
        }

        // Google has to be configured. Without this the failure is an exception
        // thrown from inside the publisher, after the campaign has been marked
        // as publishing.
        //
        // Asked of config rather than of the campaign's owner: this is one
        // common internal account, so it is either set up for everyone or for
        // nobody, and a per-user check reported it missing for colleagues who
        // simply had not run an OAuth flow they should never have needed.
        if (GoogleAdsConnection::shared() === null) {
            $problems[] = GoogleAdsConnection::missingReason();
        }

        // Google enforces these on create and refuses the ad outright, unlike
        // Meta, which accepts over-long copy and truncates it. How many it
        // wants depends on which ad is being built, which is what copyFloor()
        // answers: this applied the responsive search ad's three-and-two to
        // every channel and refused a Display campaign that Google accepts.
        $ads = $campaign->ads()->get();
        $headlines = $ads->pluck('headline')->filter()->unique();
        $descriptions = $ads->flatMap(fn ($ad): array => [$ad->description, $ad->primary_text])->filter()->unique();
        $floor = $this->copyFloor((string) ($campaign->campaign_type ?: 'search'));
        $adName = ($campaign->campaign_type ?: 'search') === 'display'
            ? 'a responsive display ad'
            : 'a responsive search ad';

        if ($ads->isNotEmpty() && $headlines->count() < $floor['headlines']) {
            $problems[] = sprintf(
                '%s needs at least %d different headline%s, and there %s %d',
                $adName,
                $floor['headlines'],
                $floor['headlines'] === 1 ? '' : 's',
                $headlines->count() === 1 ? 'is' : 'are',
                $headlines->count(),
            );
        }

        if ($ads->isNotEmpty() && $descriptions->count() < $floor['descriptions']) {
            $problems[] = sprintf(
                '%s needs at least %d different description%s, and there %s %d',
                $adName,
                $floor['descriptions'],
                $floor['descriptions'] === 1 ? '' : 's',
                $descriptions->count() === 1 ? 'is' : 'are',
                $descriptions->count(),
            );
        }

        // Search buys against keywords, and the shared flow never asks for any
        // because Meta has no equivalent. They come from the landing page, so
        // the failure mode is a page that yielded none rather than a question
        // nobody answered.
        //
        // Search only. Display buys audiences and Performance Max decides for
        // itself, and GoogleAdsService routes both to their own create methods,
        // neither of which reads a keyword. Applying this to every type blocked
        // them permanently: the gate refused, and nothing in the product can
        // supply keywords by hand, so there was no way forward at all.
        if (($campaign->campaign_type ?? 'search') === 'search') {
            try {
                if ($this->searchKeywords->criteria($campaign) === []) {
                    $problems[] = 'a Google Search campaign needs at least one selected keyword or a landing-page keyword';
                }
            } catch (RuntimeException $exception) {
                $problems[] = 'Search keywords need review: '.$exception->getMessage();
            }
        } elseif ($this->searchKeywords->hasExplicitSelection($campaign)) {
            $problems[] = 'saved Google Search keywords require a Search campaign; clear them before using this campaign type';
        }

        try {
            if (($campaign->campaign_type ?? 'search') !== 'search'
                && $this->userLists->selected($campaign) !== []) {
                $problems[] = 'saved Google Search audience lists require a Search campaign';
            }
        } catch (RuntimeException $exception) {
            $problems[] = 'Google audience lists need review: '.$exception->getMessage();
        }

        // Display and Performance Max run image ads, so they need pictures at
        // the two sizes Google states, and it rejects the ad group outright
        // without them. Checked here rather than left to the API, because by
        // the time the API says no the campaign and its budget already exist.
        if (in_array($campaign->campaign_type, ['display', 'performance_max'], true)) {
            foreach (['landscape', 'square'] as $shape) {
                if (! $this->hasImageShaped($campaign, $shape)) {
                    $problems[] = sprintf(
                        'a %s campaign needs an image %s, and none of the attached ones is',
                        $this->describeCampaignType($campaign->campaign_type)['label'],
                        DisplayImageShapes::describe($shape),
                    );
                }
            }
        }

        // Google campaign budgets are a daily amount. A lifetime total has no
        // direct equivalent, and dividing it silently would spend on a schedule
        // nobody agreed to.
        if ($campaign->usesLifetimeBudget()) {
            $problems[] = 'Google Ads takes a daily budget here, so a lifetime total cannot be published yet';
        }

        $unknown = array_diff(
            (array) ($campaign->placements ?? []),
            $this->placementsFor((string) $campaign->objective, $campaign->campaign_type),
        );

        if ($unknown !== []) {
            $problems[] = 'network not supported on Google Ads: '.implode(', ', $unknown);
        }

        // Search and Search partners are the same network as far as targeting
        // goes, and partners cannot be bought on their own.
        $chosen = (array) ($campaign->placements ?? []);

        if (in_array('search_partners', $chosen, true) && ! in_array('search', $chosen, true)) {
            $problems[] = 'search partners cannot run without the search network';
        }

        return $problems;
    }

    /**
     * Whether an attached creative is exactly this size.
     *
     * Read from the bytes rather than from the creative's recorded ratio: the
     * ratio is null on most rows, and "1:1" does not say whether the image is
     * 1200x1200 or 400x400, which is the difference Google enforces.
     */
    /**
     * Whether an attached creative is a usable image of that shape.
     *
     * This asked for exactly 1200x628 and exactly 1200x1200, which are
     * Google's recommendations rather than its requirements, so the gate
     * refused campaigns the publisher would have published and Google does
     * accept - a 1920x1005 and a 500x500 pair was reported as "needs a
     * creative at 1200x628" and then created by Google as a
     * RESPONSIVE_DISPLAY_AD on 346-085-5874.
     *
     * The shape is DisplayImageShapes' answer now, which is the same one
     * GooglePublisher sorts by, so the gate and the publish cannot disagree
     * about what a usable creative is.
     */
    private function hasImageShaped(Campaign $campaign, string $shape): bool
    {
        foreach ($campaign->ads()->with('asset')->get() as $ad) {
            $asset = $ad->asset;

            if ($asset === null || $asset->kind !== 'image') {
                continue;
            }

            $disk = Storage::disk($asset->disk ?: 'local');

            if (! $disk->exists($asset->path)) {
                continue;
            }

            $size = @getimagesizefromstring($disk->get($asset->path));

            if (is_array($size) && DisplayImageShapes::fits($shape, (int) $size[0], (int) $size[1])) {
                return true;
            }
        }

        return false;
    }

    /**
     * What Google needs that no other platform does.
     *
     * The landing page, the money, the dates, the creatives and the copy are
     * absent on purpose: those are the brief's, asked once however many
     * platforms the campaign runs on. That is what makes adding Google to an
     * existing Meta campaign five questions rather than fifteen.
     *
     * @return list<array{key: string, label: string, tool: string, offers: bool, ask?: string}>
     */
    public function steps(): array
    {
        return [
            // No ad_account step: see defaultAdAccountId(). There is one
            // account, it is set when the campaign is created, and asking which
            // one produced a question with no answers on screen.
            ['key' => 'campaign_type', 'label' => 'campaign type', 'tool' => 'choose_campaign_type', 'offers' => true,
                'ask' => 'What kind of Google campaign should this be?'],
            ['key' => 'objective', 'label' => 'campaign goal', 'tool' => 'choose_objective', 'offers' => true,
                'ask' => 'What should this campaign optimise for?'],
            ['key' => 'pixel', 'label' => 'conversion action', 'tool' => 'choose_pixel', 'offers' => true,
                'ask' => 'Which conversion action should this campaign count?'],
            ['key' => 'bidding', 'label' => 'bid strategy', 'tool' => 'set_bidding', 'offers' => true,
                'ask' => 'How should it bid?'],
            ['key' => 'targeting', 'label' => 'countries and networks', 'tool' => 'set_targeting', 'offers' => true,
                'ask' => 'Where should this run, and on which networks?'],
            ['key' => 'location_scope', 'label' => 'location detail', 'tool' => 'choose_location', 'offers' => true,
                'ask' => 'Target whole selected countries, or narrow to specific places?'],
            // Search only, and the one input a Search campaign cannot run without.
            // The publish gate has always refused a Search campaign with no
            // keyword, but nothing asked for one: set_search_keywords was
            // registered, named in no step and in no instruction, so a buyer
            // built the whole campaign and met the refusal at the end with no
            // step to go back to. Declared here, LaunchFlow places it with the
            // other targeting questions, before budget and creative.
            ['key' => 'search_keywords', 'label' => 'search keywords', 'tool' => 'set_search_keywords', 'offers' => true,
                'ask' => 'Which searches should trigger these ads?'],
            // Which child account under the manager. Placed by LaunchFlow with
            // the other platform setup questions, and only asked at all when
            // accountChoiceExists() - one permitted account is not a choice.
            ['key' => 'ad_account', 'label' => 'ad account', 'tool' => 'list_ad_accounts', 'offers' => true,
                'ask' => 'Which Google Ads account should this run on?'],
            // No utm step. Click tracking is set from the platform's own
            // macros when the campaign is created and the buyer is told,
            // rather than asked to choose the value that was going to be
            // used anyway. set_tracking still changes it on request.
        ];
    }

    /**
     * Google updates a campaign in place far more readily than Meta does, but an
     * ad's copy is still replaced rather than edited, so it is absent here.
     *
     * @return list<string>
     */
    /**
     * What GooglePublisher::update() can actually send.
     *
     * "locations" was claimed here and is not in the list any more. Nothing
     * pushed it, and a field declared editable but never sent is the worst
     * shape this can take: the edit is accepted, the snapshot moves, and the
     * campaign and the platform disagree with nothing saying so. Geo targets
     * are campaign criteria rather than campaign fields, so changing them means
     * diffing and mutating criteria, which is not built yet. Until it is, a
     * country change is refused as needing a rebuild rather than silently lost.
     *
     * @return list<string>
     */
    public function isConfigured(): bool
    {
        // One shared internal account, which exists only when both the refresh
        // token and the customer id are set.
        return GoogleAdsConnection::shared() !== null;
    }

    public function editableAfterPublish(): array
    {
        return ['name', 'budget', 'bid_strategy', 'bid_amount', 'starts_at', 'ends_at', 'placements'];
    }

    public function supportedAssetRatios(): array
    {
        return ['1.91:1', '1:1', '4:5'];
    }
}
