<?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 GoogleBusinessProfilePostType extends AbstractObjectType implements HasSourceInterface
{
    public const TYPE_NAME = 'GoogleBusinessProfilePost';
    public const TYPE_LABEL = 'Location Post';

    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' => [
                'name' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'ID',
                    ],
                ],
                'searchUrl' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'URL',
                    ],
                ],
                'topicType' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Topic Type',
                    ],
                ],
                'summary' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Summary',
                        'filters' => ['limit'],
                    ],
                    'extensions' => [
                        'call' => [
                            'func' => static::class . '::resolveSummary',
                        ],
                    ],
                ],
                'primaryMedia' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Primary Media URL',
                    ],
                    'extensions' => [
                        'call' => [
                            'func' => static::class . '::resolvePrimaryMedia',
                        ],
                    ],
                ],
                'languageCode' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Language',
                    ],
                ],
                'createTime' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Created At',
                        'filters' => ['date'],
                    ],
                ],
                'updateTime' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Updated At',
                        'filters' => ['date'],
                    ],
                ],
                'offer' => [
                    'type' => GoogleBusinessProfilePostOfferType::TYPE_NAME,
                    'metadata' => [
                        'label' => 'Offer',
                    ],
                ],
                'media' => [
                    'type' => [
                        'listOf' => GoogleBusinessProfileMediaType::TYPE_NAME,
                    ],
                    'metadata' => [
                        'label' => 'Media',
                    ],
                ],
            ],
        ];
    }

    public static function resolveSummary(array $post): string
    {
        return nl2br($post['summary'] ?? '');
    }

    public static function resolvePrimaryMedia(array $post): ?string
    {
        $name = sha1($post['name'] ?? '');
        $url = $post['media'][0]['googleUrl'] ?? '';

        if (!$url) {
            return null;
        }

        return Util\File::cacheMedia($url, "gbp-post-media-$name");
    }
}
