<?php
/**
 * @package   Essentials YOOtheme Pro 2.2.16 build 0813.1608
 * @author    ZOOlanders https://www.zoolanders.com
 * @copyright Copyright (C) Joolanders, SL
 * @license   http://www.gnu.org/licenses/gpl.html GNU/GPL
 */

namespace ZOOlanders\YOOessentials\Source\Provider\GoogleBusinessProfile\Type;

use ZOOlanders\YOOessentials\Source\GraphQL\AbstractObjectType;
use ZOOlanders\YOOessentials\Source\GraphQL\HasSourceInterface;
use ZOOlanders\YOOessentials\Util;

class GoogleBusinessProfileReviewerType extends AbstractObjectType implements HasSourceInterface
{
    public const TYPE_NAME = 'GoogleMyBusinessReviewer';
    public const TYPE_LABEL = 'Profile Reviewer';

    public function name(): string
    {
        return self::TYPE_NAME;
    }

    public function label(): string
    {
        return self::TYPE_LABEL;
    }

    public function config(): array
    {
        return [
            'metadata' => [
                'type' => true,
                'label' => $this->label(),
            ],
            'fields' => [
                'displayName' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Display Name',
                    ],
                ],
                'profilePhotoUrl' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Profile Photo URL',
                    ],
                    'extensions' => [
                        'call' => __CLASS__ . '::resolveProfileUrl',
                    ],
                ],
                'isAnonymous' => [
                    'type' => 'Boolean',
                    'metadata' => [
                        'label' => 'Is Anonymous',
                    ],
                ],
            ],
        ];
    }

    public static function resolveProfileUrl(array $reviewer): string
    {
        $url = $reviewer['profilePhotoUrl'] ?? '';
        $cacheKey = 'gbp-reviewer-photo-' . sha1($url);

        return Util\File::cacheMedia($url, $cacheKey);
    }
}
