<?php
/**
 * Project Card Component
 *
 * Renders one project card. Used by front-page (Recent Projects) and
 * archive-projects. Expects $post (WP_Post) in scope.
 *
 * @package KsPortfolio
 */

namespace KsPortfolio;

if ( ! isset( $post ) || ! ( $post instanceof \WP_Post ) ) {
	return;
}

$project_id = (int) $post->ID;
$type_label = '';
$type_terms = get_the_terms( $project_id, 'project-type' );

if ( ! empty( $type_terms ) && ! is_wp_error( $type_terms ) ) {
	$type_label = strtoupper( $type_terms[0]->name );
}

$is_website    = Projects::is_website( $project_id );
$site_url      = Projects::get_site_url( $project_id );
$site_platform = Projects::get_site_platform( $project_id );
$release_url   = Projects::get_release_url( $project_id );
$repo_url      = Projects::get_repo_browse_url( $project_id );
$repo_data     = Projects::get_repo_data( $project_id );
$language      = is_array( $repo_data ) && isset( $repo_data['stargazers_count'] ) ? (int) $repo_data['stargazers_count'] : 0;
$excerpt       = has_excerpt( $project_id ) ? get_the_excerpt( $project_id ) : wp_trim_words( wp_strip_all_tags( $post->post_content ), 30, '…' );
$card_classes  = 'project-card group';
?>

<article class="<?php echo esc_attr( $card_classes ); ?>" aria-labelledby="project-<?php echo esc_attr( (string) $project_id ); ?>-title">
	<header class="project-card__head">
		<?php if ( $type_label ) : ?>
			<span class="project-card__type"><?php echo esc_html( $type_label ); ?></span>
		<?php endif; ?>

		<span class="project-card__provider" aria-hidden="true">
			<?php echo esc_html( $is_website ? __( 'Website', 'ks-portfolio' ) : Projects::get_provider_label( $project_id ) ); ?>
		</span>
	</header>

	<h3 id="project-<?php echo esc_attr( (string) $project_id ); ?>-title" class="project-card__title">
		<a class="project-card__title-link" href="<?php echo esc_url( get_permalink( $project_id ) ); ?>">
			<?php echo esc_html( get_the_title( $project_id ) ); ?>
		</a>
	</h3>

	<?php if ( $excerpt ) : ?>
		<p class="project-card__excerpt"><?php echo esc_html( $excerpt ); ?></p>
	<?php endif; ?>

	<footer class="project-card__foot">
		<?php if ( ! $is_website && ( $language || $stars > 0 ) ) : ?>
			<div class="project-card__meta">
				<?php if ( $language ) : ?>
					<span class="project-card__lang"><?php echo esc_html( $language ); ?></span>
				<?php endif; ?>

                <?php if ( $stars > 0 ) : ?>
					<span class="project-card__stars" aria-label="<?php echo esc_attr( $stars . ' stars' ); ?>">★ <?php echo esc_html( (string) $stars ); ?></span>
				<?php endif; ?>
			</div>
		<?php elseif ( $is_website && $site_platform ) : ?>
			<div class="project-card__meta">
				<span class="project-card__platform"><?php echo esc_html( $site_platform ); ?></span>
			</div>
		<?php endif; ?>

		<div class="project-card__actions">
			<?php if ( $is_website ) : ?>
				<?php if ( $site_url ) : ?>
					<a class="project-card__action project-card__action--primary" href="<?php echo esc_url( $site_url ); ?>" target="_blank" rel="noopener noreferrer">
						<?php echo esc_html__( 'Visit Site', 'ks-portfolio' ); ?>
					</a>
				<?php endif; ?>
			<?php else : ?>
				<?php if ( $release_url ) : ?>
					<a class="project-card__action project-card__action--primary" href="<?php echo esc_url( Projects::get_download_url( $project_id ) ); ?>" rel="nofollow">
						<?php echo esc_html__( 'Download', 'ks-portfolio' ); ?>
					</a>
				<?php endif; ?>

                <?php if ( $repo_url ) : ?>
					<a class="project-card__action project-card__action--secondary" href="<?php echo esc_url( $repo_url ); ?>" target="_blank" rel="noopener noreferrer">
						<?php echo esc_html__( 'View Repo', 'ks-portfolio' ); ?>
					</a>
				<?php endif; ?>
			<?php endif; ?>
		</div>
	</footer>
</article>
