<?php
/**
 * Admin: metaboxes, media UI, global defaults page.
 *
 * @package Image_Gallery_Custom_Post_Type
 */

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

/**
 * Gallery admin UI.
 */
class NZP_IGL_Admin {

	private const PAGE_SLUG = 'nzp-igl-defaults';
	private const NONCE_META = 'nzp_igl_meta_nonce';
	private const NONCE_GLOBAL = 'nzp_igl_global_nonce';

	public function __construct() {
		add_action( 'add_meta_boxes', array( $this, 'register_meta_boxes' ) );
		add_action( 'save_post_gallerylist', array( $this, 'save_gallery_meta' ), 10, 2 );
		add_action( 'admin_menu', array( $this, 'register_submenu' ) );
		add_action( 'admin_init', array( $this, 'handle_global_save' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) );
	}

	/**
	 * @return void
	 */
	public function register_meta_boxes() {
		add_meta_box(
			'nzp_igl_gallery',
			__( 'Gallery & appearance', 'image-gallery-custom-post-type' ),
			array( $this, 'render_gallery_metabox' ),
			'gallerylist',
			'normal',
			'high'
		);
		add_meta_box(
			'nzp_igl_shortcode',
			__( 'Use this gallery', 'image-gallery-custom-post-type' ),
			array( $this, 'render_shortcode_metabox' ),
			'gallerylist',
			'side',
			'high'
		);
		add_meta_box(
			'nzp_igl_support',
			__( 'Image Gallery Pro', 'image-gallery-custom-post-type' ),
			array( $this, 'render_support_metabox' ),
			'gallerylist',
			'side',
			'low'
		);
	}

	/**
	 * Sidebar: activate Pro add-on (gallery edit screen).
	 *
	 * @param WP_Post $_post Post (unused).
	 * @return void
	 */
	public function render_support_metabox( $_post ) {
		unset( $_post );
		$this->render_support_and_pro_box();
	}

	/**
	 * Activate Pro add-on (gallery sidebar + Galleries → Settings) — upload ZIP or enable under Plugins.
	 *
	 * @return void
	 */
	public function render_support_and_pro_box() {
		$is_pro      = NZP_IGL_Limits::is_pro();
		$upload_url  = admin_url( 'plugin-install.php?tab=upload' );
		$plugins_url = admin_url( 'plugins.php' );
		$donate_url  = trim( (string) NZP_IGL_Limits::donate_url() );
		?>
		<div class="nzp-igl-admin-card nzp-igl-admin-card--support">
			<p class="nzp-igl-admin__hint nzp-igl-support-card__intro">
				<?php esc_html_e( 'Install and activate the Image Gallery Pro add-on plugin to unlock Mosaic, Showcase, Carousel, and up to 100 images per gallery.', 'image-gallery-custom-post-type' ); ?>
			</p>
			<?php if ( ! $is_pro ) : ?>
				<div class="nzp-igl-support-card__pro-block">
					<p class="nzp-igl-support-card__actions">
						<a class="button button-primary button-hero" href="<?php echo esc_url( $upload_url ); ?>"><?php esc_html_e( 'Upload Pro add-on', 'image-gallery-custom-post-type' ); ?></a>
					</p>
					<p class="description nzp-igl-support-card__secondary">
						<a href="<?php echo esc_url( $plugins_url ); ?>"><?php esc_html_e( 'Plugins', 'image-gallery-custom-post-type' ); ?></a>
						<?php esc_html_e( '— activate the add-on here if it is already installed.', 'image-gallery-custom-post-type' ); ?>
					</p>
				</div>
			<?php else : ?>
				<p class="nzp-igl-support-card__pro-active description"><?php esc_html_e( 'Image Gallery Pro is active on this site. Thank you!', 'image-gallery-custom-post-type' ); ?></p>
			<?php endif; ?>
			<?php if ( $donate_url !== '' ) : ?>
				<div class="nzp-igl-support-card__donate">
					<p class="description nzp-igl-support-card__donate-lead"><?php esc_html_e( 'This plugin is free to use. If it helps your site, you can leave a tip.', 'image-gallery-custom-post-type' ); ?></p>
					<p class="nzp-igl-support-card__actions nzp-igl-support-card__actions--donate">
						<a class="button" href="<?php echo esc_url( $donate_url ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Donate', 'image-gallery-custom-post-type' ); ?></a>
					</p>
				</div>
			<?php endif; ?>
		</div>
		<?php
	}

