<?php
/**
 * Livada SEO — General Settings View
 *
 * @package Livada_SEO
 */

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

$site_name = Livada_SEO_Loader::get_setting( 'site_name', '' );
$separator = Livada_SEO_Loader::get_setting( 'separator', '|' );
$modules   = (array) Livada_SEO_Loader::get_setting( 'modules', [] );

// AEO — Knowledge Graph configuration. Read once for the form below.
$aeo_org_description   = (string) Livada_SEO_Loader::get_setting( 'aeo_org_description', '' );
$aeo_org_founding_date = (string) Livada_SEO_Loader::get_setting( 'aeo_org_founding_date', '' );
$aeo_org_founder       = (string) Livada_SEO_Loader::get_setting( 'aeo_org_founder', '' );
$aeo_org_knows_about   = (array)  Livada_SEO_Loader::get_setting( 'aeo_org_knows_about', [] );
$aeo_knows_about_text  = implode( "\n", array_filter( $aeo_org_knows_about, 'strlen' ) );
$aeo_wikidata_id       = (string) Livada_SEO_Loader::get_setting( 'aeo_wikidata_id', '' );

// True when an API key is currently stored (we never echo the value back).
$has_anthropic_key = '' !== ( class_exists( 'Livada_SEO_Image_Bulk' )
	? Livada_SEO_Image_Bulk::get_anthropic_key()
	: '' );

