<?php

namespace App\Campaigns;

/**
 * The pairings Meta enforces between an objective, what it optimises toward,
 * and the pixel event it counts.
 *
 * Kept in one place because getting a pair wrong is not a validation error you
 * can reason about from the message: sending PURCHASE under a leads objective
 * comes back as "conversion event unavailable", subcode 2446814.
 */
class Objectives
{
    public const SUPPORTED = ['OUTCOME_SALES', 'OUTCOME_LEADS', 'OUTCOME_TRAFFIC'];

    private const OPTIMIZATION_GOAL = [
        'OUTCOME_SALES' => 'OFFSITE_CONVERSIONS',
        'OUTCOME_LEADS' => 'OFFSITE_CONVERSIONS',
        'OUTCOME_TRAFFIC' => 'LINK_CLICKS',
    ];

    private const CONVERSION_EVENT = [
        'OUTCOME_SALES' => 'PURCHASE',
        'OUTCOME_LEADS' => 'LEAD',
        'OUTCOME_TRAFFIC' => null,
    ];

    public const CALLS_TO_ACTION = [
        'SHOP_NOW', 'LEARN_MORE', 'SIGN_UP', 'GET_OFFER', 'SUBSCRIBE', 'CONTACT_US', 'BOOK_TRAVEL',
    ];

    /**
     * LOWEST_COST_WITHOUT_CAP spends the budget freely. COST_CAP holds an
     * average cost per result, which is how a target CPL or CPA is expressed.
     */
    public const BID_STRATEGIES = ['LOWEST_COST_WITHOUT_CAP', 'COST_CAP', 'LOWEST_COST_WITH_BID_CAP'];

    public const DEVICES = ['mobile', 'desktop'];

    public const PLACEMENTS = ['facebook', 'instagram', 'messenger', 'audience_network', 'threads'];

    public static function supports(?string $objective): bool
    {
        return in_array($objective, self::SUPPORTED, true);
    }

    public static function optimizationGoal(string $objective): string
    {
        return self::OPTIMIZATION_GOAL[$objective] ?? 'LINK_CLICKS';
    }

    public static function conversionEvent(?string $objective): ?string
    {
        return self::CONVERSION_EVENT[$objective] ?? null;
    }

    public static function needsPixel(?string $objective): bool
    {
        return $objective !== null && $objective !== 'OUTCOME_TRAFFIC';
    }
}
