<?php
/**
 * Project card.
 *
 * @package Gustavo_Portfolio
 */

$gustavoo_project_id         = get_the_ID();
$gustavoo_project_url        = gustavoo_portfolio_validate_project_url( get_post_meta( $gustavoo_project_id, '_gso_project_url', true ) );
$gustavoo_project_client     = sanitize_text_field( (string) get_post_meta( $gustavoo_project_id, '_gso_project_client', true ) );
$gustavoo_project_year       = absint( get_post_meta( $gustavoo_project_id, '_gso_project_year', true ) );
$gustavoo_project_link_label = sanitize_text_field( (string) get_post_meta( $gustavoo_project_id, '_gso_project_link_label', true ) );
$gustavoo_project_link_label = $gustavoo_project_link_label ?: __( 'Ver projeto', 'gustavoo-portfolio' );
$gustavoo_project_types      = taxonomy_exists( 'gso_project_type' ) ? get_the_terms( $gustavoo_project_id, 'gso_project_type' ) : array();
$gustavoo_project_tech       = taxonomy_exists( 'gso_project_tech' ) ? get_the_terms( $gustavoo_project_id, 'gso_project_tech' ) : array();

if ( ! $gustavoo_project_url ) {
	return;
}
?>

<article <?php post_class( 'project-card' ); ?>>
	<a class="project-card__link" href="<?php echo esc_url( $gustavoo_project_url ); ?>" target="_blank" rel="noopener noreferrer external">
		<figure class="project-card__media">
			<?php if ( has_post_thumbnail() ) : ?>
				<?php
				$gustavoo_thumbnail_id  = get_post_thumbnail_id();
				$gustavoo_thumbnail_alt = get_post_meta( $gustavoo_thumbnail_id, '_wp_attachment_image_alt', true );
				$gustavoo_thumbnail_alt = $gustavoo_thumbnail_alt ?: sprintf(
					/* translators: %s: project title. */
					__( 'Imagem do projeto %s', 'gustavoo-portfolio' ),
					get_the_title()
				);

				echo get_the_post_thumbnail(
					$gustavoo_project_id,
					'gustavoo-project-card',
					array(
						'alt'      => $gustavoo_thumbnail_alt,
						'class'    => 'project-card__image',
						'loading'  => 'lazy',
						'decoding' => 'async',
					)
				); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Core image markup.
				?>
			<?php else : ?>
				<span class="project-card__placeholder" aria-hidden="true">
					<img src="<?php echo esc_url( gustavoo_portfolio_get_image_source( 'logo-mark' ) ); ?>" width="96" height="96" alt="">
				</span>
			<?php endif; ?>
		</figure>

		<div class="project-card__body">
			<?php if ( ! is_wp_error( $gustavoo_project_types ) && $gustavoo_project_types ) : ?>
				<p class="project-card__type"><?php echo esc_html( implode( ' · ', wp_list_pluck( $gustavoo_project_types, 'name' ) ) ); ?></p>
			<?php endif; ?>

			<h3 class="project-card__title"><?php the_title(); ?></h3>

			<?php if ( has_excerpt() ) : ?>
				<p class="project-card__description"><?php echo esc_html( wp_trim_words( get_the_excerpt(), 24 ) ); ?></p>
			<?php endif; ?>

			<?php if ( $gustavoo_project_client || $gustavoo_project_year ) : ?>
				<p class="project-card__meta">
					<?php echo esc_html( implode( ' · ', array_filter( array( $gustavoo_project_client, $gustavoo_project_year ? (string) $gustavoo_project_year : '' ) ) ) ); ?>
				</p>
			<?php endif; ?>

			<?php if ( ! is_wp_error( $gustavoo_project_tech ) && $gustavoo_project_tech ) : ?>
				<ul class="project-card__tags" aria-label="<?php esc_attr_e( 'Tecnologias', 'gustavoo-portfolio' ); ?>">
					<?php foreach ( array_slice( $gustavoo_project_tech, 0, 4 ) as $gustavoo_technology ) : ?>
						<li><?php echo esc_html( $gustavoo_technology->name ); ?></li>
					<?php endforeach; ?>
				</ul>
			<?php endif; ?>

			<span class="project-card__cta">
				<?php echo esc_html( $gustavoo_project_link_label ); ?> <span aria-hidden="true">↗</span>
				<span class="screen-reader-text"> <?php esc_html_e( '(abre em uma nova guia)', 'gustavoo-portfolio' ); ?></span>
			</span>
		</div>
	</a>
</article>
