<?php
/**
 * File Path: templates/admin/dashboard-hub.php
 *
 * Admin Dashboard — Hub partial (GC3-2, UX rev 1)
 *
 * Left column. Contains:
 *   - Onboarding panel (empty-catalog gate)
 *   - Catalog stats bar (total, published, drafts, featured)
 *   - 5 quick-action tiles (3-across in narrow column)
 *   - Recently edited rail (4 books)
 *   - Tier-aware tile
 *   - What's New strip
 *
 * Health score and System Status live in dashboard-status.php (center column).
 *
 * @package MyBookShowroom
 * @since   1.1.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variables injected by mbs_get_template_part(); not global vars

$dash   = MBS_Admin_Dashboard::get_instance();
$stats  = $dash->get_catalog_stats();
$is_pro = mbs_is_pro();


$recent_books = get_posts(
	array(
		'post_type'      => MBS_POST_TYPE,
		'post_status'    => array( 'publish', 'draft', 'future' ),
		'posts_per_page' => 4,
		'orderby'        => 'modified',
		'order'          => 'DESC',
		'no_found_rows'  => true,
	)
);
?>

<?php
$_mbs_uid = get_current_user_id();
?>
<?php if ( 0 === $stats['total'] ) : ?>
<!-- ── Onboarding (empty catalog) ─────────────────────────────────────── -->
<div class="mbs-dashboard-card mbs-onboarding-card">
	<h3><?php esc_html_e( 'Welcome to My Book Showroom!', 'my-book-showroom' ); ?></h3>
	<p><?php esc_html_e( "Your catalog is empty — let’s add your first book.", 'my-book-showroom' ); ?></p>
	<a href="<?php echo esc_url( admin_url( 'post-new.php?post_type=' . MBS_POST_TYPE ) ); ?>"
	   class="button button-primary">
		<?php esc_html_e( 'Add Your First Book', 'my-book-showroom' ); ?>
	</a>
	<a href="<?php echo esc_url( admin_url( 'admin.php?page=my-book-showroom-settings&tab=import-export' ) ); ?>"
	   class="button" style="margin-left:8px;">
		<?php esc_html_e( 'Import Books', 'my-book-showroom' ); ?>
	</a>
</div>
<?php endif; ?>

<!-- ── Catalog Stats ───────────────────────────────────────────────────── -->
<div class="mbs-dashboard-card">
	<h2><?php esc_html_e( 'Your Showroom', 'my-book-showroom' ); ?></h2>
	<div class="mbs-catalog-stats">
		<div class="mbs-stat-item">
			<span class="mbs-stat-number"><?php echo (int) $stats['total']; ?></span>
			<span class="mbs-stat-label"><?php esc_html_e( 'Total', 'my-book-showroom' ); ?></span>
		</div>
		<div class="mbs-stat-item">
			<span class="mbs-stat-number"><?php echo (int) $stats['published']; ?></span>
			<span class="mbs-stat-label"><?php esc_html_e( 'Published', 'my-book-showroom' ); ?></span>
		</div>
		<div class="mbs-stat-item">
			<span class="mbs-stat-number"><?php echo (int) $stats['drafts']; ?></span>
			<span class="mbs-stat-label"><?php esc_html_e( 'Drafts', 'my-book-showroom' ); ?></span>
		</div>
		<div class="mbs-stat-item">
			<span class="mbs-stat-number"><?php echo (int) $stats['featured']; ?></span>
			<span class="mbs-stat-label"><?php esc_html_e( 'Featured', 'my-book-showroom' ); ?></span>
		</div>
	</div>
	<div class="mbs-stats-actions">
		<a href="<?php echo esc_url( admin_url( 'edit.php?post_type=' . MBS_POST_TYPE ) ); ?>"
		   class="button">
			<?php esc_html_e( 'All Books', 'my-book-showroom' ); ?>
		</a>
		<a href="<?php echo esc_url( admin_url( 'post-new.php?post_type=' . MBS_POST_TYPE ) ); ?>"
		   class="button button-primary">
			<?php esc_html_e( 'Add New', 'my-book-showroom' ); ?>
		</a>
	</div>
</div><!-- stats card -->

<!-- ── Recently Edited Rail ───────────────────────────────────────────── -->
<?php if ( ! empty( $recent_books ) ) : ?>
<?php if ( ! MBS_Dashboard_Widget_Prefs::is_widget_hidden( 'recently-edited', $_mbs_uid ) ) : // DASH-2A ?>
<div class="mbs-dashboard-card">
	<h2><?php esc_html_e( 'Recently Edited', 'my-book-showroom' ); ?></h2>
	<ul class="mbs-recent-rail">
		<?php foreach ( $recent_books as $book ) : ?>
			<li>
				<a class="mbs-rail-title"
				   href="<?php echo esc_url( get_edit_post_link( $book->ID ) ); ?>">
					<?php echo esc_html( get_the_title( $book->ID ) ); ?>
				</a>
				<span class="mbs-rail-date">
					<?php
					echo esc_html(
						sprintf(
							/* translators: %s: human time diff */
							__( '%s ago', 'my-book-showroom' ),
							human_time_diff(
								strtotime( $book->post_modified_gmt ),
								current_time( 'timestamp', true )
							)
						)
					);
					?>
				</span>
			</li>
		<?php endforeach; ?>
	</ul>
