<?php
/**
 * AI Insights dashboard template.
 *
 * @package MerchantKits\AttendanceManager
 */

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

$has_key = '' !== get_option( 'mkam_openai_api_key', '' );
$year    = (int) current_time( 'Y' );
$month   = (int) current_time( 'n' );
?>
<div class="wrap mkam-ai">
	<h1><?php esc_html_e( 'AI Insights', 'merchantkits-attendance-manager' ); ?></h1>

	<?php if ( ! $has_key ) : ?>
		<div class="notice notice-warning">
			<p>
				<?php
				printf(
					/* translators: %s: settings link. */
					esc_html__( 'Add your OpenAI API key in %s to enable AI insights.', 'merchantkits-attendance-manager' ),
					'<a href="' . esc_url( admin_url( 'admin.php?page=mkam-settings&tab=ai' ) ) . '">' . esc_html__( 'AI Settings', 'merchantkits-attendance-manager' ) . '</a>'
				);
				?>
			</p>
		</div>
	<?php endif; ?>

	<div class="mkam-card">
		<h2><?php esc_html_e( 'Smart Attendance Summary', 'merchantkits-attendance-manager' ); ?></h2>
		<p><?php esc_html_e( 'Generate an AI executive summary of attendance for a selected month.', 'merchantkits-attendance-manager' ); ?></p>
		<label>
			<?php esc_html_e( 'Month', 'merchantkits-attendance-manager' ); ?>
			<input type="number" id="mkam-summary-month" min="1" max="12" value="<?php echo esc_attr( $month ); ?>" />
		</label>
		<label>
			<?php esc_html_e( 'Year', 'merchantkits-attendance-manager' ); ?>
			<input type="number" id="mkam-summary-year" min="2000" max="2100" value="<?php echo esc_attr( $year ); ?>" />
		</label>
		<button type="button" class="button button-primary" id="mkam-generate-summary" <?php disabled( ! $has_key ); ?>><?php esc_html_e( 'Generate Summary', 'merchantkits-attendance-manager' ); ?></button>
		<div class="mkam-ai-result" id="mkam-summary-result" aria-live="polite"></div>
	</div>

	<div class="mkam-card">
		<h2><?php esc_html_e( 'Absence Pattern Detection', 'merchantkits-attendance-manager' ); ?></h2>
		<p><?php esc_html_e( 'Analyze absence patterns across a date range.', 'merchantkits-attendance-manager' ); ?></p>
		<label>
			<?php esc_html_e( 'From', 'merchantkits-attendance-manager' ); ?>
			<input type="date" id="mkam-patterns-from" value="<?php echo esc_attr( gmdate( 'Y-m-01' ) ); ?>" />
		</label>
		<label>
			<?php esc_html_e( 'To', 'merchantkits-attendance-manager' ); ?>
			<input type="date" id="mkam-patterns-to" value="<?php echo esc_attr( gmdate( 'Y-m-t' ) ); ?>" />
		</label>
		<button type="button" class="button button-primary" id="mkam-detect-patterns" <?php disabled( ! $has_key ); ?>><?php esc_html_e( 'Detect Patterns', 'merchantkits-attendance-manager' ); ?></button>
		<div class="mkam-ai-result" id="mkam-patterns-result" aria-live="polite"></div>
	</div>

	<?php $mkam_is_pro = function_exists( 'mkam_fs' ) && mkam_fs()->can_use_premium_code(); ?>
	<?php if ( $mkam_is_pro ) : ?>
		<div class="mkam-card">
			<h2><?php esc_html_e( 'Natural Language Query', 'merchantkits-attendance-manager' ); ?> <span class="mkam-badge mkam-badge--present"><?php esc_html_e( 'Pro', 'merchantkits-attendance-manager' ); ?></span></h2>
			<p><?php esc_html_e( 'Ask a question about this month\'s attendance in plain English.', 'merchantkits-attendance-manager' ); ?></p>
			<input type="text" class="large-text" id="mkam-nl-query-input" placeholder="<?php esc_attr_e( 'Who was late more than 3 times this month?', 'merchantkits-attendance-manager' ); ?>" <?php disabled( ! $has_key ); ?> />
			<button type="button" class="button button-primary" id="mkam-nl-query" <?php disabled( ! $has_key ); ?>><?php esc_html_e( 'Ask', 'merchantkits-attendance-manager' ); ?></button>
			<div class="mkam-ai-result" id="mkam-nl-result" aria-live="polite"></div>
		</div>
	<?php else : ?>
		<div class="mkam-card mkam-card--locked">
			<h2><?php esc_html_e( 'Natural Language Query', 'merchantkits-attendance-manager' ); ?> <span class="mkam-badge mkam-badge--pending"><?php esc_html_e( 'Pro', 'merchantkits-attendance-manager' ); ?></span></h2>
			<input type="text" class="large-text" disabled placeholder="<?php esc_attr_e( 'Who was late more than 3 times this month?', 'merchantkits-attendance-manager' ); ?>" />
			<p class="description">
				<?php if ( function_exists( 'mkam_fs' ) ) : ?>
					<a href="<?php echo esc_url( mkam_fs()->get_upgrade_url() ); ?>"><?php esc_html_e( 'Upgrade to Pro', 'merchantkits-attendance-manager' ); ?></a>
					<?php esc_html_e( 'to ask AI questions about your attendance data.', 'merchantkits-attendance-manager' ); ?>
				<?php else : ?>
					<?php esc_html_e( 'Upgrade to Pro to use AI queries.', 'merchantkits-attendance-manager' ); ?>
				<?php endif; ?>
			</p>
		</div>
	<?php endif; ?>
</div>
