<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$service_id = $args['p_id'] ?? get_the_ID();

$service_documents = gigneo_post_meta( $service_id, '_gigneo_pm_documents' );

if ( empty( $service_documents ) ) {
	return;
}
?>
<div class="gn-section gn-attachments-section">
	<div class="gn-head">
		<h3 class="gn-title"><?php echo esc_html__( 'Attachments', 'gigneo' ); ?></h3>
	</div>
	<div class="gn-body">
		<ul class="gn-attachment-list">
			<?php
			foreach ( $service_documents as $document_id ) {
				$file_path = get_attached_file( $document_id );
				$file_url  = wp_get_attachment_url( $document_id );

				if ( ! $file_path || ! file_exists( $file_path ) || ! $file_url ) {
					continue;
				}
				// File name
				$file_name = basename( $file_path );
				// File size in bytes
				$file_size_bytes = filesize( $file_path );
				// Convert to readable format (MB, KB)
				$file_size = size_format( $file_size_bytes, 2 );
				?>
				<li class="gn-attachment">
					<span class="gn-icon"><i class="hi hi-document" aria-hidden="true"></i></span>
					<div class="gn-info">
						<h6 class="gn-name"><?php echo esc_html( $file_name ); ?></h6>
						<p class="gn-size"><?php echo esc_html( $file_size ); ?></p>
					</div>
					<a href="<?php echo esc_url( $file_url ); ?>" download class="gn-btn gn-btn-download">
						<i class="hi hi-arrow-down-tray"></i>
					</a>
				</li>
			<?php } ?>
		</ul>
	</div>
</div>
