<?php
/**
 * File Path: templates/admin/dashboard-brand.php
 *
 * Admin Dashboard — Brand Monitor partial (Dashboard Phase 2)
 *
 * PRO:  Fetches the configured Google Alerts RSS URL via fetch_feed().
 *       Cached 12 hours (mbs_brand_feed_cache). Manual refresh via AJAX.
 * FREE: Static 'Check your presence' link block (Goodreads, Amazon, etc.).
 *
 * @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

$is_pro    = mbs_is_pro();
$brand_url = get_option( 'mbs_setting_brand_monitor_url', '' );

// DASH-2A: Widget visibility gate.
if ( MBS_Dashboard_Widget_Prefs::is_widget_hidden( 'brand-monitor', get_current_user_id() ) ) {
	return;
}

// DASH-2A: Multi-URL feed storage.
$feed_urls_raw = get_option( 'mbs_setting_brand_feed_urls', '' );
$feed_urls     = ( '' !== $feed_urls_raw ) ? (array) json_decode( $feed_urls_raw, true ) : array();
if ( empty( $feed_urls ) && ! empty( $brand_url ) ) {
	$feed_urls = array( $brand_url );
}

$author_terms = get_terms(
	array(
		'taxonomy'   => MBS_TAX_AUTHOR,
		'hide_empty' => false,
		'number'     => 1,
	)
);
$author_name = ( ! is_wp_error( $author_terms ) && ! empty( $author_terms ) )
	? $author_terms[0]->name
	: get_bloginfo( 'name' );
$author_slug = rawurlencode( $author_name );
?>
<div class="mbs-dashboard-card mbs-brand-card">

	<h2><?php esc_html_e( 'Brand Monitor', 'my-book-showroom' ); ?></h2>

	<?php if ( $is_pro && ! empty( $feed_urls ) ) : ?>
		<!-- PRO: live Google Alerts RSS feeds (multi-URL, DB-backed FIFO) -->
		<?php $grouped = MBS_Admin_Dashboard::fetch_brand_feed(); ?>
		<div class="mbs-brand-list" id="mbs-brand-list">
		<?php if ( ! empty( $grouped ) ) : ?>
			<?php foreach ( $grouped as $domain => $domain_items ) : ?>
				<div class="mbs-brand-feed-group">
					<h4 class="mbs-brand-feed-domain"><?php echo esc_html( $domain ); ?></h4>
					<ul class="mbs-brand-item-list">
						<?php foreach ( $domain_items as $item ) : ?>
							<li class="mbs-brand-item">
								<a href="<?php echo esc_url( $item['url'] ); ?>" target="_blank" rel="noopener">
									<?php echo esc_html( $item['title'] ); ?>
								</a>
								<span class="mbs-brand-date"><?php echo esc_html( $item['date'] ); ?></span>
							</li>
						<?php endforeach; ?>
					</ul>
				</div>
			<?php endforeach; ?>
		<?php else : ?>
			<p class="mbs-brand-empty">
				<?php esc_html_e( 'No mentions found yet. Check back later.', 'my-book-showroom' ); ?>
			</p>
		<?php endif; ?>
		</div><!-- #mbs-brand-list -->
		<button type="button" class="button mbs-refresh-brand" style="margin-top:8px;">
			<?php esc_html_e( 'Refresh All', 'my-book-showroom' ); ?>
		</button>
		<p class="description" style="margin-top:6px;font-size:11px;">
			<?php esc_html_e( 'Cached 1 hour per feed.', 'my-book-showroom' ); ?>
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=my-book-showroom&tab=dashboard' ) ); ?>">
				<?php esc_html_e( 'Configure feed URLs', 'my-book-showroom' ); ?>
			</a>
		</p>

	<?php elseif ( $is_pro && empty( $feed_urls ) ) : ?>
		<!-- PRO but no URL configured -->
		<p class="description">
			<?php
			printf(
				/* translators: %s: settings link */
				esc_html__( 'No Google Alerts RSS URL configured. %s to set it up.', 'my-book-showroom' ),
				'<a href="' . esc_url( admin_url( 'admin.php?page=my-book-showroom&tab=dashboard' ) ) . '">' .
				esc_html__( 'Go to Dashboard Settings', 'my-book-showroom' ) . '</a>'
			);
			?>
		</p>
		<div class="mbs-brand-static-links">
			<?php // Fall through to static links below. ?>
		</div>

	<?php else : ?>
		<!-- FREE: static platform links -->
		<p class="description">
			<?php esc_html_e( 'Check your presence on major book platforms.', 'my-book-showroom' ); ?>
		</p>
	<?php endif; ?>

	<?php if ( ! $is_pro || empty( $feed_urls ) ) : ?>
	<div class="mbs-brand-static-links">
		<ul>
			<li>
				<a href="<?php echo esc_url( 'https://www.goodreads.com/search?q=' . $author_slug ); ?>"
				   target="_blank" rel="noopener">
					Goodreads <span class="dashicons dashicons-external" style="font-size:14px;vertical-align:middle;"></span>
				</a>
			</li>
			<li>
				<a href="<?php echo esc_url( 'https://www.amazon.com/s?k=' . $author_slug . '&i=stripbooks' ); ?>"
				   target="_blank" rel="noopener">
					Amazon Books <span class="dashicons dashicons-external" style="font-size:14px;vertical-align:middle;"></span>
				</a>
			</li>
			<li>
				<a href="<?php echo esc_url( 'https://www.bookbub.com/search/authors?search=' . $author_slug ); ?>"
				   target="_blank" rel="noopener">
					BookBub <span class="dashicons dashicons-external" style="font-size:14px;vertical-align:middle;"></span>
				</a>
			</li>
			<li>
				<a href="<?php echo esc_url( 'https://www.barnesandnoble.com/s/' . $author_slug ); ?>"
				   target="_blank" rel="noopener">
					Barnes &amp; Noble <span class="dashicons dashicons-external" style="font-size:14px;vertical-align:middle;"></span>
				</a>
			</li>
		</ul>
		<p class="description" style="margin-top:8px;font-size:12px;">
			<?php
			printf(
				/* translators: %s: author name */
				esc_html__( 'Searching for: %s', 'my-book-showroom' ),
				'<strong>' . esc_html( $author_name ) . '</strong>'
			);
			?>
		</p>
		<?php if ( ! $is_pro ) : ?>
		<!-- MBS: Brand Monitor RSS requires PRO license -->
		<p class="description mbs-pro-upsell" style="margin-top:10px;">
			<?php
			printf(
				/* translators: %s: upgrade link */
				esc_html__( 'Upgrade to PRO to monitor Google Alerts mentions directly from your dashboard. %s', 'my-book-showroom' ),
				'<a href="' . esc_url( admin_url( 'admin.php?page=my-book-showroom&tab=dashboard' ) ) . '">' .
				esc_html__( 'Upgrade to PRO', 'my-book-showroom' ) . ' &rarr;</a>'
			);
			?>
		</p>
		<?php endif; ?>
	</div><!-- .mbs-brand-static-links -->
	<?php endif; ?>

