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

use ZOOlanders\YOOessentials\Source\Provider\Airtable\AirtableSource;
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\Type\SourceInterface;

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

    private AirtableRecordType $recordType;

    public function __construct(SourceInterface $source, AirtableRecordType $recordType)
    {
        parent::__construct($source);

        $this->recordType = $recordType;
    }

    public static function getCacheKey(): string
    {
        return 'airtable-record-query';
    }

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

        return "airtableRecord_{$id}_query";
    }

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

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

                    'args' => [
                        'recordId' => [
                            'type' => 'String',
                        ],
                        'cache' => [
                            'type' => 'Int',
                        ],
                    ],

                    'metadata' => [
                        'group' => 'Airtable',
                        'label' => $this->label(),
                        'fields' => [
                            'recordId' => [
                                'label' => 'Record ID',
                                'description' => 'The record ID which to query.',
                                'source' => true,
                            ],

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

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

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

        $recordId = $args['recordId'] ?? null;

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

        $result = self::resolveFromCache($source, $args, function () use ($source, $recordId) {
            return $source->api()->getRecord($source->base, $source->table, $recordId);
        });

        return $result;
    }
}
