<?php
/**
 * Single Project Template
 *
 * Renders the project detail page: Carbon Blue hero with provider,
 * title, and version; Download / View Repo CTAs; two-column body with
 * main content and a sidebar of owner, repository details, and stat
 * cards.
 *
 * @package KsPortfolio
 */

namespace KsPortfolio;

get_header();

while ( have_posts() ) :
	the_post();
	$project_id    = (int) get_the_ID();
	$is_website    = Projects::is_website( $project_id );
	$site_url      = Projects::get_site_url( $project_id );
	$site_platform = Projects::get_site_platform( $project_id );
	$site_shot_url = Projects::get_site_screenshot_url( $project_id, 'large' );
	$provider      = $is_website ? __( 'Website', 'ks-portfolio' ) : Projects::get_provider_label( $project_id );
	$version       = Projects::get_latest_version( $project_id );
	$release       = Projects::get_release_url( $project_id );
	$repo          = Projects::get_repo_browse_url( $project_id );
	$repo_data     = Projects::get_repo_data( $project_id );
	$owner         = Projects::get_owner_data( $project_id );
	$type_terms    = get_the_terms( $project_id, 'project-type' );
	$type_label    = ! empty( $type_terms ) && ! is_wp_error( $type_terms ) ? strtoupper( $type_terms[0]->name ) : '';
	$excerpt       = has_excerpt( $project_id ) ? get_the_excerpt( $project_id ) : '';
	?>
	<article class="single-project">
		<div class="container mx-auto my-section">

			<header class="single-project__hero">
				<p class="single-project__meta">
					<span class="single-project__provider"><?php echo esc_html( $provider ); ?></span>
					<?php if ( $type_label ) : ?>
						<span class="single-project__type"><?php echo esc_html( $type_label ); ?></span>
					<?php endif; ?>
				</p>
				<h1 class="single-project__title">
					<?php echo esc_html( get_the_title( $project_id ) ); ?>
					<?php if ( ! $is_website && 'Unknown' !== $version ) : ?>
						<span class="single-project__version">v<?php echo esc_html( $version ); ?></span>
					<?php endif; ?>
				</h1>
				<?php if ( $excerpt ) : ?>
					<p class="single-project__excerpt"><?php echo esc_html( $excerpt ); ?></p>
				<?php endif; ?>

				<div class="single-project__actions">
					<?php if ( $is_website ) : ?>
						<?php if ( $site_url ) : ?>
							<a class="button button--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 ) : ?>
							<a class="button button--primary" href="<?php echo esc_url( Projects::get_download_url( $project_id ) ); ?>" rel="nofollow">
								<?php echo esc_html__( 'Download', 'ks-portfolio' ); ?>
								<?php if ( 'Unknown' !== $version ) : ?>
									v<?php echo esc_html( $version ); ?>
								<?php endif; ?>
							</a>
						<?php endif; ?>
						<?php if ( $repo ) : ?>
							<a class="button button--outline" href="<?php echo esc_url( $repo ); ?>" target="_blank" rel="noopener noreferrer">
								<?php echo esc_html__( 'View Repo', 'ks-portfolio' ); ?>
							</a>
						<?php endif; ?>
					<?php endif; ?>
				</div>
			</header>

			<div class="single-project__layout">
				<div class="single-project__body">
					<?php if ( $is_website && $site_shot_url ) : ?>
						<figure class="single-project__screenshot">
							<a href="<?php echo esc_url( $site_url ); ?>" target="_blank" rel="noopener noreferrer">
								<img src="<?php echo esc_url( $site_shot_url ); ?>" alt="<?php echo esc_attr( get_the_title( $project_id ) ); ?>" />
							</a>
						</figure>
					<?php elseif ( has_post_thumbnail( $project_id ) ) : ?>
						<figure class="single-project__featured">
							<?php echo get_the_post_thumbnail( $project_id, 'large' ); ?>
						</figure>
					<?php endif; ?>

					<section class="single-project__content" aria-label="<?php echo esc_attr__( 'Architecture and Implementation', 'ks-portfolio' ); ?>">
						<h2><?php echo esc_html__( 'Architecture & Implementation', 'ks-portfolio' ); ?></h2>
						<?php the_content(); ?>
					</section>
				</div>

				<aside class="single-project__sidebar" aria-label="<?php echo esc_attr__( $is_website ? __( 'Website details', 'ks-portfolio' ) : __( 'Repository details', 'ks-portfolio' ) ); ?>">
					<?php if ( $is_website ) : ?>
						<?php if ( $site_platform ) : ?>
							<section class="single-project__details">
								<h3><?php echo esc_html__( 'Website Details', 'ks-portfolio' ); ?></h3>
								<?php
								set_query_var( 'label', __( 'Platform', 'ks-portfolio' ) );
								set_query_var( 'value', $site_platform );
								get_template_part( 'views/components/project-meta-row' );
								?>
							</section>
						<?php endif; ?>
					<?php else : ?>
						<?php if ( $owner ) : ?>
							<section class="single-project__maintainer">
								<h3><?php echo esc_html__( 'Maintainer', 'ks-portfolio' ); ?></h3>
								<div class="single-project__owner">
									<?php if ( ! empty( $owner['avatar_url'] ) ) : ?>
										<span class="single-project__owner-avatar" style="background-image: url('<?php echo esc_url( $owner['avatar_url'] ); ?>');" aria-hidden="true"></span>
									<?php endif; ?>
									<a class="single-project__owner-link" href="<?php echo esc_url( ! empty( $owner['html_url'] ) ? $owner['html_url'] : ( 'https://github.com/' . $owner['login'] ) ); ?>" target="_blank" rel="noopener noreferrer">
										<?php echo esc_html( $owner['login'] ); ?>
									</a>
								</div>
							</section>
						<?php endif; ?>

						<section class="single-project__details">
							<h3><?php echo esc_html__( 'Repository Details', 'ks-portfolio' ); ?></h3>
							<?php
							$updated_at = is_array( $repo_data ) && ! empty( $repo_data['updated_at'] )
								? human_time_diff( strtotime( $repo_data['updated_at'] ) ) . ' ' . __( 'ago', 'ks-portfolio' )
								: '—';
							$license    = is_array( $repo_data ) && ! empty( $repo_data['license']['name'] ) ? $repo_data['license']['name'] : __( 'None', 'ks-portfolio' );
							$language   = is_array( $repo_data ) && ! empty( $repo_data['language'] ) ? $repo_data['language'] : '—';

							$rows   = array();
							$rows[] = array(
								'label' => __( 'Version', 'ks-portfolio' ),
								'value' => 'Unknown' !== $version ? 'v' . $version : __( 'Unknown', 'ks-portfolio' ),
							);
							$rows[] = array(
								'label' => __( 'Last Updated', 'ks-portfolio' ),
								'value' => $updated_at,
							);
							$rows[] = array(
								'label' => __( 'License', 'ks-portfolio' ),
								'value' => $license,
							);
							$rows[] = array(
								'label' => __( 'Language', 'ks-portfolio' ),
								'value' => $language,
							);

							foreach ( $rows as $row ) :
								set_query_var( 'label', $row['label'] );
								set_query_var( 'value', $row['value'] );
								get_template_part( 'views/components/project-meta-row' );
							endforeach;
							?>
						</section>

						<section class="single-project__stats" aria-label="<?php echo esc_attr__( 'Repository statistics', 'ks-portfolio' ); ?>">
							<?php
							$stats   = array();
							$stats[] = array(
								'label' => __( 'Downloads', 'ks-portfolio' ),
								'value' => Projects::format_stat( get_post_meta( $project_id, '_projects_portfolio_download_count', true ) ),
							);
							$stats[] = array(
								'label' => __( 'Stars', 'ks-portfolio' ),
								'value' => Projects::format_stat( is_array( $repo_data ) ? ( $repo_data['stargazers_count'] ?? 0 ) : 0 ),
							);
							$stats[] = array(
								'label' => __( 'Forks', 'ks-portfolio' ),
								'value' => Projects::format_stat( is_array( $repo_data ) ? ( $repo_data['forks_count'] ?? 0 ) : 0 ),
							);
							$stats[] = array(
								'label' => __( 'Issues', 'ks-portfolio' ),
								'value' => Projects::format_stat( is_array( $repo_data ) ? ( $repo_data['open_issues_count'] ?? 0 ) : 0 ),
							);

							foreach ( $stats as $stat ) :
								set_query_var( 'label', $stat['label'] );
								set_query_var( 'value', $stat['value'] );
								get_template_part( 'views/components/project-stat-card' );
							endforeach;
							?>
						</section>
					<?php endif; ?>
				</aside>
			</div>
		</div>
	</article>
	<?php
endwhile;

get_footer();
