<?php

namespace FluentPlayer\App\Hooks\Handlers;
if (!defined('ABSPATH')) exit;

use FluentPlayer\App\Models\Media;
use FluentPlayer\App\Services\MediaRenderer;
use FluentPlayer\Framework\Support\Arr;

class MediaShortcodeHandler
{
    /**
     * Handle the shortcode
     *
     * @param array $atts     Shortcode attributes
     * @param string $content Shortcode content
     *
     * @return string HTML output
     */
    public function handle($atts, $content = '')
    {
        $data = [
            'id' => null,
        ];
        // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.InvalidPrefixPassed -- Filter hook name is a valid string, not a PHP identifier
        $shortcodeDefaults = apply_filters('fluent_player/media_shortcode_defaults', $data, $atts);
        $atts = shortcode_atts($shortcodeDefaults, $atts);

        $id = Arr::get($atts, 'id');
        if (!intval($id)) {
            return '';
        }

        $mediaId = absint($id);

        if (!Media::findVisible($mediaId)) {
            return '';
        }

        return MediaRenderer::renderFromPost($mediaId);
    }
}