</div><!-- recently edited card -->
<?php endif; ?>

<!-- ── Pre-Release Pipeline (ARC-5) ───────────────────────────────────── -->
<?php if ( ! MBS_Dashboard_Widget_Prefs::is_widget_hidden( 'arc-pipeline', $_mbs_uid ) ) : // DASH-2A ?>
<?php
$arc_books = MBS_Arc_Recipient_DAO::get_active_arc_books();
if ( ! empty( $arc_books ) ) :
?>
<div class="mbs-dashboard-card">
	<h2><?php esc_html_e( 'Pre-Release Pipeline', 'my-book-showroom' ); ?></h2>
	<ul class="mbs-arc-pipeline-list">
		<?php foreach ( $arc_books as $arc_book ) : ?>
			<?php
			$book_id   = isset( $arc_book->post_id ) ? (int) $arc_book->post_id : 0;
			$title     = get_the_title( $book_id );
			$edit_url  = get_edit_post_link( $book_id );
			$rev_count = MBS_Arc_Recipient_DAO::count_by_book( $book_id );
			$rev_rcvd  = MBS_Arc_Recipient_DAO::count_reviews_received( $book_id );
			?>
			<li class="mbs-arc-pipeline-item">
				<a class="mbs-arc-book-title" href="<?php echo esc_url( $edit_url ); ?>">
					<?php echo esc_html( $title ); ?>
				</a>
				<span class="mbs-arc-pipeline-meta">
					<?php
					echo esc_html(
						sprintf(
							/* translators: 1: reviews received, 2: total sent */
							__( '%1$d / %2$d reviews received', 'my-book-showroom' ),
							$rev_rcvd,
							$rev_count
						)
					);
					?>
				</span>
			</li>
		<?php endforeach; ?>
	</ul>
</div><!-- arc pipeline card -->
<?php endif; ?>
<?php endif; // DASH-2B: close arc-pipeline widget visibility ?>
<?php endif; // close if ( ! empty( $recent_books ) ) ?>

<!-- ── What's New ───────────────────────────────────────────────────────── -->
<?php if ( defined( 'MBS_WHATS_NEW' ) && '' !== MBS_WHATS_NEW ) : ?>
<?php if ( ! MBS_Dashboard_Widget_Prefs::is_widget_hidden( 'whats-new', $_mbs_uid ) ) : // DASH-2A ?>
<div class="mbs-whats-new">
	<strong><?php esc_html_e( "What’s New:", 'my-book-showroom' ); ?></strong>
	<?php echo esc_html( MBS_WHATS_NEW ); ?>
</div>
<?php endif; // inner widget visibility ?>
<?php endif; // outer MBS_WHATS_NEW ?>
