<?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\AbstractQueryType;
use ZOOlanders\YOOessentials\Source\GraphQL\HasSourceInterface;
use ZOOlanders\YOOessentials\Source\Resolver\CachesResolvedSourceData;
use ZOOlanders\YOOessentials\Source\Resolver\LoadsSourceFromArgs;
use ZOOlanders\YOOessentials\Source\Provider\GoogleBusinessProfile\GoogleBusinessProfileController;
use ZOOlanders\YOOessentials\Source\Provider\GoogleBusinessProfile\GoogleBusinessProfileSource;

class GoogleBusinessProfileReviewQuery extends AbstractQueryType implements HasSourceInterface
{
    use LoadsSourceFromArgs, CachesResolvedSourceData;

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

        return "googleMyBusinessReview_{$id}_query";
    }

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

    public static function getCacheKey(): string
    {
        return 'google-business-profile-review';
    }

    public function config(): array
    {
        return [
            'fields' => [
                $this->name() => [
                    'type' => GoogleBusinessProfileReviewType::TYPE_NAME,
                    'args' => [
                        'review_id' => [
                            'type' => 'String',
                        ],
                        'cache' => [
                            'type' => 'Int',
                        ],
                    ],
                    'metadata' => [
                        'group' => 'Google Business Profile',
                        'label' => $this->label(),
                        'fields' => [
                            'review_id' => [
                                'label' => 'Review',
                                'type' => 'yooessentials-select-dropdown-async',
                                'description' => 'The location review which content to fetch.',
                                'endpoint' => GoogleBusinessProfileController::GET_REVIEWS_ENDPOINT,
                                'source' => true,
                                'params' => [
                                    'source_id' => $this->source->id(),
                                ],
                            ],
                            'cache' => $this->cacheField(),
                        ],
                    ],
                    'extensions' => [
                        'call' => [
                            'func' => static::class . '::resolve',
                            'args' => [
                                'source_id' => $this->source->id(),
                            ],
                        ],
                    ],
                ],
            ],
        ];
    }

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

        $reviewId = $args['review_id'] ?? null;

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

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