<?php if ( ! defined( 'ABSPATH' ) ) { exit; } ?>
<?php
$upload             = isset( $upload ) && is_array( $upload ) ? $upload : array();
$upload_card_variant = isset( $upload_card_variant ) ? sanitize_html_class( (string) $upload_card_variant ) : 'default';
$upload_id          = absint( $upload['id'] ?? 0 );
$upload_status      = sanitize_key( (string) ( $upload['status'] ?? 'pending' ) );
$upload_labels      = isset( $portal['i18n']['statusLabels'] ) && is_array( $portal['i18n']['statusLabels'] ) ? $portal['i18n']['statusLabels'] : array();
$upload_status_text = isset( $upload_labels[ $upload_status ] ) ? (string) $upload_labels[ $upload_status ] : ucfirst( str_replace( '_', ' ', $upload_status ) );
$upload_product     = isset( $upload['product'] ) && is_array( $upload['product'] ) ? $upload['product'] : array();
$upload_caption     = trim( wp_strip_all_tags( (string) ( $upload['caption'] ?? $upload['content'] ?? '' ) ) );
$upload_excerpt     = '' !== $upload_caption ? wp_trim_words( $upload_caption, 14, '...' ) : __( 'No caption added yet.', 'ugcify' );
$upload_title       = ! empty( $upload_product['title'] ) ? (string) $upload_product['title'] : __( 'Creator upload', 'ugcify' );
$upload_date        = ! empty( $upload['created_at'] ) ? strtotime( (string) $upload['created_at'] ) : 0;
$upload_date_label  = $upload_date ? wp_date( get_option( 'date_format', 'M j, Y' ), $upload_date ) : __( 'Just now', 'ugcify' );
$upload_thumbnail   = '';
$upload_media_type  = 'image';
$upload_interactive = 'history' !== $upload_card_variant;

if ( ! empty( $upload['images'][0] ) ) {
	$upload_thumbnail = (string) $upload['images'][0];
} elseif ( ! empty( $upload_product['thumbnail'] ) ) {
	$upload_thumbnail = (string) $upload_product['thumbnail'];
} elseif ( ! empty( $upload['videos'][0] ) ) {
	$upload_media_type = 'video';
}
?>
<article class="ugcify-portal-upload-card is-<?php echo esc_attr( $upload_card_variant ); ?>" data-upload-id="<?php echo esc_attr( (string) $upload_id ); ?>" data-status="<?php echo esc_attr( $upload_status ); ?>">
	<?php if ( $upload_interactive ) : ?>
	<button type="button" class="ugcify-portal-upload-card__button" data-ugcify-portal-upload-detail="<?php echo esc_attr( (string) $upload_id ); ?>" aria-label="<?php echo esc_attr( sprintf( __( 'Open upload detail for %s', 'ugcify' ), $upload_title ) ); ?>">
	<?php else : ?>
	<div class="ugcify-portal-upload-card__button is-static" aria-label="<?php echo esc_attr( $upload_title ); ?>">
	<?php endif; ?>
		<span class="ugcify-portal-upload-card__media">
			<?php if ( '' !== $upload_thumbnail ) : ?>
				<img src="<?php echo esc_url( $upload_thumbnail ); ?>" alt="<?php echo esc_attr( $upload_title ); ?>">
			<?php elseif ( 'video' === $upload_media_type ) : ?>
				<span class="ugcify-portal-upload-card__placeholder"><?php esc_html_e( 'Video', 'ugcify' ); ?></span>
			<?php else : ?>
				<span class="ugcify-portal-upload-card__placeholder"><?php esc_html_e( 'Upload', 'ugcify' ); ?></span>
			<?php endif; ?>
		</span>

		<span class="ugcify-portal-upload-card__content">
			<span class="ugcify-portal-status-badge is-<?php echo esc_attr( $upload_status ); ?>"><?php echo esc_html( $upload_status_text ); ?></span>
			<strong class="ugcify-portal-upload-card__title"><?php echo esc_html( $upload_title ); ?></strong>
			<span class="ugcify-portal-upload-card__excerpt"><?php echo esc_html( $upload_excerpt ); ?></span>
			<span class="ugcify-portal-upload-card__meta"><?php echo esc_html( $upload_date_label ); ?></span>
		</span>
	<?php if ( $upload_interactive ) : ?>
	</button>
	<?php else : ?>
	</div>
	<?php endif; ?>
</article>