<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
$order_id = $args['p_id'] ?? '';

if ( ! $order_id ) {
	return;
}
$freelancer_profile_id = $args['freelancer_profile_id'] ?? '';
$profile_id            = $args['profile_id'] ?? '';
$date_format           = $args['date_format'] ?? '';
$time_format           = $args['time_format'] ?? '';

$profile_id            = intval( $profile_id );
$freelancer_profile_id = intval( $freelancer_profile_id );

global $wpdb;
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$attachments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}gigneo_attachments WHERE post_id = %d AND post_type = %s ORDER BY uploaded_at DESC", $order_id, 'order' ) );
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching

$employer_files = count(
	array_filter(
		$attachments,
		function ( $att ) use ( $profile_id ) {
			return isset( $att->profile_id ) && intval( $att->profile_id ) === $profile_id;
		}
	)
);

$freelancer_files = count(
	array_filter(
		$attachments,
		function ( $att ) use ( $freelancer_profile_id ) {
			return isset( $att->profile_id ) && intval( $att->profile_id ) === $freelancer_profile_id;
		}
	)
);

$attachment_types = array(
	'upload'      => esc_html__( 'Upload', 'gigneo' ),
	'requirement' => esc_html__( 'Requirement', 'gigneo' ),
	'deliverable' => esc_html__( 'Deliverable', 'gigneo' ),
);
?>
<div class="gn-order-attachments-wrapper">
	<div id="gn-order-attachments" class="gn-order-section gn-order-attachments">
		<div class="gn-head">
			<div class="gn-heading-info">
				<h3 class="gn-title"><?php echo esc_html__( 'Attachments', 'gigneo' ); ?></h3>
			</div>
			<ul class="gn-attachment-filters">
				<li data-gn-filter="all" class="gn-active">
					<?php echo esc_html__( 'All', 'gigneo' ); ?>
					<span class="gn-count"><?php echo esc_html( count( $attachments ) ); ?></span>
				</li>
				<li data-gn-filter="employer-files">
					<?php
					printf(
							// translators: %s is employer singular
						esc_html__( 'By %s', 'gigneo' ),
						esc_html( gigneo_employer_singular() )
					);
					?>
					<span class="gn-count"><?php echo esc_html( $employer_files ?? 0 ); ?></span>
				</li>
				<li data-gn-filter="freelancer-files">
					<?php
					printf(
					// translators: %s is freelancer singular
						esc_html__( 'By %s', 'gigneo' ),
						esc_html( gigneo_freelancer_singular() )
					);
					?>
					<span class="gn-count"><?php echo esc_html( $freelancer_files ?? 0 ); ?></span>
				</li>
			</ul>
		</div>
		<div class="gn-body">
			<ul class="gn-attachment-list">
				<?php
				if ( ! empty( $attachments ) ) {
					foreach ( $attachments as $attachment ) {
						$attachment_path = gigneo_upload_dir_path( $attachment->path );
						$attachment_url  = gigneo_upload_dir_url( $attachment->path );
						if ( file_exists( $attachment_path ) ) {
							$attachment_meta       = ! empty( $attachment->meta ) ? maybe_unserialize( $attachment->meta ) : array();
							$attachment_type       = ! empty( $attachment_meta['type'] ) ? $attachment_meta['type'] : '';
							$attachment_type_label = ! empty( $attachment_types[ $attachment_type ] ) ? $attachment_types[ $attachment_type ] : '';
							?>
						<li class="gn-attachment gn-<?php echo esc_attr( absint( $attachment->profile_id ) === $freelancer_profile_id ? 'freelancer-files' : 'employer-files' ); ?>">
								<span class="gn-icon">
									<i class="hi hi-document-text" aria-hidden="true"></i>
								</span>
							<div class="gn-info">
								<h5 class="gn-title">
									<?php echo esc_html( $attachment->name ); ?>
									<?php
									if ( ! empty( $attachment_type_label ) ) {
										?>
										<span class="gn-status-tag gn-<?php echo esc_attr( $attachment_type ); ?>"><?php echo esc_html( $attachment_type_label ); ?></span>
									<?php } ?>
								</h5>
								<div class="gn-details">
										<?php
										if ( ! empty( $attachment->profile_id ) ) {
											$attachment_profile_image_id = get_post_thumbnail_id( $attachment->profile_id );
											?>
										<div class="gn-author">
											<figure class="gn-avatar">
												<?php gigneo_attachment_image_src( $attachment_profile_image_id, 'thumbnail', 'profile' ); ?>
											</figure>
											<h6 class="gn-name"><?php echo esc_html( gigneo_profile_name( $attachment->profile_id ) ); ?></h6>
										</div>
										<?php } ?>
										<?php
										if ( ! empty( $attachment->size ) ) {
											?>
										<span class="gn-size">
											<i class="hi hi-server-stack" aria-hidden="true"></i>
											<?php
											printf(
											// translators: %s is attachment size in MB
												esc_html__( '%s MB', 'gigneo' ),
												esc_html( number_format( $attachment->size / ( 1024 * 1024 ), 2 ) )
											);
											?>
											</span>
											<?php
										}
										if ( ! empty( $attachment->uploaded_at ) ) {
											$datetime_format = $date_format . ' ' . $time_format;
											$upload_date     = gigneo_datetime( $attachment->uploaded_at_gmt );
											$upload_date     = gmdate( $datetime_format, strtotime( $upload_date ) );
											?>
										<span class="gn-date"><i class="hi hi-calendar" aria-hidden="true"></i><?php echo esc_html( $upload_date ); ?></span>
										<?php } ?>
								</div>
							</div>
							<div class="gn-actions">
								<a href="<?php echo esc_url( $attachment_url ); ?>" class="gn-btn gn-btn-download" download>
									<i class="hi hi-arrow-down-tray" aria-hidden="true"></i>
								</a>
							</div>
						</li>
							<?php
						}
					}
				} else {
					$not_found_args = array(
						'title'       => esc_html__( 'No attachments found', 'gigneo' ),
						'description' => esc_html__( 'No attachments found for this order', 'gigneo' ),
					);
					gigneo_not_found( 'order-attachments', $not_found_args );
				}
				?>
			</ul>
		</div>
	</div>
</div>
