<?php
/**
 * WordPress Dashboard Widget
 *
 * Adds a widget to the WordPress admin homepage (/wp-admin/)
 * showing ClearPost status at a glance.
 *
 * @package Simple_AI_Auto_Post
 * @since 2.5.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Register the dashboard widget.
 */
function saiap_register_dashboard_widget() {
	// Only for users who can manage options.
	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}

	wp_add_dashboard_widget(
		'saiap_dashboard_widget',
		__( 'ClearPost - SEO AI Agent, Content Generator and MCP', 'clearpost-simple-ai-auto-post' ),
		'saiap_render_dashboard_widget'
	);
}
add_action( 'wp_dashboard_setup', 'saiap_register_dashboard_widget' );

/**
 * Render the dashboard widget content.
 */
function saiap_render_dashboard_widget() {
	$scenario = saiap_get_user_scenario();

	switch ( $scenario ) {
		case 'no_trial_no_keys':
			saiap_widget_no_trial_no_keys();
			break;

		case 'no_trial_has_keys':
			saiap_widget_no_trial_has_keys();
			break;

		case 'active_trial':
			saiap_widget_active_trial();
			break;

		case 'expired_trial':
			saiap_widget_expired_trial();
			break;

		case 'paid':
			saiap_widget_paid();
			break;
	}
}

/**
 * Widget for new users without trial or API keys.
 */
function saiap_widget_no_trial_no_keys() {
	?>
	<div class="saiap-widget saiap-widget--cta">
		<div class="saiap-widget__icon">
			<span class="dashicons dashicons-edit"></span>
		</div>
		<h3><?php esc_html_e( 'Start Generating Content on Autopilot', 'clearpost-simple-ai-auto-post' ); ?></h3>
		<p><?php esc_html_e( 'Let AI research SEO keywords, create a content calendar, and publish posts automatically.', 'clearpost-simple-ai-auto-post' ); ?></p>
		<button type="button" class="button button-primary button-hero saiap-start-trial-btn">
			<?php esc_html_e( 'Start Free 7-Day Trial', 'clearpost-simple-ai-auto-post' ); ?>
		</button>
		<p class="saiap-widget__secondary">
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=saiap&tab=ai-settings' ) ); ?>">
				<?php esc_html_e( 'Or configure your own API keys', 'clearpost-simple-ai-auto-post' ); ?>
			</a>
		</p>
	</div>
	<?php
}

/**
 * Widget for users with API keys but no trial.
 */
function saiap_widget_no_trial_has_keys() {
	?>
	<div class="saiap-widget saiap-widget--upgrade">
		<div class="saiap-widget__icon">
			<span class="dashicons dashicons-update-alt"></span>
		</div>
		<h3><?php esc_html_e( 'Upgrade to Automated Publishing', 'clearpost-simple-ai-auto-post' ); ?></h3>
		<p><?php esc_html_e( 'You\'re generating content manually. Try automated SEO-driven publishing - free for 7 days.', 'clearpost-simple-ai-auto-post' ); ?></p>
		<button type="button" class="button button-primary saiap-start-trial-btn">
			<?php esc_html_e( 'Start Free Trial', 'clearpost-simple-ai-auto-post' ); ?>
		</button>
		<p class="saiap-widget__secondary">
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=saiap&tab=settings' ) ); ?>">
				<?php esc_html_e( 'Continue with manual generation', 'clearpost-simple-ai-auto-post' ); ?>
			</a>
		</p>
	</div>
	<?php
}

/**
 * Widget for users with active trial.
 */
function saiap_widget_active_trial() {
	$total_generated = function_exists( 'saiap_get_generated_posts_count' ) ? saiap_get_generated_posts_count() : 0;
	$scheduled       = function_exists( 'saiap_get_planned_calendar_items_count' ) ? saiap_get_planned_calendar_items_count() : 0;
	$dashboard_url   = admin_url( 'admin.php?page=saiap&tab=dashboard' );

	?>
	<div class="saiap-widget saiap-widget--status saiap-widget--active">
		<?php if ( function_exists( 'saiap_render_trial_status_badge' ) ) : ?>
			<div class="saiap-widget__trial-badge">
				<?php saiap_render_trial_status_badge(); ?>
			</div>
		<?php else : ?>
			<div class="saiap-widget__header">
				<span class="saiap-widget__status-indicator saiap-widget__status-indicator--running"></span>
				<span class="saiap-widget__status-text"><?php esc_html_e( 'ClearPost is running', 'clearpost-simple-ai-auto-post' ); ?></span>
			</div>
		<?php endif; ?>

		<div class="saiap-widget__stats">
			<div class="saiap-widget__stat">
				<span class="saiap-widget__stat-value"><?php echo esc_html( number_format_i18n( $total_generated ) ); ?></span>
				<span class="saiap-widget__stat-label"><?php echo esc_html( _n( 'Post Generated', 'Posts Generated', $total_generated, 'clearpost-simple-ai-auto-post' ) ); ?></span>
			</div>
			<div class="saiap-widget__stat">
				<span class="saiap-widget__stat-value"><?php echo esc_html( number_format_i18n( $scheduled ) ); ?></span>
				<span class="saiap-widget__stat-label"><?php echo esc_html( _n( 'Post Scheduled', 'Posts Scheduled', $scheduled, 'clearpost-simple-ai-auto-post' ) ); ?></span>
			</div>
		</div>

		<a href="<?php echo esc_url( $dashboard_url ); ?>" class="button">
			<?php esc_html_e( 'Open Dashboard', 'clearpost-simple-ai-auto-post' ); ?>
		</a>
	</div>
	<?php
}

