<?php
/**
 * Knowledge base ingest view.
 *
 * @package AetherisAIChat
 *
 * @var array $settings
 * @var bool  $has_auth
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
?>
<div class="wrap aetheris-wrap">
	<?php Aetheris_AI_Chat_Admin::render_header( __( 'Knowledge base', 'aetheris-ai-chat' ) ); ?>
	<p class="description">
		<?php esc_html_e( 'Train your chatbot on your site, blog, or documents. Aetheris chunks and embeds the content so the bot can cite it in answers.', 'aetheris-ai-chat' ); ?>
	</p>

	<?php Aetheris_AI_Chat_Admin::flash_notice(); ?>

	<?php if ( ! $has_auth ) : ?>
		<div class="notice notice-warning">
			<p>
				<?php
				printf(
					/* translators: %s settings page URL */
					esc_html__( 'Add your API key in %s to ingest knowledge base content from here.', 'aetheris-ai-chat' ),
					'<a href="' . esc_url( admin_url( 'admin.php?page=' . Aetheris_AI_Chat_Admin::SLUG ) ) . '">'
					. esc_html__( 'Settings', 'aetheris-ai-chat' ) . '</a>'
				);
				?>
			</p>
		</div>
	<?php endif; ?>

	<div class="aetheris-grid">
		<div class="aetheris-card">
			<h2><?php esc_html_e( 'Ingest a URL', 'aetheris-ai-chat' ); ?></h2>
			<p class="description">
				<?php esc_html_e( 'Paste a public URL (your homepage, a docs page, a blog post). Aetheris fetches and indexes the page content.', 'aetheris-ai-chat' ); ?>
			</p>
			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="aetheris-form">
				<?php wp_nonce_field( 'aetheris_ingest_url' ); ?>
				<input type="hidden" name="action" value="aetheris_ingest_url" />
				<table class="form-table" role="presentation">
					<tr>
						<th scope="row">
							<label for="kb_url"><?php esc_html_e( 'URL', 'aetheris-ai-chat' ); ?></label>
						</th>
						<td>
							<input type="url"
								id="kb_url"
								name="kb_url"
								class="regular-text code"
								placeholder="https://example.com/about"
								required
								<?php disabled( ! $has_auth ); ?> />
						</td>
					</tr>
				</table>
				<?php submit_button( __( 'Ingest URL', 'aetheris-ai-chat' ), 'primary', '', true, $has_auth ? array() : array( 'disabled' => 'disabled' ) ); ?>
			</form>
		</div>

		<div class="aetheris-card">
			<h2><?php esc_html_e( 'Upload a document', 'aetheris-ai-chat' ); ?></h2>
			<p class="description">
				<?php esc_html_e( 'PDF, TXT, MD, HTML, or DOCX. Up to 20 MB.', 'aetheris-ai-chat' ); ?>
			</p>
			<form method="post"
				action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>"
				enctype="multipart/form-data"
				class="aetheris-form">
				<?php wp_nonce_field( 'aetheris_ingest_file' ); ?>
				<input type="hidden" name="action" value="aetheris_ingest_file" />
				<table class="form-table" role="presentation">
					<tr>
						<th scope="row">
							<label for="kb_title"><?php esc_html_e( 'Title (optional)', 'aetheris-ai-chat' ); ?></label>
						</th>
						<td>
							<input type="text"
								id="kb_title"
								name="kb_title"
								class="regular-text"
								<?php disabled( ! $has_auth ); ?> />
						</td>
					</tr>
					<tr>
						<th scope="row">
							<label for="kb_file"><?php esc_html_e( 'File', 'aetheris-ai-chat' ); ?></label>
						</th>
						<td>
							<input type="file"
								id="kb_file"
								name="kb_file"
								accept=".pdf,.txt,.md,.html,.htm,.docx"
								required
								<?php disabled( ! $has_auth ); ?> />
						</td>
					</tr>
				</table>
				<?php submit_button( __( 'Upload + ingest', 'aetheris-ai-chat' ), 'primary', '', true, $has_auth ? array() : array( 'disabled' => 'disabled' ) ); ?>
			</form>
		</div>
	</div>

	<div class="aetheris-card aetheris-tip">
		<h3><?php esc_html_e( 'Tip — auto-train on your site', 'aetheris-ai-chat' ); ?></h3>
		<p>
			<?php esc_html_e( 'Ingest your homepage first, then your top 3–5 most-visited pages (Pricing, About, FAQ). The bot answers best when it has 5–20 well-chosen sources rather than the entire site.', 'aetheris-ai-chat' ); ?>
		</p>
	</div>
</div>