$all_modules = [
	'meta'         => __( 'Meta Tags (title, description, robots)', 'livada-seo' ),
	'sitemap'      => __( 'XML Sitemap', 'livada-seo' ),
	'sitemap_html' => __( 'HTML Sitemap', 'livada-seo' ),
	'schema'       => __( 'Schema / Structured Data', 'livada-seo' ),
	'opengraph'    => __( 'Open Graph & Social Meta Tags', 'livada-seo' ),
	'redirects'    => __( 'URL Redirections', 'livada-seo' ),
	'breadcrumbs'  => __( 'Breadcrumbs', 'livada-seo' ),
	'analysis'     => __( 'On-Page SEO Analysis', 'livada-seo' ),
	'robots'       => __( 'Robots.txt Manager', 'livada-seo' ),
	'indexnow'     => __( 'IndexNow Pings', 'livada-seo' ),
	'toc'          => __( 'Table of Contents', 'livada-seo' ),
	'image_seo'    => __( 'Image SEO (alt, lazy, sitemap)', 'livada-seo' ),
	'analytics'    => __( 'Analytics injection (GA4, Matomo, Clarity)', 'livada-seo' ),
	'geo'          => __( 'GEO (author expertise, speakable)', 'livada-seo' ),
	'aeo'          => __( 'AEO (llms.txt, AI markers)', 'livada-seo' ),
	'site_audit'   => __( 'Site Audit (daily crawl + scoring)', 'livada-seo' ),
	'woocommerce'  => __( 'WooCommerce schemas (Product, Offer, AggregateRating, Review)', 'livada-seo' ),
	'og_image'     => __( 'OG image auto-generator (programmatic 1200×630 PNG)', 'livada-seo' ),
];
?>
<div class="wrap livada-seo-wrap has-ls-hero">
<?php include LIVADA_SEO_PATH . 'includes/admin/views/_partials/header.php'; ?>
	<h1><?php esc_html_e( 'General Settings', 'livada-seo' ); ?></h1>

	<?php if ( isset( $_GET['updated'] ) ) : // phpcs:ignore ?>
		<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Settings saved.', 'livada-seo' ); ?></p></div>
	<?php endif; ?>

	<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
		<?php wp_nonce_field( 'livada_seo_save_general' ); ?>
		<input type="hidden" name="action" value="livada_seo_save_general">

		<table class="form-table" role="presentation">
			<tbody>

				<tr>
					<th scope="row">
						<label for="livada_site_name"><?php esc_html_e( 'Site Name', 'livada-seo' ); ?></label>
					</th>
					<td>
						<input
							type="text"
							id="livada_site_name"
							name="livada_seo[site_name]"
							value="<?php echo esc_attr( $site_name ); ?>"
							class="regular-text"
							placeholder="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>"
						>
						<p class="description"><?php esc_html_e( 'Override the site name used in title templates. Leave blank to use the WordPress site title.', 'livada-seo' ); ?></p>
					</td>
				</tr>

				<tr>
					<th scope="row">
						<label for="livada_separator"><?php esc_html_e( 'Title Separator', 'livada-seo' ); ?></label>
					</th>
					<td>
						<input
							type="text"
							id="livada_separator"
							name="livada_seo[separator]"
							value="<?php echo esc_attr( $separator ); ?>"
							class="small-text"
							maxlength="5"
						>
						<p class="description"><?php esc_html_e( 'Character used to separate title segments (e.g. | – •).', 'livada-seo' ); ?></p>
					</td>
				</tr>

				<tr>
					<th scope="row">
						<label for="livada_anthropic_api_key"><?php esc_html_e( 'Anthropic API Key', 'livada-seo' ); ?></label>
					</th>
					<td>
						<input
							type="password"
							id="livada_anthropic_api_key"
							name="livada_seo[anthropic_api_key]"
							value=""
							class="regular-text"
							autocomplete="off"
							placeholder="<?php echo $has_anthropic_key ? esc_attr__( '••••••••  (key stored — leave blank to keep)', 'livada-seo' ) : esc_attr__( 'sk-ant-…', 'livada-seo' ); ?>"
						>
						<?php if ( $has_anthropic_key ) : ?>
							<label style="margin-left:12px;">
								<input type="checkbox" name="livada_seo[anthropic_api_key_remove]" value="1">
								<?php esc_html_e( 'Remove stored key', 'livada-seo' ); ?>
							</label>
						<?php endif; ?>
						<p class="description">
							<?php esc_html_e( 'Powers AI alt-text generation in the Image SEO bulk editor. The key is encrypted at rest with a key derived from your wp-config secrets.', 'livada-seo' ); ?>
						</p>
					</td>
				</tr>

				<tr>
					<th scope="row"><?php esc_html_e( 'Active Modules', 'livada-seo' ); ?></th>
					<td>
						<fieldset>
							<legend class="screen-reader-text"><?php esc_html_e( 'Enable or disable each module.', 'livada-seo' ); ?></legend>
							<?php foreach ( $all_modules as $key => $label ) : ?>
								<label class="livada-module-toggle">
									<input
										type="checkbox"
										name="livada_seo[modules][]"
										value="<?php echo esc_attr( $key ); ?>"
										<?php checked( in_array( $key, $modules, true ) ); ?>
									>
									<?php echo esc_html( $label ); ?>
								</label><br>
							<?php endforeach; ?>
						</fieldset>
					</td>
				</tr>

			</tbody>
		</table>

		<h2 style="margin-top:2em;"><?php esc_html_e( 'AEO — Knowledge Graph', 'livada-seo' ); ?></h2>
		<p class="description" style="max-width:760px;">
			<?php esc_html_e( 'These fields feed both the Organization JSON-LD entity emitted on the front page and the /llms.txt file that AI engines (ChatGPT, Gemini, Perplexity, Claude) consume to understand your business. The schema.org type used for the Knowledge Graph node follows the Schema → Business Type setting (Hotel, Campground, Restaurant, etc.).', 'livada-seo' ); ?>
		</p>

		<table class="form-table" role="presentation">
			<tbody>

				<tr>
					<th scope="row">
						<label for="livada_aeo_org_description"><?php esc_html_e( 'Organization description', 'livada-seo' ); ?></label>
					</th>
					<td>
						<textarea
							id="livada_aeo_org_description"
							name="livada_seo[aeo_org_description]"
							class="large-text"
							rows="4"
							placeholder="<?php esc_attr_e( 'A short paragraph describing what your business does, for whom, and what makes it distinctive.', 'livada-seo' ); ?>"
						><?php echo esc_textarea( $aeo_org_description ); ?></textarea>
						<p class="description">
							<?php esc_html_e( 'Used as the description in the Organization JSON-LD and as the > tagline at the top of /llms.txt.', 'livada-seo' ); ?>
						</p>
					</td>
				</tr>

				<tr>
					<th scope="row">
						<label for="livada_aeo_org_founding_date"><?php esc_html_e( 'Founding year', 'livada-seo' ); ?></label>
					</th>
					<td>
						<input
							type="text"
							id="livada_aeo_org_founding_date"
							name="livada_seo[aeo_org_founding_date]"
							value="<?php echo esc_attr( $aeo_org_founding_date ); ?>"
							class="small-text"
							maxlength="4"
							inputmode="numeric"
							pattern="[0-9]{4}"
							placeholder="<?php esc_attr_e( 'ex: 1985', 'livada-seo' ); ?>"
						>
						<p class="description">
							<?php esc_html_e( 'Year the business was founded. Emitted as foundingDate.', 'livada-seo' ); ?>
						</p>
					</td>
				</tr>

				<tr>
					<th scope="row">
						<label for="livada_aeo_org_founder"><?php esc_html_e( 'Founder name', 'livada-seo' ); ?></label>
					</th>
					<td>
						<input
							type="text"
							id="livada_aeo_org_founder"
							name="livada_seo[aeo_org_founder]"
							value="<?php echo esc_attr( $aeo_org_founder ); ?>"
							class="regular-text"
							placeholder="<?php esc_attr_e( 'ex: Jean Dupont', 'livada-seo' ); ?>"
						>
						<p class="description">
							<?php esc_html_e( 'Full name of the founder. Emitted as a Person node under founder.', 'livada-seo' ); ?>
						</p>
					</td>
				</tr>

				<tr>
					<th scope="row">
						<label for="livada_aeo_org_knows_about"><?php esc_html_e( 'Areas of expertise', 'livada-seo' ); ?></label>
					</th>
					<td>
						<textarea
							id="livada_aeo_org_knows_about"
							name="livada_seo[aeo_org_knows_about]"
							class="large-text"
							rows="5"
							placeholder="<?php esc_attr_e( "One topic per line. e.g.\nHôtellerie de plein air\nCamping familial\nGorges du Verdon\nRandonnée pédestre", 'livada-seo' ); ?>"
						><?php echo esc_textarea( $aeo_knows_about_text ); ?></textarea>
						<p class="description">
							<?php esc_html_e( 'One subject per line. Emitted as the knowsAbout array in the Knowledge Graph and used to build the "Content" section of /llms.txt instead of the WordPress categories. Strong signal for AI engines about your domain expertise.', 'livada-seo' ); ?>
						</p>
					</td>
				</tr>

				<tr>
					<th scope="row">
						<label for="livada_aeo_wikidata_id"><?php esc_html_e( 'Wikidata entity ID', 'livada-seo' ); ?></label>
					</th>
					<td>
						<input
							type="text"
							id="livada_aeo_wikidata_id"
							name="livada_seo[aeo_wikidata_id]"
							value="<?php echo esc_attr( $aeo_wikidata_id ); ?>"
							class="regular-text"
							placeholder="Q140007358"
							pattern="^Q\d+$"
							autocomplete="off"
							data-lpignore="true"
							data-1p-ignore="true"
							spellcheck="false"
						/>
						<?php if ( '' !== $aeo_wikidata_id && preg_match( '/^Q\d+$/', $aeo_wikidata_id ) ) : ?>
							<p class="description">
								<?php
								printf(
									/* translators: %s: link to the Wikidata page */
									esc_html__( 'Linked to %s — this URL is automatically added to your schema sameAs so AI engines can anchor your business to its Wikidata record.', 'livada-seo' ),
									'<a href="' . esc_url( 'https://www.wikidata.org/wiki/' . $aeo_wikidata_id ) . '" target="_blank" rel="noopener noreferrer">' . esc_html( $aeo_wikidata_id ) . '</a>'
								);
								?>
							</p>
						<?php else : ?>
							<p class="description">
								<?php esc_html_e( 'Format: Q followed by digits (e.g. Q140007358). The corresponding Wikidata URL is auto-injected into the schema sameAs of your primary business entity — anchoring your site in the structured knowledge base that ChatGPT, Perplexity, Gemini and Google AI Overviews consult to identify and cite real-world entities. Leave empty if you have no Wikidata page yet.', 'livada-seo' ); ?>
							</p>
						<?php endif; ?>
					</td>
				</tr>

			</tbody>
		</table>

		<?php submit_button( __( 'Save Settings', 'livada-seo' ) ); ?>
	</form>
</div>
