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

use ZOOlanders\YOOessentials\Source\GraphQL\AbstractQueryType;
use ZOOlanders\YOOessentials\Source\GraphQL\HasSourceInterface;
use ZOOlanders\YOOessentials\Source\Resolver\CachesResolvedSourceData;
use ZOOlanders\YOOessentials\Source\Resolver\LoadsSourceFromArgs;
use ZOOlanders\YOOessentials\Source\Provider\Facebook\FacebookSource;
use ZOOlanders\YOOessentials\Source\Provider\Facebook\HasApiRequest;

class FacebookPageReviewsQueryType extends AbstractQueryType implements HasSourceInterface
{
    use LoadsSourceFromArgs, CachesResolvedSourceData, HasApiRequest;

    public function name(): string
    {
        $id = $this->source()->id();

        return "facebook_{$id}_page_reviews_query";
    }

    public function label(): string
    {
        return "{$this->source()->name()} Page Reviews";
    }

    public static function getCacheKey(): string
    {
        return 'facebook-page-reviews';
    }

    public function config(): array
    {
        return [
            'fields' => [
                $this->name() => [
                    'type' => [
                        'listOf' => FacebookReviewType::TYPE_NAME,
                    ],

                    'args' => [
                        'limit' => [
                            'type' => 'Int',
                        ],
                        'cache' => [
                            'type' => 'Int',
                        ],
                    ],

                    'metadata' => [
                        'group' => 'Facebook',
                        'label' => $this->label(),
                        'fields' => [
                            'limit' => [
                                'label' => 'Limit',
                                'type' => 'yooessentials-number',
                                'description' => 'The maximum amount of posts to fetch.',
                                'default' => FacebookSource::MEDIA_LIMIT_DEFAULT,
                                'source' => true,
                                'attr' => [
                                    'max' => 100,
                                ],
                            ],

                            'cache' => $this->cacheField(),
                        ],
                    ],

                    'extensions' => [
                        'call' => [
                            'func' => __CLASS__ . '::resolve',
                            'args' => [
                                'source_id' => $this->source->id(),
                            ],
                        ],
                    ],
                ],
            ],
        ];
    }

    public static function resolve($root, array $args): array
    {
        /** @var FacebookSource */
        $source = self::loadSource($args, FacebookSource::class);

        if (!$source) {
            return [];
        }

        /** @var $api FacebookApi|null */
        $api = self::api($source->account());

        if (!$api) {
            return [];
        }

        return self::resolveFromCache($source, $args, function () use ($source, $api, $args) {
            return $api->reviews($source->pageId(), $args);
        });
    }
}