	/**
	 * Images + display options in one metabox / one card.
	 *
	 * @param WP_Post $post Post.
	 * @return void
	 */
	public function render_gallery_metabox( WP_Post $post ) {
		wp_nonce_field( 'nzp_igl_save_meta', self::NONCE_META );
		$ids     = NZP_IGL_Storage::get_image_ids( $post->ID );
		$opts    = NZP_IGL_Storage::get_merged_options( $post->ID );
		$raw     = get_post_meta( $post->ID, NZP_IGL_Storage::META_OPTS, true );
		$is_post = is_array( $raw ) && ! empty( $raw );
		?>
		<div class="nzp-igl-admin-card nzp-igl-admin-card--unified">
			<div class="nzp-igl-admin-card__section">
				<h3 class="nzp-igl-admin-card__title"><?php esc_html_e( 'Images', 'image-gallery-custom-post-type' ); ?></h3>
				<p class="nzp-igl-admin__hint"><?php esc_html_e( 'Add images from your Media Library. Drag thumbnails to reorder.', 'image-gallery-custom-post-type' ); ?></p>
				<?php if ( ! NZP_IGL_Limits::is_pro() ) : ?>
					<p class="description"><?php echo esc_html( sprintf(
						/* translators: %d: max images in free tier */
						__( 'Free plan: up to %d images per gallery. For Mosaic, Showcase, Carousel, and more images, use Image Gallery Pro — see the blue box below for how to upgrade.', 'image-gallery-custom-post-type' ),
						NZP_IGL_Limits::max_gallery_images()
					) ); ?></p>
				<?php else : ?>
					<p class="description"><?php echo esc_html( sprintf(
						/* translators: %d: max images on Pro tier */
						__( 'Pro: you can add up to %d images per gallery. Choose Mosaic, Showcase, or Carousel under Appearance below.', 'image-gallery-custom-post-type' ),
						NZP_IGL_Limits::max_gallery_images()
					) ); ?></p>
				<?php endif; ?>
				<input type="hidden" name="nzp_igl_image_ids" id="nzp_igl_image_ids" value="<?php echo esc_attr( implode( ',', $ids ) ); ?>" />
				<p class="nzp-igl-admin-card__actions">
					<button type="button" class="button button-primary" id="nzp_igl_select_images"><?php esc_html_e( 'Select / upload images', 'image-gallery-custom-post-type' ); ?></button>
					<button type="button" class="button" id="nzp_igl_clear_images"><?php esc_html_e( 'Clear all', 'image-gallery-custom-post-type' ); ?></button>
				</p>
				<ul id="nzp_igl_sortable" class="nzp-igl-admin__thumbs" aria-label="<?php esc_attr_e( 'Gallery image order', 'image-gallery-custom-post-type' ); ?>">
					<?php
					foreach ( $ids as $aid ) {
						if ( ! wp_attachment_is_image( $aid ) ) {
							continue;
						}
						$url = wp_get_attachment_image_url( $aid, 'thumbnail' );
						if ( ! $url ) {
							continue;
						}
						?>
						<li class="nzp-igl-admin__thumb" data-id="<?php echo esc_attr( (string) $aid ); ?>">
							<span class="nzp-igl-admin__thumb-inner">
								<img src="<?php echo esc_url( $url ); ?>" alt="" width="80" height="80" loading="lazy" />
								<button type="button" class="nzp-igl-admin__remove" aria-label="<?php esc_attr_e( 'Remove image', 'image-gallery-custom-post-type' ); ?>">&times;</button>
							</span>
						</li>
						<?php
					}
					?>
				</ul>
			</div>
			<div class="nzp-igl-admin-card__divider" role="presentation"></div>
			<div class="nzp-igl-admin-card__section">
				<h3 class="nzp-igl-admin-card__title"><?php esc_html_e( 'Appearance', 'image-gallery-custom-post-type' ); ?></h3>
				<p class="nzp-igl-admin__hint"><?php esc_html_e( 'Layout and colors apply to this gallery on the front end. Leave colors empty to inherit from Galleries → Settings.', 'image-gallery-custom-post-type' ); ?></p>
				<?php $this->render_pro_notice(); ?>
				<table class="form-table nzp-igl-admin-form" role="presentation">
					<tr>
						<th scope="row"><label for="nzp_igl_layout"><?php esc_html_e( 'Layout', 'image-gallery-custom-post-type' ); ?></label></th>
						<td>
							<?php $this->render_layout_select( 'nzp_igl_opts[layout]', (string) $opts['layout'], 'nzp_igl_layout' ); ?>
						</td>
					</tr>
					<tr>
						<th scope="row"><?php esc_html_e( 'Grid columns', 'image-gallery-custom-post-type' ); ?></th>
						<td>
							<select name="nzp_igl_opts[columns]">
								<?php for ( $c = 1; $c <= 4; $c++ ) : ?>
									<option value="<?php echo (int) $c; ?>" <?php selected( (int) $opts['columns'], $c ); ?>><?php echo (int) $c; ?></option>
								<?php endfor; ?>
							</select>
						</td>
					</tr>
					<tr>
						<th scope="row"><?php esc_html_e( 'Card corner radius (px)', 'image-gallery-custom-post-type' ); ?></th>
						<td><input type="number" name="nzp_igl_opts[card_radius]" min="0" max="32" step="1" value="<?php echo (int) $opts['card_radius']; ?>" class="small-text" /></td>
					</tr>
					<tr>
						<th scope="row"><?php esc_html_e( 'Accent color', 'image-gallery-custom-post-type' ); ?></th>
						<td><input type="text" name="nzp_igl_opts[accent_color]" value="<?php echo esc_attr( (string) $opts['accent_color'] ); ?>" class="nzp-igl-color-field" data-default-color="" /></td>
					</tr>
					<tr>
						<th scope="row"><?php esc_html_e( 'Title color', 'image-gallery-custom-post-type' ); ?></th>
						<td><input type="text" name="nzp_igl_opts[title_color]" value="<?php echo esc_attr( (string) $opts['title_color'] ); ?>" class="nzp-igl-color-field" data-default-color="" /></td>
					</tr>
					<tr>
						<th scope="row"><?php esc_html_e( 'Caption / text color', 'image-gallery-custom-post-type' ); ?></th>
						<td><input type="text" name="nzp_igl_opts[excerpt_color]" value="<?php echo esc_attr( (string) $opts['excerpt_color'] ); ?>" class="nzp-igl-color-field" data-default-color="" /></td>
					</tr>
					<tr>
						<th scope="row"><?php esc_html_e( 'Visibility', 'image-gallery-custom-post-type' ); ?></th>
						<td>
							<label><input type="checkbox" name="nzp_igl_opts[show_titles]" value="1" <?php checked( ! empty( $opts['show_titles'] ) ); ?> /> <?php esc_html_e( 'Show titles', 'image-gallery-custom-post-type' ); ?></label><br />
							<label><input type="checkbox" name="nzp_igl_opts[show_captions]" value="1" <?php checked( ! empty( $opts['show_captions'] ) ); ?> /> <?php esc_html_e( 'Show captions / descriptions', 'image-gallery-custom-post-type' ); ?></label><br />
							<label><input type="checkbox" name="nzp_igl_opts[link_to_media]" value="1" <?php checked( ! empty( $opts['link_to_media'] ) ); ?> /> <?php esc_html_e( 'Link titles to the media / attachment page (thumbnails open the image viewer)', 'image-gallery-custom-post-type' ); ?></label>
						</td>
					</tr>
				</table>
				<?php if ( $is_post ) : ?>
					<p class="nzp-igl-admin-card__footer"><label><input type="checkbox" name="nzp_igl_reset_design" value="1" /> <?php esc_html_e( 'Reset this gallery’s design to global defaults on save', 'image-gallery-custom-post-type' ); ?></label></p>
				<?php endif; ?>
			</div>
		</div>
		<?php
	}

