<?php
/**
 * Livada SEO — Titles & Metas Settings View
 *
 * @package Livada_SEO
 */

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

// Collect registered public post types.
$post_types = get_post_types( [ 'public' => true ], 'objects' );

// Collect registered public taxonomies.
$taxonomies = get_taxonomies( [ 'public' => true ], 'objects' );

$variables_hint = '<code>%title%</code>, <code>%sitename%</code>, <code>%sep%</code>, <code>%tagline%</code>, <code>%category%</code>, <code>%tag%</code>, <code>%term%</code>, <code>%author%</code>, <code>%year%</code>, <code>%month%</code>';
?>
<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( 'Titles &amp; Metas', '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; ?>

	<p class="description">
		<?php esc_html_e( 'Available variables:', 'livada-seo' ); ?>
		<?php echo wp_kses_post( $variables_hint ); ?>
	</p>

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

		<!-- ── Homepage ── -->
		<h2><?php esc_html_e( 'Homepage', 'livada-seo' ); ?></h2>
		<table class="form-table" role="presentation">
			<tbody>
				<tr>
					<th scope="row">
						<label for="livada_title_home"><?php esc_html_e( 'Home Title', 'livada-seo' ); ?></label>
					</th>
					<td>
						<input
							type="text"
							id="livada_title_home"
							name="livada_seo[title_home]"
							value="<?php echo esc_attr( Livada_SEO_Loader::get_setting( 'title_home', '%sitename% %sep% %tagline%' ) ); ?>"
							class="large-text"
						>
					</td>
				</tr>
				<tr>
					<th scope="row">
						<label for="livada_desc_home"><?php esc_html_e( 'Home Description', 'livada-seo' ); ?></label>
					</th>
					<td>
						<textarea
							id="livada_desc_home"
							name="livada_seo[desc_home]"
							class="large-text"
							rows="3"
						><?php echo esc_textarea( Livada_SEO_Loader::get_setting( 'desc_home', '' ) ); ?></textarea>
						<p class="description"><?php esc_html_e( 'Default meta description for the homepage. Aim for 120–160 characters.', 'livada-seo' ); ?></p>
					</td>
				</tr>
			</tbody>
		</table>

		<!-- ── Post Types ── -->
		<h2><?php esc_html_e( 'Post Types', 'livada-seo' ); ?></h2>
		<table class="form-table" role="presentation">
			<tbody>
				<?php foreach ( $post_types as $pt ) :
					$key     = 'title_cpt_' . $pt->name;
					// Built-in keys.
					if ( $pt->name === 'post' ) {
						$key = 'title_post';
					} elseif ( $pt->name === 'page' ) {
						$key = 'title_page';
					}
					$default = '%title% %sep% %sitename%';
					$value   = Livada_SEO_Loader::get_setting( $key, $default );
				?>
					<tr>
						<th scope="row">
							<label for="livada_<?php echo esc_attr( $key ); ?>">
								<?php echo esc_html( $pt->labels->singular_name ); ?>
								<span class="description">(<?php echo esc_html( $pt->name ); ?>)</span>
							</label>
						</th>
						<td>
							<input
								type="text"
								id="livada_<?php echo esc_attr( $key ); ?>"
								name="livada_seo[<?php echo esc_attr( $key ); ?>]"
								value="<?php echo esc_attr( $value ); ?>"
								class="large-text"
							>
						</td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>

		<!-- ── Taxonomies ── -->
		<h2><?php esc_html_e( 'Taxonomies', 'livada-seo' ); ?></h2>
		<table class="form-table" role="presentation">
			<tbody>
				<?php foreach ( $taxonomies as $tax ) :
					$key   = 'title_tax_' . $tax->name;
					// Built-in keys.
					if ( $tax->name === 'category' ) {
						$key = 'title_category';
					} elseif ( $tax->name === 'post_tag' ) {
						$key = 'title_tag';
					}
					$default = '%term% %sep% %sitename%';
					$value   = Livada_SEO_Loader::get_setting( $key, $default );
				?>
					<tr>
						<th scope="row">
							<label for="livada_<?php echo esc_attr( $key ); ?>">
								<?php echo esc_html( $tax->labels->singular_name ); ?>
								<span class="description">(<?php echo esc_html( $tax->name ); ?>)</span>
							</label>
						</th>
						<td>
							<input
								type="text"
								id="livada_<?php echo esc_attr( $key ); ?>"
								name="livada_seo[<?php echo esc_attr( $key ); ?>]"
								value="<?php echo esc_attr( $value ); ?>"
								class="large-text"
							>
						</td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>

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