<?php

/**
 * Read-only code viewer metabox on built-plugin edit screens.
 *
 * @package LolaCore\PluginBuilder\Admin
 * @license GPL-2.0-or-later
 */

declare(strict_types=1);

namespace LolaCore\PluginBuilder\Admin;

use LolaCore\PluginBuilder\PostType\PluginPostType;

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

/**
 * Registers the Generated files metabox (PHP / JS / CSS tabs).
 */
class PluginEditScreen {

	/**
	 * Register metabox hooks.
	 */
	public function register(): void {
		add_action('add_meta_boxes', array($this, 'registerMetaBoxes'));
	}

	/**
	 * @param string $post_type Current post type.
	 */
	public function registerMetaBoxes( string $post_type ): void {
		if ($post_type !== PluginPostType::POST_TYPE) {
			return;
		}

		add_meta_box(
			'lolacore_pb_generated_files',
			__('Generated files', 'lolacore'),
			array($this, 'renderMetaBox'),
			PluginPostType::POST_TYPE,
			'normal',
			'high'
		);
	}

	/**
	 * @param \WP_Post $post Current post.
	 */
	public function renderMetaBox( \WP_Post $post ): void {
		$post_id = (int) $post->ID;

		$slug        = (string) get_post_meta($post_id, PluginPostType::META_SLUG, true);
		$version     = max(1, (int) get_post_meta($post_id, PluginPostType::META_VERSION, true));
		$confidence  = (int) get_post_meta($post_id, PluginPostType::META_CONFIDENCE, true);
		$loadStatus  = (string) get_post_meta($post_id, PluginPostType::META_LOAD_SAFE_STATUS, true);
		$note        = (string) get_post_meta($post_id, PluginPostType::META_CONFIDENCE_NOTE, true);
		$description = (string) get_post_meta($post_id, PluginPostType::META_DESCRIPTION, true);
		$suggestions = get_post_meta($post_id, PluginPostType::META_SUGGESTIONS, true);
		if (! is_array($suggestions)) {
			$suggestions = array();
		}

		$php = (string) get_post_meta($post_id, PluginPostType::META_PHP, true);
		$js  = (string) get_post_meta($post_id, PluginPostType::META_JS, true);
		$css = (string) get_post_meta($post_id, PluginPostType::META_CSS, true);

		$incomplete = ($php === '' && $js === '' && $css === '');

		?>
		<div class="lolacore-pb-edit">
			<?php if ($incomplete) : ?>
				<p class="lolacore-pb-notice lolacore-pb-notice-warning">
					<?php esc_html_e('This plugin has incomplete generated files.', 'lolacore'); ?>
				</p>
			<?php endif; ?>

			<div class="lolacore-pb-meta-grid">
				<div class="lolacore-pb-meta-item">
					<span class="lolacore-pb-meta-label"><?php esc_html_e('Slug', 'lolacore'); ?></span>
					<code class="lolacore-pb-meta-value"><?php echo esc_html($slug !== '' ? $slug : '—'); ?></code>
				</div>
				<div class="lolacore-pb-meta-item">
					<span class="lolacore-pb-meta-label"><?php esc_html_e('Version', 'lolacore'); ?></span>
					<span class="lolacore-pb-meta-value"><?php echo esc_html((string) $version); ?></span>
				</div>
				<div class="lolacore-pb-meta-item lolacore-pb-meta-item-wide">
					<span class="lolacore-pb-meta-label"><?php esc_html_e('Status', 'lolacore'); ?></span>
					<span class="lolacore-pb-meta-value"><?php echo esc_html(LibraryColumns::installStatusLabel($post_id)); ?></span>
				</div>
				<?php
				$loadLabel = ReviewQualityLabels::loadTestLabel(array('status' => $loadStatus));
				if ($loadLabel !== '') :
					?>
				<div class="lolacore-pb-meta-item">
					<span class="lolacore-pb-meta-label"><?php esc_html_e('Load test', 'lolacore'); ?></span>
					<span class="lolacore-pb-meta-value"><?php echo esc_html($loadLabel); ?></span>
				</div>
				<?php endif; ?>
				<?php
				$reviewLabel = ReviewQualityLabels::codeReviewLabel($confidence);
				if ($reviewLabel !== '') :
					?>
				<div class="lolacore-pb-meta-item">
					<span class="lolacore-pb-meta-label"><?php esc_html_e('Code review', 'lolacore'); ?></span>
					<span class="lolacore-pb-meta-value"><?php echo esc_html($reviewLabel); ?></span>
				</div>
				<?php endif; ?>
			</div>

			<?php if ($note !== '') : ?>
				<p class="lolacore-pb-confidence-note">
					<strong><?php esc_html_e('Review note', 'lolacore'); ?>:</strong>
					<?php echo esc_html($note); ?>
				</p>
			<?php endif; ?>

			<div class="lolacore-pb-meta-item lolacore-pb-meta-item-wide">
				<span class="lolacore-pb-meta-label"><?php esc_html_e('Description', 'lolacore'); ?></span>
				<textarea class="lolacore-pb-readonly" rows="4" readonly><?php echo esc_textarea($description); ?></textarea>
			</div>

			<?php if (! empty($suggestions)) : ?>
				<div class="lolacore-pb-meta-item lolacore-pb-meta-item-wide">
					<span class="lolacore-pb-meta-label"><?php esc_html_e('Suggestions', 'lolacore'); ?></span>
					<ul class="lolacore-pb-suggestions">
						<?php foreach ($suggestions as $suggestion) : ?>
							<?php if (is_scalar($suggestion) && (string) $suggestion !== '') : ?>
								<li><?php echo esc_html((string) $suggestion); ?></li>
							<?php endif; ?>
						<?php endforeach; ?>
					</ul>
				</div>
			<?php endif; ?>

			<div class="lolacore-pb-tabs" role="tablist" aria-label="<?php esc_attr_e('Generated source files', 'lolacore'); ?>">
				<button type="button" class="lolacore-pb-tab is-active" data-tab="php" role="tab" aria-selected="true">
					<?php esc_html_e('PHP', 'lolacore'); ?>
				</button>
				<button type="button" class="lolacore-pb-tab" data-tab="js" role="tab" aria-selected="false">
					<?php esc_html_e('JS', 'lolacore'); ?>
				</button>
				<button type="button" class="lolacore-pb-tab" data-tab="css" role="tab" aria-selected="false">
					<?php esc_html_e('CSS', 'lolacore'); ?>
				</button>
			</div>

			<div class="lolacore-pb-tab-panels">
				<?php
				$this->renderCodePanel('php', $php, true);
				$this->renderCodePanel('js', $js, false);
				$this->renderCodePanel('css', $css, false);
				?>
			</div>
		</div>
		<?php
	}

	private function renderCodePanel( string $tab, string $code, bool $active ): void {
		$hidden = $active ? '' : ' hidden';
		$empty  = $code === '';
		?>
		<div class="lolacore-pb-tab-panel<?php echo esc_attr($hidden); ?>" data-panel="<?php echo esc_attr($tab); ?>" role="tabpanel">
			<?php if ($empty) : ?>
				<p class="lolacore-pb-notice"><?php esc_html_e('No file content stored for this tab.', 'lolacore'); ?></p>
			<?php else : ?>
				<textarea class="lolacore-pb-code" readonly spellcheck="false"><?php echo esc_textarea($code); ?></textarea>
			<?php endif; ?>
		</div>
		<?php
	}
}