</div><!-- .mbs-brand-card -->


<!-- ── Activity Feed (Publisher tier, Dashboard Phase 2) ───────────────── -->
<?php
$_mbs_uid       = get_current_user_id(); // DASH-2A: required by is_widget_hidden() below.
if ( mbs_is_pub() ) :
	$activity_log = MBS_Activity_Log_DAO::get_recent( 10 );
	if ( ! empty( $activity_log ) ) :
?>
<?php if ( ! MBS_Dashboard_Widget_Prefs::is_widget_hidden( 'activity-log', $_mbs_uid ) ) : // DASH-2A ?>
<div class="mbs-dashboard-card mbs-activity-card">
	<h2><?php esc_html_e( 'Team Activity', 'my-book-showroom' ); ?></h2>
	<ul class="mbs-activity-list">
		<?php foreach ( $activity_log as $entry ) : ?>
			<?php
			$user_data  = get_userdata( (int) $entry->user_id );
			$user_label = $user_data ? $user_data->display_name : __( 'Unknown', 'my-book-showroom' );
			$book_title = get_the_title( (int) $entry->book_id );
			// INSIGHTS-2: include new event-type labels.
				$action_map = array(
					'publish'  => __( 'published', 'my-book-showroom' ),
					'draft'    => __( 'saved as draft', 'my-book-showroom' ),
					'edit'     => __( 'edited', 'my-book-showroom' ),
					'import'   => __( 'ran import', 'my-book-showroom' ),
					'export'   => __( 'exported catalog', 'my-book-showroom' ),
					'arc_send' => __( 'sent ARC for', 'my-book-showroom' ),
				);
			$action_str = $action_map[ $entry->action ] ?? esc_html( $entry->action );
			?>
			<li class="mbs-activity-item">
				<span class="mbs-activity-user"><?php echo esc_html( $user_label ); ?></span>
				<?php echo esc_html( $action_str ); ?>
				<a href="<?php echo esc_url( get_edit_post_link( (int) $entry->book_id ) ); ?>"><?php echo esc_html( $book_title ); ?></a>
				<span class="mbs-activity-time">
					<?php
					echo esc_html(
						sprintf(
							/* translators: %s: human time diff */
							__( '%s ago', 'my-book-showroom' ),
							human_time_diff( strtotime( $entry->created_at ), current_time( 'timestamp', true ) )
						)
					);
					?>
				</span>
			</li>
		<?php endforeach; ?>
	</ul>
</div><!-- activity card -->
<?php endif; endif; ?>
<?php endif; // DASH-2B: close mbs_is_pub() activity feed ?>
