<?php
/**
 * Structure Viewer — Item Data helpers
 *
 * Plugin and theme metadata lookups.
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit( 'Direct access denied.' );
}

final class SV_ItemData {

    /**
     * Return an array of [ folder => ['Name' => ...] ] for the given plugin dirs.
     * Sorted alphabetically by Name.
     */
    public static function get_plugin_list( array $dirs ) {
        if ( ! function_exists( 'get_plugin_data' ) ) {
            require_once ABSPATH . 'wp-admin/includes/plugin.php';
        }

        $list = [];
        foreach ( $dirs as $dir ) {
            $dir = realpath( $dir );
            if ( ! $dir || ! is_dir( $dir ) ) {
                continue;
            }
            $folder = basename( $dir );
            $main   = self::find_main_plugin_file( $dir );
            $list[ $folder ] = $main
                ? ( get_plugin_data( $main, false, false ) ?: [ 'Name' => $folder ] )
                : [ 'Name' => $folder ];
        }

        uasort( $list, fn( $a, $b ) => strcasecmp( $a['Name'], $b['Name'] ) );
        return $list;
    }

    /**
     * Return an array of [ folder => ['Name' => ...] ] for the given theme dirs.
     */
    public static function get_theme_list( array $dirs ) {
        $list = [];
        foreach ( $dirs as $dir ) {
            $dir = realpath( $dir );
            if ( ! $dir || ! is_dir( $dir ) ) {
                continue;
            }
            $folder = basename( $dir );
            $theme  = wp_get_theme( $folder );
            $list[ $folder ] = [
                'Name' => $theme->exists() ? ( $theme->get( 'Name' ) ?: $folder ) : $folder,
            ];
        }

        uasort( $list, fn( $a, $b ) => strcasecmp( $a['Name'], $b['Name'] ) );
        return $list;
    }

    /**
     * Find the root PHP file that contains the "Plugin Name:" header.
     */
    public static function find_main_plugin_file( $plugin_dir ) {
        $plugin_dir = realpath( $plugin_dir );
        if ( ! $plugin_dir || ! is_dir( $plugin_dir ) ) {
            return false;
        }

        $php_files = glob( $plugin_dir . '/*.php' );
        if ( empty( $php_files ) ) {
            return false;
        }

        foreach ( $php_files as $file ) {
            $file = realpath( $file );
            if ( ! $file || strpos( $file, $plugin_dir ) !== 0 || ! is_readable( $file ) ) {
                continue;
            }
            $content = file_get_contents( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions
            if ( $content && preg_match( '/Plugin Name:/i', $content ) ) {
                return $file;
            }
        }

        return false;
    }

    /**
     * Build <option> HTML for a plugin or theme selector.
     */
    public static function get_item_options( $type, $selected ) {
        $html = '';

        if ( $type === 'plugin' ) {
            $html .= '<option value="">' . esc_html__( 'All Plugins', 'structure-viewer' ) . '</option>';
            $dirs  = glob( WP_PLUGIN_DIR . '/*', GLOB_ONLYDIR ) ?: [];
            $list  = self::get_plugin_list( $dirs );
        } else {
            $html .= '<option value="">' . esc_html__( 'All Themes', 'structure-viewer' ) . '</option>';
            $dirs  = glob( get_theme_root() . '/*', GLOB_ONLYDIR ) ?: [];
            $list  = self::get_theme_list( $dirs );
        }

        foreach ( $list as $folder => $data ) {
            $name  = esc_html( $data['Name'] );
            $sel   = selected( $selected, $folder, false );
            $html .= "<option value='" . esc_attr( $folder ) . "' $sel data-name='" . esc_attr( $name ) . "'>" . $name . '</option>';
        }

        return $html;
    }
}