<?php
/**
 * Admin database management template for Ask Any plugin
 *
 * This template displays the database management interface with:
 * - Database usage statistics
 * - Data cleanup operations
 * - Export/Import functionality
 * - Table optimization tools
 *
 * @package Askany
 * @since 1.0.0
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Check pro status
$plugin = \Askany\Plugin::get_instance();
$is_pro = $plugin->is_pro_active();
?>

<div class="wrap askany-database-management">
	<h1>
		<?php esc_html_e( 'Database Management', 'askany' ); ?>
		<?php if ( ! $is_pro ) : ?>
			<span class="askany-pro-badge">PRO</span>
		<?php endif; ?>
	</h1>
	
	<!-- Storage Overview Cards -->
	<div class="askany-db-overview">
		<div class="askany-db-cards">
			<div class="askany-db-card">
				<div class="card-icon">📊</div>
				<div class="card-content">
					<h3><?php esc_html_e( 'Chat Messages', 'askany' ); ?></h3>
					<div class="card-value"><?php echo esc_html( $table_sizes['chat_messages']['formatted_size'] ); ?></div>
					<div class="card-meta"><?php echo esc_html( number_format( $record_counts['chat_messages'] ) ); ?> <?php esc_html_e( 'records', 'askany' ); ?></div>
				</div>
			</div>

			<div class="askany-db-card">
				<div class="card-icon">💬</div>
				<div class="card-content">
					<h3><?php esc_html_e( 'Chat Sessions', 'askany' ); ?></h3>
					<div class="card-value"><?php echo esc_html( $table_sizes['chat_sessions']['formatted_size'] ); ?></div>
					<div class="card-meta"><?php echo esc_html( number_format( $record_counts['chat_sessions'] ) ); ?> <?php esc_html_e( 'records', 'askany' ); ?></div>
				</div>
			</div>

			<div class="askany-db-card">
				<div class="card-icon">❓</div>
				<div class="card-content">
					<h3><?php esc_html_e( 'Q&A Pairs', 'askany' ); ?></h3>
					<div class="card-value"><?php echo esc_html( $table_sizes['qa_pairs']['formatted_size'] ); ?></div>
					<div class="card-meta"><?php echo esc_html( number_format( $record_counts['qa_pairs'] ) ); ?> <?php esc_html_e( 'records', 'askany' ); ?></div>
				</div>
			</div>

			<div class="askany-db-card">
				<div class="card-icon">🔗</div>
				<div class="card-content">
					<h3><?php esc_html_e( 'External Sources', 'askany' ); ?></h3>
					<div class="card-value"><?php echo esc_html( $table_sizes['external_sources']['formatted_size'] ); ?></div>
					<div class="card-meta"><?php echo esc_html( number_format( $record_counts['external_sources'] ) ); ?> <?php esc_html_e( 'records', 'askany' ); ?></div>
				</div>
			</div>

			<div class="askany-db-card">
				<div class="card-icon">📄</div>
				<div class="card-content">
					<h3><?php esc_html_e( 'Content Index', 'askany' ); ?></h3>
					<div class="card-value"><?php echo esc_html( $table_sizes['content_index']['formatted_size'] ); ?></div>
					<div class="card-meta"><?php echo esc_html( number_format( $record_counts['content_index'] ) ); ?> <?php esc_html_e( 'records', 'askany' ); ?></div>
				</div>
			</div>

			<div class="askany-db-card">
				<div class="card-icon">📋</div>
				<div class="card-content">
					<h3><?php esc_html_e( 'PDF Index', 'askany' ); ?></h3>
					<div class="card-value"><?php echo esc_html( $table_sizes['pdf_index']['formatted_size'] ); ?></div>
					<div class="card-meta"><?php echo esc_html( number_format( $record_counts['pdf_index'] ) ); ?> <?php esc_html_e( 'records', 'askany' ); ?></div>
				</div>
			</div>

			<div class="askany-db-card">
				<div class="card-icon">👥</div>
				<div class="card-content">
					<h3><?php esc_html_e( 'User Agents', 'askany' ); ?></h3>
					<div class="card-value"><?php echo esc_html( $table_sizes['user_agents']['formatted_size'] ); ?></div>
					<div class="card-meta"><?php echo esc_html( number_format( $record_counts['user_agents'] ) ); ?> <?php esc_html_e( 'records', 'askany' ); ?></div>
				</div>
			</div>

			<div class="askany-db-card">
				<div class="card-icon">📝</div>
				<div class="card-content">
					<h3><?php esc_html_e( 'Chat History', 'askany' ); ?></h3>
					<div class="card-value"><?php echo esc_html( $table_sizes['chat_history']['formatted_size'] ); ?></div>
					<div class="card-meta"><?php echo esc_html( number_format( $record_counts['chat_history'] ) ); ?> <?php esc_html_e( 'records', 'askany' ); ?></div>
				</div>
			</div>

			<div class="askany-db-card">
				<div class="card-icon">⌨️</div>
				<div class="card-content">
					<h3><?php esc_html_e( 'Typing Indicators', 'askany' ); ?></h3>
					<div class="card-value"><?php echo esc_html( $table_sizes['typing_indicators']['formatted_size'] ); ?></div>
					<div class="card-meta"><?php echo esc_html( number_format( $record_counts['typing_indicators'] ) ); ?> <?php esc_html_e( 'records', 'askany' ); ?></div>
				</div>
			</div>

			<div class="askany-db-card">
				<div class="card-icon">❓</div>
				<div class="card-content">
					<h3><?php esc_html_e( 'Home FAQs', 'askany' ); ?></h3>
					<div class="card-value"><?php echo esc_html( $table_sizes['home_faqs']['formatted_size'] ); ?></div>
					<div class="card-meta"><?php echo esc_html( number_format( $record_counts['home_faqs'] ) ); ?> <?php esc_html_e( 'records', 'askany' ); ?></div>
				</div>
			</div>

			<div class="askany-db-card">
				<div class="card-icon">🔗</div>
				<div class="card-content">
					<h3><?php esc_html_e( 'Home Links', 'askany' ); ?></h3>
					<div class="card-value"><?php echo esc_html( $table_sizes['home_links']['formatted_size'] ); ?></div>
					<div class="card-meta"><?php echo esc_html( number_format( $record_counts['home_links'] ) ); ?> <?php esc_html_e( 'records', 'askany' ); ?></div>
				</div>
			</div>

			<div class="askany-db-card">
				<div class="card-icon">📊</div>
				<div class="card-content">
					<h3><?php esc_html_e( 'API Stats', 'askany' ); ?></h3>
					<div class="card-value"><?php echo esc_html( $table_sizes['api_stats']['formatted_size'] ); ?></div>
					<div class="card-meta"><?php echo esc_html( number_format( $record_counts['api_stats'] ) ); ?> <?php esc_html_e( 'records', 'askany' ); ?></div>
				</div>
			</div>

			<div class="askany-db-card">
				<div class="card-icon">💡</div>
				<div class="card-content">
					<h3><?php esc_html_e( 'Feature Requests', 'askany' ); ?></h3>
					<div class="card-value"><?php echo esc_html( $table_sizes['feature_requests']['formatted_size'] ); ?></div>
					<div class="card-meta"><?php echo esc_html( number_format( $record_counts['feature_requests'] ) ); ?> <?php esc_html_e( 'records', 'askany' ); ?></div>
				</div>
			</div>

			<div class="askany-db-card">
				<div class="card-icon">🐛</div>
				<div class="card-content">
					<h3><?php esc_html_e( 'Bug Reports', 'askany' ); ?></h3>
					<div class="card-value"><?php echo esc_html( $table_sizes['bug_reports']['formatted_size'] ); ?></div>
					<div class="card-meta"><?php echo esc_html( number_format( $record_counts['bug_reports'] ) ); ?> <?php esc_html_e( 'records', 'askany' ); ?></div>
				</div>
			</div>

			<div class="askany-db-card">
				<div class="card-icon">🧠</div>
				<div class="card-content">
					<h3><?php esc_html_e( 'RAG Embeddings', 'askany' ); ?></h3>
					<div class="card-value"><?php echo esc_html( $table_sizes['embeddings']['formatted_size'] ); ?></div>
					<div class="card-meta"><?php echo esc_html( number_format( $record_counts['embeddings'] ) ); ?> <?php esc_html_e( 'vectors', 'askany' ); ?></div>
				</div>
			</div>

			<div class="askany-db-card total">
				<div class="card-icon">💾</div>
				<div class="card-content">
					<h3><?php esc_html_e( 'Total Database', 'askany' ); ?></h3>
					<div class="card-value"><?php echo esc_html( $table_sizes['total']['formatted_size'] ); ?></div>
					<div class="card-meta"><?php esc_html_e( 'All AskAny tables', 'askany' ); ?></div>
				</div>
			</div>
		</div>
	</div>

	<!-- Quick Actions -->
	<div class="askany-db-section <?php echo ! $is_pro ? 'askany-pro-locked' : ''; ?>">
		<h2>
			<?php esc_html_e( 'Quick Actions', 'askany' ); ?>
			<?php if ( ! $is_pro ) : ?>
				<span class="askany-pro-tag" title="<?php esc_attr_e( 'This feature requires Pro version', 'askany' ); ?>">PRO</span>
			<?php endif; ?>
		</h2>
		<?php if ( ! $is_pro ) : ?>
			<div class="askany-pro-overlay">
				<div class="askany-pro-notice-small">
					<div class="askany-pro-notice-icon">
						<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
							<rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect>
							<path d="M7 11V7a5 5 0 0 1 10 0v4"></path>
						</svg>
					</div>
					<div class="askany-pro-notice-content">
						<h4><?php esc_html_e( 'Pro Feature', 'askany' ); ?></h4>
						<p><?php esc_html_e( 'Database quick actions are available in Pro version.', 'askany' ); ?></p>
						<button type="button" class="button button-primary askany-open-pro-modal">
							<?php esc_html_e( 'Upgrade to Pro', 'askany' ); ?>
						</button>
					</div>
				</div>
			</div>
		<?php endif; ?>
		<div class="askany-quick-actions <?php echo ! $is_pro ? 'disabled' : ''; ?>">
			<button type="button" class="button button-secondary" id="cleanup-10-days" <?php echo ! $is_pro ? 'disabled' : ''; ?>>
				🗑️ <?php esc_html_e( 'Clean Last 10 Days', 'askany' ); ?>
			</button>
			<button type="button" class="button button-secondary" id="cleanup-30-days" <?php echo ! $is_pro ? 'disabled' : ''; ?>>
				🗑️ <?php esc_html_e( 'Clean Last 30 Days', 'askany' ); ?>
			</button>
			<button type="button" class="button button-secondary" id="optimize-tables" <?php echo ! $is_pro ? 'disabled' : ''; ?>>
				⚡ <?php esc_html_e( 'Optimize Tables', 'askany' ); ?>
			</button>
			<button type="button" class="button button-secondary" id="export-all-data" <?php echo ! $is_pro ? 'disabled' : ''; ?>>
				📤 <?php esc_html_e( 'Export All Data', 'askany' ); ?>
			</button>
		</div>
	</div>

	<!-- Data Filters -->
	<div class="askany-db-section <?php echo ! $is_pro ? 'askany-pro-locked' : ''; ?>">
		<h2>
			<?php esc_html_e( 'Data Filters', 'askany' ); ?>
			<?php if ( ! $is_pro ) : ?>
				<span class="askany-pro-tag" title="<?php esc_attr_e( 'This feature requires Pro version', 'askany' ); ?>">PRO</span>
			<?php endif; ?>
		</h2>
		<?php if ( ! $is_pro ) : ?>
			<div class="askany-pro-overlay">
				<div class="askany-pro-notice-small">
					<div class="askany-pro-notice-icon">
						<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
							<rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect>
							<path d="M7 11V7a5 5 0 0 1 10 0v4"></path>
						</svg>
					</div>
					<div class="askany-pro-notice-content">
						<h4><?php esc_html_e( 'Pro Feature', 'askany' ); ?></h4>
						<p><?php esc_html_e( 'Advanced data filtering is available in Pro version.', 'askany' ); ?></p>
						<button type="button" class="button button-primary askany-open-pro-modal">
							<?php esc_html_e( 'Upgrade to Pro', 'askany' ); ?>
						</button>
					</div>
				</div>
			</div>
		<?php endif; ?>
		<div class="askany-filters <?php echo ! $is_pro ? 'disabled' : ''; ?>">
			<div class="filter-row">
				<label for="date-from"><?php esc_html_e( 'From Date:', 'askany' ); ?></label>
				<input type="date" id="date-from" name="date_from" value="<?php echo date( 'Y-m-d', strtotime( '-30 days' ) ); ?>" <?php echo ! $is_pro ? 'disabled' : ''; ?> />
				
				<label for="date-to"><?php esc_html_e( 'To Date:', 'askany' ); ?></label>
				<input type="date" id="date-to" name="date_to" value="<?php echo date( 'Y-m-d' ); ?>" <?php echo ! $is_pro ? 'disabled' : ''; ?> />
				
				<button type="button" class="button button-primary" id="apply-filters" <?php echo ! $is_pro ? 'disabled' : ''; ?>>
					<?php esc_html_e( 'Apply Filters', 'askany' ); ?>
				</button>
			</div>
			
			<div class="filter-row">
				<label for="session-type"><?php esc_html_e( 'Session Type:', 'askany' ); ?></label>
				<select id="session-type" name="session_type" <?php echo ! $is_pro ? 'disabled' : ''; ?>>
					<option value="all"><?php esc_html_e( 'All Sessions', 'askany' ); ?></option>
					<option value="anonymous"><?php esc_html_e( 'Anonymous Only', 'askany' ); ?></option>
					<option value="logged_in"><?php esc_html_e( 'Logged-in Users Only', 'askany' ); ?></option>
					<option value="agent_handled"><?php esc_html_e( 'Agent Handled', 'askany' ); ?></option>
					<option value="ai_only"><?php esc_html_e( 'AI Only', 'askany' ); ?></option>
				</select>
			</div>
		</div>
	</div>

	<!-- Operations -->
	<div class="askany-db-section <?php echo ! $is_pro ? 'askany-pro-locked' : ''; ?>">
		<h2>
			<?php esc_html_e( 'Database Operations', 'askany' ); ?>
			<?php if ( ! $is_pro ) : ?>
				<span class="askany-pro-tag" title="<?php esc_attr_e( 'This feature requires Pro version', 'askany' ); ?>">PRO</span>
			<?php endif; ?>
		</h2>
		<?php if ( ! $is_pro ) : ?>
			<div class="askany-pro-overlay">
				<div class="askany-pro-notice-small">
					<div class="askany-pro-notice-icon">
						<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
							<rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect>
							<path d="M7 11V7a5 5 0 0 1 10 0v4"></path>
						</svg>
					</div>
					<div class="askany-pro-notice-content">
						<h4><?php esc_html_e( 'Pro Feature', 'askany' ); ?></h4>
						<p><?php esc_html_e( 'Advanced database operations are available in Pro version.', 'askany' ); ?></p>
						<button type="button" class="button button-primary askany-open-pro-modal">
							<?php esc_html_e( 'Upgrade to Pro', 'askany' ); ?>
						</button>
					</div>
				</div>
			</div>
		<?php endif; ?>
		<div class="askany-operations <?php echo ! $is_pro ? 'disabled' : ''; ?>">
			<div class="operation-group">
				<h3>
					<?php esc_html_e( 'Export Operations', 'askany' ); ?>
					<?php if ( ! $is_pro ) : ?>
						<span class="askany-pro-tag-small" title="<?php esc_attr_e( 'Pro feature', 'askany' ); ?>">PRO</span>
					<?php endif; ?>
				</h3>
				<div class="operation-buttons">
					<button type="button" class="button button-secondary" id="export-filtered" <?php echo ! $is_pro ? 'disabled' : ''; ?>>
						📤 <?php esc_html_e( 'Export Filtered', 'askany' ); ?>
					</button>
					<button type="button" class="button button-secondary" id="export-csv" <?php echo ! $is_pro ? 'disabled' : ''; ?>>
						📊 <?php esc_html_e( 'Export CSV', 'askany' ); ?>
					</button>
					<button type="button" class="button button-secondary" id="export-json" <?php echo ! $is_pro ? 'disabled' : ''; ?>>
						📋 <?php esc_html_e( 'Export JSON', 'askany' ); ?>
					</button>
				</div>
			</div>

			<div class="operation-group">
				<h3>
					<?php esc_html_e( 'Import Operations', 'askany' ); ?>
					<?php if ( ! $is_pro ) : ?>
						<span class="askany-pro-tag-small" title="<?php esc_attr_e( 'Pro feature', 'askany' ); ?>">PRO</span>
					<?php endif; ?>
				</h3>
				<div class="operation-buttons">
					<input type="file" id="import-file" accept=".csv,.json,.sql" style="display: none;" <?php echo ! $is_pro ? 'disabled' : ''; ?> />
					<button type="button" class="button button-secondary" id="import-data" <?php echo ! $is_pro ? 'disabled' : ''; ?>>
						📥 <?php esc_html_e( 'Import Data', 'askany' ); ?>
					</button>
				</div>
				<div id="import-progress" style="display: none;">
					<div class="progress-bar">
						<div class="progress-fill"></div>
					</div>
					<div class="progress-text">0%</div>
				</div>
			</div>

			<div class="operation-group danger-zone">
				<h3>
					<?php esc_html_e( 'Danger Zone', 'askany' ); ?>
					<?php if ( ! $is_pro ) : ?>
						<span class="askany-pro-tag-small" title="<?php esc_attr_e( 'Pro feature', 'askany' ); ?>">PRO</span>
					<?php endif; ?>
				</h3>
				<div class="operation-buttons">
					<button type="button" class="button button-secondary" id="delete-filtered" <?php echo ! $is_pro ? 'disabled' : ''; ?>>
						🗑️ <?php esc_html_e( 'Delete Filtered', 'askany' ); ?>
					</button>
					<button type="button" class="button button-secondary button-danger" id="truncate-all" <?php echo ! $is_pro ? 'disabled' : ''; ?>>
						⚠️ <?php esc_html_e( 'Truncate All', 'askany' ); ?>
					</button>
				</div>
			</div>
		</div>
	</div>

	<!-- Results Area -->
	<div class="askany-db-section" id="results-section" style="display: none;">
		<h2><?php esc_html_e( 'Operation Results', 'askany' ); ?></h2>
		<div id="operation-results"></div>
	</div>
</div>

<!-- Confirmation Modal -->
<div id="askany-confirm-modal" class="askany-modal" style="display: none;">
	<div class="modal-content">
		<div class="modal-header">
			<h3 id="modal-title"><?php esc_html_e( 'Confirm Operation', 'askany' ); ?></h3>
			<span class="modal-close">&times;</span>
		</div>
		<div class="modal-body">
			<div class="warning-icon">⚠️</div>
			<div id="modal-message"></div>
			<div class="confirmation-input" style="display: none;">
				<label for="confirm-text"><?php esc_html_e( 'Type "CONFIRM" to proceed:', 'askany' ); ?></label>
				<input type="text" id="confirm-text" placeholder="CONFIRM" />
			</div>
		</div>
		<div class="modal-footer">
			<button type="button" class="button button-secondary" id="modal-cancel">
				<?php esc_html_e( 'Cancel', 'askany' ); ?>
			</button>
			<button type="button" class="button button-primary" id="modal-confirm" disabled>
				<?php esc_html_e( 'Proceed', 'askany' ); ?>
			</button>
		</div>
	</div>
</div>

<!-- Styles and scripts are enqueued above -->

<!-- JavaScript functionality is handled by database-management.js -->