<?php
/**
 * TamRank Media Library Integration
 *
 * Voegt AI Alt Text generatie toe aan WordPress Media Library
 *
 * @package TamRank
 * @since 6.4.0
 */

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Media Library Integration klasse
 */
class TamRank_Media_Library_Integration
{

    /**
     * Singleton instance
     *
     * @var TamRank_Media_Library_Integration
     */
    private static $instance = null;

    /**
     * Singleton get instance
     *
     * @return TamRank_Media_Library_Integration
     */
    public static function get_instance()
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Constructor
     */
    private function __construct()
    {
        // Voeg custom velden toe aan media modal
        add_filter('attachment_fields_to_edit', array($this, 'add_ai_alt_field'), 10, 2);

        // Enqueue scripts en styles voor media library
        add_action('admin_enqueue_scripts', array($this, 'enqueue_media_scripts'));
    }

    /**
     * Voeg AI Alt Text generator toe aan media modal
     *
     * @param array $form_fields Array van form velden
     * @param WP_Post $post Attachment post object
     * @return array Gewijzigde form velden
     */
    public function add_ai_alt_field($form_fields, $post)
    {
        // Alleen tonen voor afbeeldingen
        if (strpos($post->post_mime_type, 'image/') !== 0) {
            return $form_fields;
        }

        // FREE renders the upsell unconditionally. When the PRO add-on is active
        // and licensed, it replaces this field with the generator UI via its own
        // attachment_fields_to_edit filter at priority 11 (cluster-4 strip).
        ob_start();
        ?>
        <div class="tamrank-ai-alt-generator" data-attachment-id="<?php echo esc_attr($post->ID); ?>">
            <!-- PRO upgrade message — small inline -->
            <div class="tamrank-pro-upgrade">
                <div class="tamrank-pro-upgrade__row">
                    <span class="tamrank-pro-upgrade__icon">
                        <svg width="16" height="16" fill="none" viewBox="0 0 24 24"><path d="M12 2L15.09 8.26L22 9.27L17 14.14L18.18 21.02L12 17.77L5.82 21.02L7 14.14L2 9.27L8.91 8.26L12 2Z" fill="#10b981"/></svg>
                    </span>
                    <span class="tamrank-pro-upgrade__text"><?php tamrank_e('generate_alt_text_with_ai', 'Generate alt text with AI'); ?></span>
                    <span class="tamrank-pro-upgrade__badge">PRO</span>
                </div>
                <div class="tamrank-pro-upgrade__link-row">
                    <a href="<?php echo esc_url(tam_rank_get_upgrade_url('media_library', 'ai_alt_text')); ?>"
                        class="tamrank-pro-upgrade__link" target="_blank">
                        <?php tamrank_e('discover_pro', 'Discover PRO'); ?> &rarr;
                    </a>
                </div>
            </div>
        </div>
        <?php
        $html = ob_get_clean();

        $form_fields['tamrank_ai_alt'] = array(
            'label' => '', // Lege label voor full-width layout
            'input' => 'html',
            'html' => $html,
            'show_in_edit' => true,
            'helps' => '', // Verberg de help text
        );

        return $form_fields;
    }

    /**
     * Enqueue scripts en styles voor media library
     *
     * @param string $hook Current admin page hook
     */
    public function enqueue_media_scripts($hook)
    {
        // Alleen op media library pages
        if (!in_array($hook, array('upload.php', 'post.php', 'post-new.php'))) {
            return;
        }

        // FREE only ships the upsell + field-container styling. The generator
        // JS, localize and widget CSS moved to the tam-rank-pro add-on, which
        // enqueues them on top of this handle (cluster-4 strip).
        wp_enqueue_style(
            'tamrank-ai-image-alt',
            TAM_RANK_PLUGIN_URL . 'assets/css/ai-image-alt.css',
            array('tamrank-variables'),
            TAM_RANK_VERSION
        );

        // On the full attachment edit screen, move the TamRank fields into the
        // side column under the "Save" box. The media-insert modal is untouched.
        if ($hook === 'post.php') {
            $screen = get_current_screen();
            if ($screen && isset($screen->post_type) && $screen->post_type === 'attachment') {
                wp_enqueue_script(
                    'tamrank-media-edit',
                    TAM_RANK_PLUGIN_URL . 'assets/js/tamrank-media-edit.js',
                    array(),
                    TAM_RANK_VERSION,
                    true
                );
            }
        }
    }
}

/**
 * Initialize Media Library Integration
 */
function tamrank_media_library_integration()
{
    return TamRank_Media_Library_Integration::get_instance();
}
