<?php
/**
 * Structure Viewer — WP-CLI commands
 */

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

if ( class_exists( 'WP_CLI' ) ) {
    class Structure_Viewer_CLI_Command extends WP_CLI_Command {

        /**
         * Export plugin or theme structure
         *
         * ## OPTIONS
         *
         * <type>
         * : Type of item (plugin or theme)
         *
         * <n>
         * : Name of the plugin or theme
         *
         * [--format=<format>]
         * : Export format (txt, json, xml)
         * ---
         * default: txt
         * options:
         *   - txt
         *   - json
         *   - xml
         * ---
         *
         * [--output=<file>]
         * : Output file path
         *
         * ## EXAMPLES
         *
         * wp structure-viewer export plugin akismet --format=json
         * wp structure-viewer export theme twentytwentyone --output=/path/to/structure.txt
         */
        public function export( $args, $assoc_args ) {
            $type        = $args[0];
            $name        = $args[1];
            $format      = isset( $assoc_args['format'] ) ? $assoc_args['format'] : 'txt';
            $output_file = isset( $assoc_args['output'] ) ? $assoc_args['output'] : null;

            if ( ! in_array( $type, [ 'plugin', 'theme' ], true ) ) {
                WP_CLI::error( 'Type must be either "plugin" or "theme"' );
            }
            if ( ! in_array( $format, [ 'txt', 'json', 'xml' ], true ) ) {
                WP_CLI::error( 'Format must be one of: txt, json, xml' );
            }

            $items_dir = ( $type === 'plugin' ) ? WP_PLUGIN_DIR : get_theme_root();
            $item_path = realpath( $items_dir . '/' . $name );

            if ( $item_path === false || ! is_dir( $item_path ) ) {
                WP_CLI::error( "{$type} '{$name}' not found." );
            }

            if ( $format === 'json' ) {
                $structure = SV_Exporter::build_json( $item_path, $name, $type );
            } elseif ( $format === 'xml' ) {
                $structure = SV_Exporter::build_xml( $item_path, $name, $type );
            } else {
                $structure  = $name . ' (' . ( $type === 'plugin' ? 'Plugin' : 'Theme' ) . ")\n";
                $structure .= SV_Exporter::build_txt( $item_path, $item_path );
                $structure .= SV_Scanner::promo_note();
            }

            if ( $output_file ) {
                $written = @file_put_contents( $output_file, $structure ); // phpcs:ignore
                if ( $written === false ) {
                    WP_CLI::error( "Could not write to: {$output_file}" );
                }
                WP_CLI::success( "Structure exported to: {$output_file}" );
            } else {
                WP_CLI::line( $structure );
            }
        }

        /**
         * List all plugins or themes
         *
         * ## OPTIONS
         *
         * <type>
         * : Type of items to list (plugins or themes)
         *
         * ## EXAMPLES
         *
         * wp structure-viewer list plugins
         * wp structure-viewer list themes
         */
        public function list( $args ) {
            $type = $args[0];

            if ( $type === 'plugins' ) {
                $items          = glob( WP_PLUGIN_DIR . '/*', GLOB_ONLYDIR ) ?: [];
                $item_data_list = SV_ItemData::get_plugin_list( $items );
            } elseif ( $type === 'themes' ) {
                $items          = glob( get_theme_root() . '/*', GLOB_ONLYDIR ) ?: [];
                $item_data_list = SV_ItemData::get_theme_list( $items );
            } else {
                WP_CLI::error( 'Type must be either "plugins" or "themes"' );
            }

            $table_data = [];
            foreach ( $item_data_list as $folder => $data ) {
                $table_data[] = [
                    'name'   => $data['Name'] ?? $folder,
                    'folder' => $folder,
                ];
            }

            WP_CLI\Utils\format_items( 'table', $table_data, [ 'name', 'folder' ] );
        }
    }

    WP_CLI::add_command( 'structure-viewer', 'Structure_Viewer_CLI_Command' );
}
