<?php
/**
 * The template for displaying attachment pages
 *
 * This template displays individual attachment pages (images, videos, documents).
 * It shows the attachment file with metadata and navigation to parent post.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#attachment
 *
 * @package ROI_Theme
 * @since 1.0.0
 */

get_header();
?>

	<main id="main-content" class="site-main" role="main">

		<div class="content-wrapper">

			<!-- Primary Content Area -->
			<div id="primary" class="content-area">

				<?php
				while ( have_posts() ) :
					the_post();
					?>

					<article id="post-<?php the_ID(); ?>" <?php post_class( 'attachment-page' ); ?>>

						<!-- Attachment Header -->
						<header class="entry-header">
							<h1 class="entry-title attachment-title">
								<?php the_title(); ?>
							</h1>

							<!-- Attachment Meta Information -->
							<div class="entry-meta attachment-meta">
								<span class="posted-on">
									<time class="entry-date published" datetime="<?php echo esc_attr( get_the_date( 'c' ) ); ?>">
										<?php
										printf(
											/* translators: %s: attachment date */
											esc_html__( 'Uploaded on %s', 'roi-theme' ),
											'<span class="date-text">' . esc_html( get_the_date() ) . '</span>'
										);
										?>
									</time>
								</span>

								<?php
								// Display file size
								$metadata = wp_get_attachment_metadata();
								if ( isset( $metadata['filesize'] ) ) :
									?>
									<span class="file-size">
										<?php
										printf(
											/* translators: %s: file size */
											esc_html__( 'Size: %s', 'roi-theme' ),
											esc_html( size_format( $metadata['filesize'] ) )
										);
										?>
									</span>
								<?php endif; ?>

								<?php
								// Display image dimensions if applicable
								if ( wp_attachment_is_image() && isset( $metadata['width'] ) && isset( $metadata['height'] ) ) :
									?>
									<span class="image-dimensions">
										<?php
										printf(
											/* translators: 1: width, 2: height */
											esc_html__( 'Dimensions: %1$s × %2$s pixels', 'roi-theme' ),
											esc_html( number_format_i18n( $metadata['width'] ) ),
											esc_html( number_format_i18n( $metadata['height'] ) )
										);
										?>
									</span>
								<?php endif; ?>
							</div><!-- .entry-meta -->
						</header><!-- .entry-header -->

						<!-- Attachment Content -->
						<div class="entry-content attachment-content">

							<?php
							// Display the attachment
							if ( wp_attachment_is_image() ) :
								// Image attachment
								?>
								<div class="attachment-image">
									<?php
									echo wp_get_attachment_image(
										get_the_ID(),
										'full',
										false,
										array(
											'alt' => get_the_title(),
											'class' => 'attachment-full-size',
										)
									);
									?>
								</div>

								<!-- Download link -->
								<div class="attachment-actions">
									<a href="<?php echo esc_url( wp_get_attachment_url() ); ?>"
									   class="btn btn-primary download-link"
									   download>
										<?php esc_html_e( 'Download Original', 'roi-theme' ); ?>
									</a>
								</div>

							<?php else : ?>
								<!-- Non-image attachment -->
								<div class="attachment-file">
									<p>
										<?php
										printf(
											/* translators: %s: attachment file name */
											esc_html__( 'File: %s', 'roi-theme' ),
											'<strong>' . esc_html( basename( get_attached_file( get_the_ID() ) ) ) . '</strong>'
										);
										?>
									</p>

									<!-- Download button -->
									<a href="<?php echo esc_url( wp_get_attachment_url() ); ?>"
									   class="btn btn-primary download-link"
									   download>
										<?php esc_html_e( 'Download File', 'roi-theme' ); ?>
									</a>
								</div>
							<?php endif; ?>

							<!-- Attachment Caption -->
							<?php
							if ( ! empty( wp_get_attachment_caption() ) ) :
								?>
								<div class="attachment-caption">
									<?php echo wp_kses_post( wpautop( wp_get_attachment_caption() ) ); ?>
								</div>
							<?php endif; ?>

							<!-- Attachment Description -->
							<?php
							the_content();

							// Display page links for paginated content
							wp_link_pages(
								array(
									'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'roi-theme' ),
									'after'  => '</div>',
								)
							);
							?>

						</div><!-- .entry-content -->

						<!-- Attachment Footer -->
						<footer class="entry-footer">
							<?php
							// Link to parent post if exists
							$parent_post = get_post_parent();
							if ( $parent_post ) :
								?>
								<div class="parent-post-link">
									<a href="<?php echo esc_url( get_permalink( $parent_post ) ); ?>">
										<?php
										printf(
											/* translators: %s: parent post title */
											esc_html__( 'View: %s', 'roi-theme' ),
											'<span class="parent-title">' . esc_html( get_the_title( $parent_post ) ) . '</span>'
										);
										?>
									</a>
								</div>
							<?php endif; ?>

							<?php
							// Edit post link for logged-in users with permission
							edit_post_link(
								sprintf(
									wp_kses(
										/* translators: %s: Attachment title. Only visible to screen readers. */
										__( 'Edit<span class="screen-reader-text"> "%s"</span>', 'roi-theme' ),
										array(
											'span' => array(
												'class' => array(),
											),
										)
									),
									get_the_title()
								),
								'<span class="edit-link">',
								'</span>'
							);
							?>
						</footer><!-- .entry-footer -->

					</article><!-- #post-<?php the_ID(); ?> -->

					<?php
					// Display comments section if enabled
					if ( comments_open() || get_comments_number() ) :
						comments_template();
					endif;

				endwhile; // End of the loop.
				?>

			</div><!-- #primary -->

			<?php
			/**
			 * Sidebar
			 * Display the sidebar if it's active.
			 */
			if ( is_active_sidebar( 'sidebar-1' ) ) :
				get_sidebar();
			endif;
			?>

		</div><!-- .content-wrapper -->

	</main><!-- #main-content -->

<?php
get_footer();
