<?php

namespace App\Support;

class PromptLandingPagePixels
{
    public const GOOGLE = 'google';

    public const META = 'meta';

    public static function provider(?string $utmSource): ?string
    {
        $source = strtolower(trim((string) $utmSource));

        return match ($source) {
            'google' => self::GOOGLE,
            'facebook', 'fb', 'meta' => self::META,
            default => null,
        };
    }

    public static function googleAccountId(string $sendTo): ?string
    {
        if (preg_match('/^(AW-\d+)/', $sendTo, $matches) !== 1) {
            return null;
        }

        return $matches[1];
    }

    public static function isSafeId(string $value): bool
    {
        return $value !== '' && preg_match('/^[A-Za-z0-9_\/-]+$/', $value) === 1;
    }

    public static function isGoogleSendTo(string $value): bool
    {
        return self::isSafeId($value) && self::googleAccountId($value) !== null;
    }

    public static function isMetaPixelId(string $value): bool
    {
        return preg_match('/^\d{1,20}$/', $value) === 1;
    }
}
