<?php

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Twig\Components\Social;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Manager\SocialNetworkProviderManager;
use CpCreation\VitiCore\Bridge\Symfony\Bundle\Provider\SocialNetwork\BasicInstagramProvider;
use CpCreation\VitiCore\Bridge\Symfony\Bundle\Twig\Components\Behavior\HasTemplateOverride;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Attribute\PreMount;

#[AsTwigComponent('social:InstagramMedias', template: '@components/social/InstagramMedias.html.twig')]
final class InstagramMedias
{
    use HasTemplateOverride;

    private const ALLOWED_LIMITS = [3, 6, 9, 12];
    private const FETCH_LIMIT = 12;

    public int $limit = 6;

    /** @var list<array<string, mixed>>|null */
    private ?array $medias = null;

    public function __construct(
        private readonly SocialNetworkProviderManager $socialNetworkProviderManager,
    ) {
    }

    /**
     * @param array<string, mixed> $data
     *
     * @return array<string, mixed>
     */
    #[PreMount]
    public function preMount(array $data): array
    {
        if (!\array_key_exists('limit', $data)) {
            return $data;
        }

        $limit = $data['limit'];
        if (!\is_int($limit) && !(\is_string($limit) && ctype_digit($limit))) {
            throw new \InvalidArgumentException('La prop "limit" du composant social:InstagramMedias doit être 3, 6, 9 ou 12.');
        }

        $limit = (int) $limit;
        if (!\in_array($limit, self::ALLOWED_LIMITS, true)) {
            throw new \InvalidArgumentException('La prop "limit" du composant social:InstagramMedias doit être 3, 6, 9 ou 12.');
        }

        $data['limit'] = $limit;

        return $data;
    }

    /**
     * @return list<array<string, mixed>>
     */
    public function getMedias(): array
    {
        if (null !== $this->medias) {
            return $this->medias;
        }

        try {
            $medias = $this->socialNetworkProviderManager->getMedias(BasicInstagramProvider::TYPE, self::FETCH_LIMIT);
        } catch (\RuntimeException) {
            return $this->medias = [];
        }

        if (!\is_array($medias)) {
            return $this->medias = [];
        }

        $medias = array_filter(
            $medias,
            static fn (mixed $media): bool => \is_array($media)
                && isset($media['permalink'])
                && isset($media['media_type'])
                && (
                    ('VIDEO' === $media['media_type'] && isset($media['thumbnail_url']))
                    || ('VIDEO' !== $media['media_type'] && isset($media['media_url']))
                )
        );

        return $this->medias = \array_slice(array_values($medias), 0, $this->limit);
    }
}
