<?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\GooglePhotos\Type;

use ZOOlanders\YOOessentials\Source\GraphQL\TypeInterface;
use ZOOlanders\YOOessentials\Util;

class GooglePhotosAlbumType implements TypeInterface
{
    public const TYPE_NAME = 'GooglePhotosAlbum';
    public const TYPE_LABEL = 'Google Photos Album';

    public function type(): string
    {
        return TypeInterface::TYPE_OBJECT;
    }

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

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

    public function config(): array
    {
        return [
            'fields' => [
                'id' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'ID',
                    ],
                ],
                'title' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Title',
                        'filters' => ['limit'],
                    ],
                ],
                'coverPhotoBaseUrl' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Cover Photo URL',
                    ],
                    'extensions' => [
                        'call' => __CLASS__ . '::resolveCoverPhotoUrl',
                    ],
                ],
                'mediaItemsCount' => [
                    'type' => 'Int',
                    'metadata' => [
                        'label' => 'Total Media Count',
                    ],
                ],
            ],

            'metadata' => [
                'type' => true,
                'label' => $this->label(),
            ],
        ];
    }

    public static function resolveCoverPhotoUrl(array $album): string
    {
        $id = $album['id'];
        $url = $album['coverPhotoBaseUrl'] ?? '';

        if ($id && $url) {
            return Util\File::cacheMedia($url, "google-photos-album-{$id}");
        }

        return '';
    }
}
