<?php
/**
 * Single Changelog Template
 *
 * @package PCChangelogManager
 */

// Prevent direct access to file.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Let theme handle header/footer
get_header();
?>

<div class="site-content" role="main">
	<?php
	while ( have_posts() ) :
		the_post();

		$post_id       = get_the_ID();
		$post_type     = new PC_CLM_Post_Type();
		$version       = $post_type->get_meta( $post_id, 'version_number' );
		$release_date  = $post_type->get_meta( $post_id, 'release_date' );
		$category      = $post_type->get_meta( $post_id, 'category' );
		$categories    = $post_type->get_categories();
		$category_name = isset( $categories[ $category ] ) ? $categories[ $category ] : $category;

		$upvote_count = (int) get_post_meta( $post_id, '_pc_clm_upvotes', true );
		$has_voted = false;

		$ip_address = $_SERVER['REMOTE_ADDR'];
		$user_id = get_current_user_id();
		$voted_key = $user_id ? "pc_clm_voted_user_{$user_id}" : "pc_clm_voted_ip_{$ip_address}";
		$voted_posts = get_transient( $voted_key );

		if ( $voted_posts && in_array( $post_id, $voted_posts ) ) {
			$has_voted = true;
		}

		$entry_classes = array( 'pc-clm-entry', 'pc-clm-single-entry' );
		if ( $category ) {
			$entry_classes[] = 'pc-clm-category-' . $category;
		}
		?>
		<article id="post-<?php the_ID(); ?>" <?php post_class( $entry_classes ); ?>>
			<div class="pc-clm-entry-wrapper">
				<div class="pc-clm-entry-header">
					<div class="pc-clm-entry-meta">
						<?php if ( $version ) : ?>
							<span class="pc-clm-version-badge"><?php echo esc_html( $version ); ?></span>
						<?php endif; ?>

						<?php if ( $release_date ) : ?>
							<span class="pc-clm-release-date"><?php echo esc_html( date_i18n( get_option( 'date_format' ), strtotime( $release_date ) ) ); ?></span>
						<?php endif; ?>

						<?php if ( $category ) : ?>
							<span class="pc-clm-category-badge pc-clm-category-<?php echo esc_attr( $category ); ?>"><?php echo esc_html( $category_name ); ?></span>
						<?php endif; ?>
					</div>

					<div class="pc-clm-upvote-section">
						<button class="pc-clm-upvote-btn <?php echo $has_voted ? 'voted' : ''; ?>"
						        data-post-id="<?php echo esc_attr( $post_id ); ?>"
						        <?php echo $has_voted ? 'disabled' : ''; ?>>
							<span class="pc-clm-upvote-icon">
								<svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
									<path d="M12 2l2.4 7.4h7.6l-6 4.6 2.3 7.4-6.3-4.6-6.3 4.6 2.3-7.4-6-4.6h7.6z"/>
								</svg>
							</span>
							<span class="pc-clm-upvote-count"><?php echo esc_html( $upvote_count ); ?></span>
						</button>
						<span class="pc-clm-upvote-label"><?php echo esc_html( _n( 'upvote', 'upvotes', $upvote_count, 'pc-changelog-manager-abc123' ) ); ?></span>
					</div>
				</div>

				<div class="pc-clm-entry-content-wrapper">
					<h1 class="pc-clm-entry-title">
						<?php the_title(); ?>
					</h1>

					<div class="pc-clm-entry-content">
						<?php the_content(); ?>
					</div>
				</div>
			</div>
		</article>

		<?php
		if ( comments_open() || get_comments_number() ) {
			comments_template();
		}
	endwhile;
	?>
</div>

<?php get_footer(); ?>
