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

$job_id           = $args['p_id'] ?? get_the_ID();
$attachments      = gigneo_post_meta( $job_id, '_gigneo_pm_attachments' );
$attachment_limit = gigneo_option( '_gigneo_ts_job_attachment_limit', 5 );

if ( empty( $attachments ) || ! is_array( $attachments ) ) {
	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
			$attachment_placeholder_url = GIGNEO_DIR_URL . 'assets/img/icon/document-svg.png';
			$attachments                = array_slice( $attachments, 0, $attachment_limit );

			foreach ( $attachments as $attachment_id ) {
				if ( empty( $attachment_id ) ) {
					continue;
				}

				$file_path = get_attached_file( $attachment_id );
				if ( ! $file_path || ! file_exists( $file_path ) ) {
					continue;
				}

				$file_url        = wp_get_attachment_url( $attachment_id );
				$file_name       = basename( $file_path );
				$file_size_bytes = filesize( $file_path );
				$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>