	/**
	 * @param WP_Post $post Post.
	 * @return void
	 */
	public function render_shortcode_metabox( WP_Post $post ) {
		if ( $post->post_status === 'auto-draft' ) {
			echo '<div class="nzp-igl-admin-card nzp-igl-admin-card--compact"><p class="nzp-igl-admin__hint">' . esc_html__( 'Publish or save the gallery to get a shortcode.', 'image-gallery-custom-post-type' ) . '</p></div>';
			return;
		}
		$code = '[show_gallery_img id="' . (int) $post->ID . '"]';
		?>
		<div class="nzp-igl-admin-card nzp-igl-admin-card--compact">
			<p class="nzp-igl-admin__hint"><?php esc_html_e( 'Paste into a page, post, or widget:', 'image-gallery-custom-post-type' ); ?></p>
			<p><code class="nzp-igl-admin__code" id="nzp-igl-shortcode-text"><?php echo esc_html( $code ); ?></code></p>
			<p class="nzp-igl-admin-card__actions nzp-igl-admin-card__actions--tight"><button type="button" class="button button-primary button-small" id="nzp-igl-copy-shortcode"><?php esc_html_e( 'Copy shortcode', 'image-gallery-custom-post-type' ); ?></button></p>
		</div>
		<?php
	}

	/**
	 * @param int     $post_id Post ID.
	 * @param WP_Post $post    Post.
	 * @return void
	 */
	public function save_gallery_meta( $post_id, $post ) {
		if ( ! isset( $_POST[ self::NONCE_META ] ) ) {
			return;
		}
		if ( ! check_admin_referer( 'nzp_igl_save_meta', self::NONCE_META ) ) {
			return;
		}
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
			return;
		}
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
			return;
		}
		if ( ! $post instanceof WP_Post || $post->post_type !== 'gallerylist' ) {
			return;
		}

		$ids = array();
		if ( isset( $_POST['nzp_igl_image_ids'] ) ) {
			$raw = sanitize_text_field( wp_unslash( (string) $_POST['nzp_igl_image_ids'] ) );
			$parts = $raw !== '' ? explode( ',', $raw ) : array();
			$ids   = NZP_IGL_Storage::sanitize_image_ids( $parts );
		}
		update_post_meta( $post_id, NZP_IGL_Storage::META_IDS, $ids );

		if ( ! empty( $_POST['nzp_igl_reset_design'] ) ) {
			delete_post_meta( $post_id, NZP_IGL_Storage::META_OPTS );
		} elseif ( isset( $_POST['nzp_igl_opts'] ) && is_array( $_POST['nzp_igl_opts'] ) ) {
			$opts_in = map_deep( wp_unslash( $_POST['nzp_igl_opts'] ), 'sanitize_text_field' );
			$opts    = NZP_IGL_Storage::sanitize_options(
				array(
					'layout'        => isset( $opts_in['layout'] ) ? $opts_in['layout'] : '',
					'columns'       => isset( $opts_in['columns'] ) ? $opts_in['columns'] : '',
					'card_radius'   => isset( $opts_in['card_radius'] ) ? $opts_in['card_radius'] : '',
					'accent_color'  => isset( $opts_in['accent_color'] ) ? $opts_in['accent_color'] : '',
					'title_color'   => isset( $opts_in['title_color'] ) ? $opts_in['title_color'] : '',
					'excerpt_color' => isset( $opts_in['excerpt_color'] ) ? $opts_in['excerpt_color'] : '',
					'show_titles'   => ! empty( $_POST['nzp_igl_opts']['show_titles'] ) ? 1 : 0,
					'show_captions' => ! empty( $_POST['nzp_igl_opts']['show_captions'] ) ? 1 : 0,
					'link_to_media' => ! empty( $_POST['nzp_igl_opts']['link_to_media'] ) ? 1 : 0,
				)
			);
			update_post_meta( $post_id, NZP_IGL_Storage::META_OPTS, $opts );
		}
	}

	/**
	 * @return void
	 */
	public function register_submenu() {
		add_submenu_page(
			'edit.php?post_type=gallerylist',
			__( 'Gallery settings', 'image-gallery-custom-post-type' ),
			__( 'Settings', 'image-gallery-custom-post-type' ),
			'manage_options',
			self::PAGE_SLUG,
			array( $this, 'render_settings_page' )
		);
	}

	/**
	 * @return void
	 */
	public function handle_global_save() {
		if ( ! isset( $_POST['nzp_igl_global_save'], $_POST[ self::NONCE_GLOBAL ] ) ) {
			return;
		}
		if ( ! check_admin_referer( 'nzp_igl_save_global', self::NONCE_GLOBAL ) ) {
			return;
		}
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}
		$slug = isset( $_POST['nzp_igl_page'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['nzp_igl_page'] ) ) : '';
		if ( $slug !== self::PAGE_SLUG ) {
			return;
		}
		$opts_in = isset( $_POST['nzp_igl_global'] ) && is_array( $_POST['nzp_igl_global'] )
			? map_deep( wp_unslash( $_POST['nzp_igl_global'] ), 'sanitize_text_field' )
			: array();
		$opts    = NZP_IGL_Storage::sanitize_options(
			array(
				'layout'        => isset( $opts_in['layout'] ) ? $opts_in['layout'] : '',
				'columns'       => isset( $opts_in['columns'] ) ? $opts_in['columns'] : '',
				'card_radius'   => isset( $opts_in['card_radius'] ) ? $opts_in['card_radius'] : '',
				'accent_color'  => isset( $opts_in['accent_color'] ) ? $opts_in['accent_color'] : '',
				'title_color'   => isset( $opts_in['title_color'] ) ? $opts_in['title_color'] : '',
				'excerpt_color' => isset( $opts_in['excerpt_color'] ) ? $opts_in['excerpt_color'] : '',
				'show_titles'   => ! empty( $_POST['nzp_igl_global']['show_titles'] ) ? 1 : 0,
				'show_captions' => ! empty( $_POST['nzp_igl_global']['show_captions'] ) ? 1 : 0,
				'link_to_media' => ! empty( $_POST['nzp_igl_global']['link_to_media'] ) ? 1 : 0,
			)
		);
		update_option( NZP_IGL_Storage::OPTION_GLOBAL, $opts, false );
		$redirect = add_query_arg(
			array(
				'post_type'         => 'gallerylist',
				'page'              => self::PAGE_SLUG,
				'settings-updated'  => '1',
			),
			admin_url( 'edit.php' )
		);
		wp_safe_redirect( $redirect );
		exit;
	}

	/**
	 * @return void
	 */
	public function render_settings_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have permission to access this page.', 'image-gallery-custom-post-type' ) );
		}
		$opts = NZP_IGL_Storage::get_global_options();
		if ( isset( $_GET['settings-updated'] ) && '1' === sanitize_text_field( wp_unslash( (string) $_GET['settings-updated'] ) ) ) {
			echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Defaults saved.', 'image-gallery-custom-post-type' ) . '</p></div>';
		}
		?>
		<div class="wrap nzp-igl-admin-wrap nzp-igl-admin-wrap--settings">
			<form method="post" action="" class="nzp-igl-admin-settings-form">
				<?php wp_nonce_field( 'nzp_igl_save_global', self::NONCE_GLOBAL ); ?>
				<input type="hidden" name="nzp_igl_global_save" value="1" />
				<input type="hidden" name="nzp_igl_page" value="<?php echo esc_attr( self::PAGE_SLUG ); ?>" />
				<div class="nzp-igl-admin-card nzp-igl-admin-card--unified nzp-igl-admin-card--settings">
					<div class="nzp-igl-admin-card__section nzp-igl-admin-card__section--settings-hero">
						<h1 class="nzp-igl-settings-page-title"><?php esc_html_e( 'Gallery defaults', 'image-gallery-custom-post-type' ); ?></h1>
						<p class="nzp-igl-admin__hint nzp-igl-admin__hint--settings-lead"><?php esc_html_e( 'New galleries start with these values. Each gallery can override them when you edit that gallery.', 'image-gallery-custom-post-type' ); ?></p>
					</div>
					<div class="nzp-igl-admin-card__divider" role="presentation"></div>
					<div class="nzp-igl-admin-card__section">
						<h2 class="nzp-igl-admin-card__title nzp-igl-admin-card__title--form"><?php esc_html_e( 'Default appearance', 'image-gallery-custom-post-type' ); ?></h2>
						<p class="nzp-igl-admin__hint"><?php esc_html_e( 'These options apply until a gallery sets its own layout and colors.', 'image-gallery-custom-post-type' ); ?></p>
						<?php $this->render_pro_notice(); ?>
						<table class="form-table nzp-igl-admin-form" role="presentation">
							<tr>
								<th scope="row"><label for="g_layout"><?php esc_html_e( 'Default layout', 'image-gallery-custom-post-type' ); ?></label></th>
								<td>
									<?php $this->render_layout_select( 'nzp_igl_global[layout]', (string) $opts['layout'], 'g_layout' ); ?>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e( 'Default grid columns', 'image-gallery-custom-post-type' ); ?></th>
								<td>
									<select name="nzp_igl_global[columns]">
										<?php for ( $c = 1; $c <= 4; $c++ ) : ?>
											<option value="<?php echo (int) $c; ?>" <?php selected( (int) $opts['columns'], $c ); ?>><?php echo (int) $c; ?></option>
										<?php endfor; ?>
									</select>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e( 'Default card radius (px)', 'image-gallery-custom-post-type' ); ?></th>
								<td><input type="number" name="nzp_igl_global[card_radius]" min="0" max="32" value="<?php echo (int) $opts['card_radius']; ?>" class="small-text" /></td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e( 'Accent color', 'image-gallery-custom-post-type' ); ?></th>
								<td><input type="text" name="nzp_igl_global[accent_color]" value="<?php echo esc_attr( (string) $opts['accent_color'] ); ?>" class="nzp-igl-color-field" data-default-color="" /></td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e( 'Title color', 'image-gallery-custom-post-type' ); ?></th>
								<td><input type="text" name="nzp_igl_global[title_color]" value="<?php echo esc_attr( (string) $opts['title_color'] ); ?>" class="nzp-igl-color-field" data-default-color="" /></td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e( 'Caption color', 'image-gallery-custom-post-type' ); ?></th>
								<td><input type="text" name="nzp_igl_global[excerpt_color]" value="<?php echo esc_attr( (string) $opts['excerpt_color'] ); ?>" class="nzp-igl-color-field" data-default-color="" /></td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e( 'Visibility defaults', 'image-gallery-custom-post-type' ); ?></th>
								<td>
									<label><input type="checkbox" name="nzp_igl_global[show_titles]" value="1" <?php checked( ! empty( $opts['show_titles'] ) ); ?> /> <?php esc_html_e( 'Show titles', 'image-gallery-custom-post-type' ); ?></label><br />
									<label><input type="checkbox" name="nzp_igl_global[show_captions]" value="1" <?php checked( ! empty( $opts['show_captions'] ) ); ?> /> <?php esc_html_e( 'Show captions', 'image-gallery-custom-post-type' ); ?></label><br />
									<label><input type="checkbox" name="nzp_igl_global[link_to_media]" value="1" <?php checked( ! empty( $opts['link_to_media'] ) ); ?> /> <?php esc_html_e( 'Link titles to media (thumbnails use the viewer)', 'image-gallery-custom-post-type' ); ?></label>
								</td>
							</tr>
						</table>
					</div>
					<?php submit_button( __( 'Save defaults', 'image-gallery-custom-post-type' ), 'primary', 'submit', true, array( 'class' => 'button-large nzp-igl-admin-save-btn' ) ); ?>
				</div>
			</form>
			<section class="nzp-igl-admin-card nzp-igl-admin-card--shortcode-panel" aria-labelledby="nzp-igl-shortcode-heading">
				<h2 id="nzp-igl-shortcode-heading" class="nzp-igl-admin-card__title"><?php esc_html_e( 'Shortcode', 'image-gallery-custom-post-type' ); ?></h2>
				<p class="nzp-igl-admin__hint"><?php esc_html_e( 'Embed one gallery by ID (copy the ID from the gallery edit screen):', 'image-gallery-custom-post-type' ); ?></p>
				<p><code class="nzp-igl-admin__code nzp-igl-admin__code--lg">[show_gallery_img id="123"]</code></p>
				<p class="description"><?php esc_html_e( 'Optional attributes: layout, load_styles, limit. Omit id to list all published gallery posts (legacy).', 'image-gallery-custom-post-type' ); ?></p>
			</section>
			<div class="nzp-igl-admin-settings-support-wrap">
				<h2 class="nzp-igl-support-settings-title"><?php esc_html_e( 'Image Gallery Pro', 'image-gallery-custom-post-type' ); ?></h2>
				<?php $this->render_support_and_pro_box(); ?>
			</div>
		</div>
		<?php
	}

	/**
	 * @param string $hook Hook suffix.
	 * @return void
	 */
	public function enqueue_admin_assets( $hook ) {
		$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
		$is_gallery_edit = $screen && $screen->post_type === 'gallerylist' && ( $screen->base === 'post' || $screen->base === 'post-new' );
		$is_settings     = $hook === 'gallerylist_page_' . self::PAGE_SLUG;

		if ( ! $is_gallery_edit && ! $is_settings ) {
			return;
		}

		wp_enqueue_media();
		wp_enqueue_style( 'wp-color-picker' );
		wp_enqueue_script( 'wp-color-picker' );
		wp_enqueue_script( 'jquery-ui-sortable' );

		wp_enqueue_style(
			'nzp-igl-admin',
			NZP_IMAGE_GALLERY_URL . 'css/admin-gallery.css',
			array(),
			NZP_IMAGE_GALLERY_VERSION
		);
		wp_enqueue_script(
			'nzp-igl-admin',
			NZP_IMAGE_GALLERY_URL . 'js/admin-gallery.js',
			array( 'jquery', 'jquery-ui-sortable', 'wp-color-picker' ),
			NZP_IMAGE_GALLERY_VERSION,
			true
		);
		wp_localize_script(
			'nzp-igl-admin',
			'nzpIglAdmin',
			array(
				'frameTitle'   => __( 'Gallery images', 'image-gallery-custom-post-type' ),
				'frameButton'  => __( 'Add to gallery', 'image-gallery-custom-post-type' ),
				'copied'       => __( 'Copied!', 'image-gallery-custom-post-type' ),
				'removeLabel'  => __( 'Remove image', 'image-gallery-custom-post-type' ),
				'maxImages'    => NZP_IGL_Limits::max_gallery_images(),
				'maxImagesMsg' => __( 'You have reached the maximum number of images for your plan.', 'image-gallery-custom-post-type' ),
			)
		);
	}

	/**
	 * Layout dropdown: free layouts + Pro layouts (disabled until licensed).
	 *
	 * @param string $name     Select name attribute.
	 * @param string $selected Saved layout slug.
	 * @param string $id       Element id.
	 * @return void
	 */
	private function render_layout_select( $name, $selected, $id ) {
		$selected = NZP_IGL_Limits::normalize_layout( $selected );
		$is_pro   = NZP_IGL_Limits::is_pro();
		$free     = array(
			'grid'    => __( 'Grid', 'image-gallery-custom-post-type' ),
			'listing' => __( 'Listing', 'image-gallery-custom-post-type' ),
		);
		$pro_l    = array(
			'mosaic'   => __( 'Mosaic', 'image-gallery-custom-post-type' ),
			'showcase' => __( 'Showcase', 'image-gallery-custom-post-type' ),
			'carousel' => __( 'Carousel', 'image-gallery-custom-post-type' ),
		);
		?>
		<select name="<?php echo esc_attr( $name ); ?>" id="<?php echo esc_attr( $id ); ?>">
			<?php if ( $is_pro ) : ?>
				<optgroup label="<?php esc_attr_e( 'Included', 'image-gallery-custom-post-type' ); ?>">
					<?php foreach ( $free as $val => $lab ) : ?>
						<option value="<?php echo esc_attr( $val ); ?>" <?php selected( $selected, $val ); ?>><?php echo esc_html( $lab ); ?></option>
					<?php endforeach; ?>
				</optgroup>
				<optgroup label="<?php esc_attr_e( 'Image Gallery Pro', 'image-gallery-custom-post-type' ); ?>">
					<?php foreach ( $pro_l as $val => $lab ) : ?>
						<option value="<?php echo esc_attr( $val ); ?>" <?php selected( $selected, $val ); ?>><?php echo esc_html( $lab ); ?></option>
					<?php endforeach; ?>
				</optgroup>
			<?php else : ?>
				<?php foreach ( $free as $val => $lab ) : ?>
					<option value="<?php echo esc_attr( $val ); ?>" <?php selected( $selected, $val ); ?>><?php echo esc_html( $lab ); ?></option>
				<?php endforeach; ?>
			<?php endif; ?>
		</select>
		<?php
	}

	/**
	 * Explains Free vs Pro and how to upgrade (edit gallery + global settings).
	 *
	 * @return void
	 */
	private function render_pro_notice() {
		if ( NZP_IGL_Limits::is_pro() ) {
			$this->render_pro_active_notice();
			return;
		}
		$upload_url  = admin_url( 'plugin-install.php?tab=upload' );
		$plugins_url = admin_url( 'plugins.php' );
		$donate_url  = trim( (string) NZP_IGL_Limits::donate_url() );
		$max_free    = (int) NZP_IGL_Limits::max_gallery_images();
		$pro_cap   = 100;
		?>
		<div class="notice notice-info inline nzp-igl-pro-notice" role="region" aria-labelledby="nzp-igl-pro-notice-heading">
			<h3 id="nzp-igl-pro-notice-heading" class="nzp-igl-pro-notice__title"><?php esc_html_e( 'You are using Image Gallery (free)', 'image-gallery-custom-post-type' ); ?></h3>

			<div class="nzp-igl-pro-notice__cols">
				<div class="nzp-igl-pro-notice__col">
					<p class="nzp-igl-pro-notice__label"><?php esc_html_e( 'Included now', 'image-gallery-custom-post-type' ); ?></p>
					<ul class="nzp-igl-pro-notice__list">
						<li><?php esc_html_e( 'Layouts: Grid and Listing', 'image-gallery-custom-post-type' ); ?></li>
						<li>
							<?php
							echo esc_html(
								sprintf(
									/* translators: %d: max images on free tier */
									_n(
										'Up to %d image per gallery',
										'Up to %d images per gallery',
										$max_free,
										'image-gallery-custom-post-type'
									),
									$max_free
								)
							);
							?>
						</li>
					</ul>
				</div>
				<div class="nzp-igl-pro-notice__col nzp-igl-pro-notice__col--pro">
					<p class="nzp-igl-pro-notice__label"><?php esc_html_e( 'Image Gallery Pro adds', 'image-gallery-custom-post-type' ); ?></p>
					<ul class="nzp-igl-pro-notice__list">
						<li><?php esc_html_e( 'Layouts: Mosaic, Showcase, and Carousel', 'image-gallery-custom-post-type' ); ?></li>
						<li>
							<?php
							echo esc_html(
								sprintf(
									/* translators: %d: max images on Pro tier */
									__( 'Up to %d images per gallery', 'image-gallery-custom-post-type' ),
									$pro_cap
								)
							);
							?>
						</li>
					</ul>
				</div>
			</div>

			<div class="nzp-igl-pro-notice__upgrade">
				<p class="nzp-igl-pro-notice__upgrade-title"><?php esc_html_e( 'How to enable Pro', 'image-gallery-custom-post-type' ); ?></p>
				<ol class="nzp-igl-pro-notice__steps">
					<li><?php esc_html_e( 'Get the Image Gallery Pro add-on ZIP (from your purchase or developer).', 'image-gallery-custom-post-type' ); ?></li>
					<li><?php esc_html_e( 'In WordPress: Plugins → Add New → Upload Plugin, install the ZIP, then activate “Image Gallery Pro (Add-on)”.', 'image-gallery-custom-post-type' ); ?></li>
					<li><?php esc_html_e( 'Keep this free plugin active too. Refresh this screen — Pro layouts unlock automatically.', 'image-gallery-custom-post-type' ); ?></li>
				</ol>
				<p class="nzp-igl-pro-notice__cta">
					<a class="button button-primary" href="<?php echo esc_url( $upload_url ); ?>"><?php esc_html_e( 'Upload Pro add-on', 'image-gallery-custom-post-type' ); ?></a>
					<a class="button" href="<?php echo esc_url( $plugins_url ); ?>"><?php esc_html_e( 'Plugins', 'image-gallery-custom-post-type' ); ?></a>
					<?php if ( $donate_url !== '' ) : ?>
						<a class="button" href="<?php echo esc_url( $donate_url ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Donate', 'image-gallery-custom-post-type' ); ?></a>
					<?php endif; ?>
				</p>
			</div>
		</div>
		<?php
	}

	/**
	 * After purchase: how to use Mosaic, Showcase, Carousel, and higher limits.
	 *
	 * @return void
	 */
	private function render_pro_active_notice() {
		$max_pro     = (int) NZP_IGL_Limits::max_gallery_images();
		$donate_url  = trim( (string) NZP_IGL_Limits::donate_url() );
		?>
		<div class="notice notice-success inline nzp-igl-pro-notice nzp-igl-pro-notice--active" role="region" aria-labelledby="nzp-igl-pro-active-heading">
			<h3 id="nzp-igl-pro-active-heading" class="nzp-igl-pro-notice__title"><?php esc_html_e( 'Image Gallery Pro is active', 'image-gallery-custom-post-type' ); ?></h3>
			<p class="nzp-igl-pro-notice__lead"><?php esc_html_e( 'Use the steps below on each gallery (or set defaults under Galleries → Settings).', 'image-gallery-custom-post-type' ); ?></p>
			<ol class="nzp-igl-pro-notice__steps nzp-igl-pro-notice__steps--pro-use">
				<li>
					<?php esc_html_e( 'Add or reorder images above (up to the Pro limit).', 'image-gallery-custom-post-type' ); ?>
					<?php
					echo ' ';
					echo esc_html(
						sprintf(
							/* translators: %d: max images on Pro tier */
							__( 'Current limit: up to %d images per gallery.', 'image-gallery-custom-post-type' ),
							$max_pro
						)
					);
					?>
				</li>
				<li><?php esc_html_e( 'Under Appearance, open Layout and pick Mosaic, Showcase, or Carousel (or keep Grid / Listing). Save the gallery.', 'image-gallery-custom-post-type' ); ?></li>
				<li>
					<?php esc_html_e( 'Copy the shortcode from the sidebar and paste it into a page or post. Example:', 'image-gallery-custom-post-type' ); ?>
					<code class="nzp-igl-pro-notice__code"><?php echo esc_html( '[show_gallery_img id="123" layout="mosaic"]' ); ?></code>
					<span class="description"><?php esc_html_e( 'Change the id to your gallery ID; layout is optional if you already saved one here.', 'image-gallery-custom-post-type' ); ?></span>
				</li>
				<li><?php esc_html_e( 'Optional: go to Galleries → Settings to set default layout and colors for new galleries.', 'image-gallery-custom-post-type' ); ?></li>
			</ol>
			<?php if ( $donate_url !== '' ) : ?>
				<p class="nzp-igl-pro-notice__donate">
					<a class="button" href="<?php echo esc_url( $donate_url ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Donate', 'image-gallery-custom-post-type' ); ?></a>
					<span class="description"><?php esc_html_e( 'Support ongoing development.', 'image-gallery-custom-post-type' ); ?></span>
				</p>
			<?php endif; ?>
			<p class="nzp-igl-pro-notice__keep description">
				<?php esc_html_e( 'Keep the Image Gallery Pro add-on activated under Plugins. If Pro layouts disappear, re-activate the Pro plugin or contact support from your purchase email.', 'image-gallery-custom-post-type' ); ?>
			</p>
		</div>
		<?php
	}
}
