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

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

class InstagramBusinessUserType implements TypeInterface
{
    public const TYPE_NAME = 'InstagramBusinessUser';
    public const TYPE_LABEL = 'Instagram Business User';

    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',
                    ],
                ],
                'name' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Name',
                    ],
                ],
                'website' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Website',
                    ],
                ],
                'biography' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Biography',
                        'filters' => ['limit'],
                    ],
                ],
                'profile_picture_url' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Picture URL',
                    ],
                    'extensions' => [
                        'call' => __CLASS__ . '::profilePictureUrl',
                    ],
                ],
                'followers_count' => [
                    'type' => 'Int',
                    'metadata' => [
                        'label' => 'Total Followers',
                    ],
                ],
                'follows_count' => [
                    'type' => 'Int',
                    'metadata' => [
                        'label' => 'Total Follows',
                    ],
                ],
                'media_count' => [
                    'type' => 'Int',
                    'metadata' => [
                        'label' => 'Total Media',
                    ],
                ],
            ],

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

    public static function profilePictureUrl($user)
    {
        $id = $user['id'] ?? '';
        $url = $user['profile_picture_url'] ?? '';

        if (!$url || !$id) {
            return '';
        }

        return Util\File::cacheMedia($url, "ig-media-user-$id");
    }
}
