<?php

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

if ( ! isset( $module_id ) || empty( $module_id ) ) {
	$module_id = 'robots-txt-manager';
}

$mod         = null;
$path        = '';
$writable    = false;
$sitemap     = home_url( '/wp-sitemap.xml' );
try {
	$mod      = new \RobotsTxtManager\Module();
	$path     = $mod ? $mod->getPath() : '';
	$writable = $mod ? $mod->isWritable() : false;
	$sitemap  = $mod ? $mod->getSitemapUrl() : $sitemap;
} catch ( \Throwable $e ) {
	$mod = null;
}
?>

<div class="wp-powersuite-module-settings">
	<p class="description">
		<?php esc_html_e( 'Customize your site\'s robots.txt file directly from the WordPress admin panel. Control which parts of your site search engines can crawl, add sitemap references, and manage crawler behavior—all without FTP access or file editing.', 'wp-powersuite' ); ?>
	</p>

	<form class="wp-powersuite-module-settings-form" data-module-id="<?php echo esc_attr( $module_id ); ?>" data-ajax-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_powersuite_ajax' ) ); ?>">
		<?php wp_nonce_field( 'wp_powersuite_save_module_settings_' . $module_id, 'wp_powersuite_nonce' ); ?>
		<div class="wpps-robots-txt" data-module-id="<?php echo esc_attr( $module_id ); ?>" data-sitemap-url="<?php echo esc_attr( $sitemap ); ?>">

			<p class="description" style="margin-bottom: 12px;">
				<strong><?php esc_html_e( 'File:', 'wp-powersuite' ); ?></strong>
				<code><?php echo esc_html( $path ); ?></code>
				<?php if ( ! $writable ) : ?>
					<span style="color: #646970;"> — <?php esc_html_e( 'Not writable. Content will be served via WordPress filter (no file required).', 'wp-powersuite' ); ?></span>
				<?php endif; ?>
			</p>

			<p class="wpps-robots-actions">
				<button type="button" class="button wpps-robots-load"><?php esc_html_e( 'Load', 'wp-powersuite' ); ?></button>
				<button type="button" class="button wpps-robots-add-sitemap">
					<?php esc_html_e( 'Add sitemap', 'wp-powersuite' ); ?>
				</button>
				<button type="button" class="button wpps-robots-backup">
					<?php esc_html_e( 'Create Backup', 'wp-powersuite' ); ?>
				</button>
			</p>

			<textarea
				id="wpps-robots-content"
				class="large-text code wpps-robots-code-editor"
				rows="18"
				style="width: 100%; font-family: Consolas, Monaco, monospace; font-size: 13px;"
				placeholder="<?php esc_attr_e( 'Click Load to fetch robots.txt, or add rules and Save to create it.', 'wp-powersuite' ); ?>"
			></textarea>

			<p class="description" style="margin-top: 12px;">
				<?php esc_html_e( 'Use User-agent, Disallow, Allow, and Sitemap. Use the Create Backup button to create a backup before saving.', 'wp-powersuite' ); ?>
			</p>
		</div>
	</form>
</div>
