<?php

namespace App\Campaigns;

/**
 * How a chosen Meta conversion is stored, and the promoted object it publishes as.
 *
 * Standard events stay as Meta's enum (LEAD, PURCHASE). A raw custom event keeps
 * its name, because publishing OTHER alone optimises toward the generic bucket
 * and not attestpathcustom. A custom conversion keeps its id. Both sit in the
 * existing conversion_event column, so no schema change is required.
 */
class MetaPromotedConversion
{
    public static function isCustomEventName(string $name): bool
    {
        return preg_match('/^[A-Za-z][A-Za-z0-9_]{0,39}$/', $name) === 1;
    }

    public static function storedCustomEvent(string $name): string
    {
        return 'event:'.$name;
    }

    public static function storedCustomConversion(string $id): string
    {
        return 'conversion:'.$id;
    }

    public static function customEventName(?string $stored): ?string
    {
        if (! is_string($stored) || ! str_starts_with($stored, 'event:')) {
            return null;
        }

        $name = substr($stored, strlen('event:'));

        return self::isCustomEventName($name) ? $name : null;
    }

    public static function customConversionId(?string $stored): ?string
    {
        if (! is_string($stored) || ! str_starts_with($stored, 'conversion:')) {
            return null;
        }

        $id = substr($stored, strlen('conversion:'));

        return preg_match('/^[0-9]+$/', $id) === 1 ? $id : null;
    }

    /**
     * The custom_event_type a stored value publishes as.
     *
     * Null for a custom conversion, which publishes by id and has no enum.
     */
    public static function eventType(?string $stored): ?string
    {
        if ($stored === null || $stored === '' || self::customConversionId($stored) !== null) {
            return null;
        }

        if (self::customEventName($stored) !== null) {
            return 'OTHER';
        }

        return $stored;
    }

    /** What the campaign panel shows for a stored value. */
    public static function label(?string $stored): ?string
    {
        if ($stored === null || $stored === '') {
            return null;
        }

        if (($name = self::customEventName($stored)) !== null) {
            return $name;
        }

        if (self::customConversionId($stored) !== null) {
            return 'Custom conversion';
        }

        return Objectives::eventName($stored) ?: $stored;
    }

    /**
     * @return array<string, string>
     */
    public static function promotedObject(string $pixelId, string $stored): array
    {
        if (($id = self::customConversionId($stored)) !== null) {
            return [
                'pixel_id' => $pixelId,
                'custom_conversion_id' => $id,
            ];
        }

        if (($name = self::customEventName($stored)) !== null) {
            return [
                'pixel_id' => $pixelId,
                'custom_event_type' => 'OTHER',
                'custom_event_str' => $name,
            ];
        }

        return [
            'pixel_id' => $pixelId,
            'custom_event_type' => $stored,
        ];
    }
}