/**
 * Widget for users with expired trial.
 */
function saiap_widget_expired_trial() {
	$pricing_url = saiap_get_checkout_url();
	?>
	<div class="saiap-widget saiap-widget--status saiap-widget--paused">
		<div class="saiap-widget__header">
			<span class="saiap-widget__status-indicator saiap-widget__status-indicator--paused"></span>
			<span class="saiap-widget__status-text"><?php esc_html_e( 'ClearPost is paused', 'clearpost-simple-ai-auto-post' ); ?></span>
		</div>

		<p class="saiap-widget__message">
			<?php esc_html_e( 'Your 7-day trial has ended. Subscribe to continue automated publishing.', 'clearpost-simple-ai-auto-post' ); ?>
		</p>

		<a href="<?php echo esc_url( $pricing_url ); ?>" class="button button-primary" target="_blank">
			<?php esc_html_e( 'Resume Publishing', 'clearpost-simple-ai-auto-post' ); ?>
		</a>

		<p class="saiap-widget__secondary">
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=saiap&tab=ai-settings' ) ); ?>">
				<?php esc_html_e( 'Or continue manually with API keys', 'clearpost-simple-ai-auto-post' ); ?>
			</a>
		</p>
	</div>
	<?php
}

/**
 * Widget for paid users.
 */
function saiap_widget_paid() {
	$total_generated = function_exists( 'saiap_get_generated_posts_count' ) ? saiap_get_generated_posts_count() : 0;
	$scheduled       = function_exists( 'saiap_get_planned_calendar_items_count' ) ? saiap_get_planned_calendar_items_count() : 0;
	$dashboard_url   = admin_url( 'admin.php?page=saiap&tab=dashboard' );

	?>
	<div class="saiap-widget saiap-widget--status saiap-widget--active">
		<div class="saiap-widget__header">
			<span class="saiap-widget__status-indicator saiap-widget__status-indicator--running"></span>
			<span class="saiap-widget__status-text"><?php esc_html_e( 'ClearPost is running', 'clearpost-simple-ai-auto-post' ); ?></span>
		</div>

		<div class="saiap-widget__stats">
			<div class="saiap-widget__stat">
				<span class="saiap-widget__stat-value"><?php echo esc_html( number_format_i18n( $total_generated ) ); ?></span>
				<span class="saiap-widget__stat-label"><?php echo esc_html( _n( 'Post Generated', 'Posts Generated', $total_generated, 'clearpost-simple-ai-auto-post' ) ); ?></span>
			</div>
			<div class="saiap-widget__stat">
				<span class="saiap-widget__stat-value"><?php echo esc_html( number_format_i18n( $scheduled ) ); ?></span>
				<span class="saiap-widget__stat-label"><?php echo esc_html( _n( 'Post Scheduled', 'Posts Scheduled', $scheduled, 'clearpost-simple-ai-auto-post' ) ); ?></span>
			</div>
		</div>

		<a href="<?php echo esc_url( $dashboard_url ); ?>" class="button">
			<?php esc_html_e( 'Open Dashboard', 'clearpost-simple-ai-auto-post' ); ?>
		</a>
	</div>
	<?php
}

/**
 * Enqueue widget styles.
 */
function saiap_enqueue_dashboard_widget_styles( $hook ) {
	if ( 'index.php' !== $hook ) {
		return;
	}

	// Only for users who can manage options.
	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}

	wp_enqueue_style(
		'saiap-dashboard-widget',
		plugin_dir_url( dirname( __FILE__ ) ) . 'assets/css/dashboard-widget.css',
		array(),
		defined( 'SAIAP_VERSION' ) ? SAIAP_VERSION : '1.0.0'
	);

	// Enqueue trial badge styles for the reusable badge component.
	wp_enqueue_style(
		'saiap-trial',
		plugin_dir_url( dirname( __FILE__ ) ) . 'assets/css/trial.css',
		array(),
		defined( 'SAIAP_VERSION' ) ? SAIAP_VERSION : '1.0.0'
	);

	// Enqueue JS for trial button (uses global banner JS).
	$js_file = plugin_dir_path( dirname( __FILE__ ) ) . 'assets/js/global-banner.js';
	wp_enqueue_script(
		'saiap-global-banner',
		plugin_dir_url( dirname( __FILE__ ) ) . 'assets/js/global-banner.js',
		array( 'jquery' ),
		file_exists( $js_file ) ? filemtime( $js_file ) : '1.0.0',
		true
	);

	wp_localize_script(
		'saiap-global-banner',
		'saiapGlobalBanner',
		array(
			'ajaxurl'    => admin_url( 'admin-ajax.php' ),
			'trialNonce' => wp_create_nonce( 'saiap_trial_nonce' ),
			'adminEmail' => get_option( 'admin_email', '' ),
		)
	);
}
add_action( 'admin_enqueue_scripts', 'saiap_enqueue_dashboard_widget_styles' );

