<?php

use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

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

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

$module_class = 'AdsTxtManager\\Module';
$module_instance = new $module_class();

// Only read file if module is enabled (admin.php is only loaded for enabled modules, but extra safety check)
if ( $module_instance->isEnabled() ) {
	$file_exists  = $module_instance->fileExists();
	$is_writable  = $module_instance->isWritable();
	$file_path    = $module_instance->getAdsTxtPath();
	$file_content = $module_instance->getAdsTxtContent();
	$stored       = OptionStoreModuleSettingsRepository::forModule( $module_id )->all( [] );
	// Prefer on-disk file when it exists; fall back to saved settings for virtual serving.
	if ( ! $file_exists && isset( $stored['content'] ) ) {
		$file_content = (string) $stored['content'];
	}
} else {
	$file_exists  = false;
	$is_writable  = false;
	$file_content = '';
	$file_path    = ABSPATH . 'ads.txt';
}
?>

<div class="wp-powersuite-module-settings">
	<p class="description">
		<?php esc_html_e( 'Manage your site\'s ads.txt file directly from WordPress. The ads.txt file tells advertising networks which companies are authorized to sell ad space on your site.', 'wp-powersuite' ); ?>
	</p>

	<?php if ( ! $is_writable ) : ?>
		<div class="notice notice-warning inline" style="margin: 15px 0;">
			<p>
				<strong><?php esc_html_e( 'Warning:', 'wp-powersuite' ); ?></strong>
				<?php esc_html_e( 'The ads.txt file cannot be written. Your content will be saved and served virtually when the file does not exist. To enable physical file serving, check that the root directory is writable or create the file manually.', 'wp-powersuite' ); ?>
			</p>
		</div>
	<?php endif; ?>

	<?php if ( $file_exists ) : ?>
		<div class="notice notice-info inline" style="margin: 15px 0;">
			<p>
				<strong><?php esc_html_e( 'File Location:', 'wp-powersuite' ); ?></strong>
				<code><?php echo esc_html( $file_path ); ?></code>
			</p>
		</div>
	<?php else : ?>
		<div class="notice notice-warning inline" style="margin: 15px 0;">
			<p>
				<strong><?php esc_html_e( 'File Not Found:', 'wp-powersuite' ); ?></strong>
				<?php esc_html_e( 'The ads.txt file does not exist yet. It will be created when you save your first entry.', 'wp-powersuite' ); ?>
			</p>
		</div>
	<?php endif; ?>

	<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-ads-txt" data-module-id="<?php echo esc_attr( $module_id ); ?>">
			<p class="wpps-ads-actions">
				<button type="button" class="button wpps-ads-backup" <?php disabled( ! $is_writable ); ?>>
					<?php esc_html_e( 'Create Backup', 'wp-powersuite' ); ?>
				</button>
			</p>

			<table class="form-table" role="presentation">
				<tbody>
					<tr>
						<th scope="row">
							<label for="<?php echo esc_attr( $module_id ); ?>_content"><?php esc_html_e( 'Ads.txt Content', 'wp-powersuite' ); ?></label>
						</th>
						<td>
							<textarea
								id="<?php echo esc_attr( $module_id ); ?>_content"
								name="content"
								class="large-text code wp-powersuite-advanced-setting wpps-ads-code-editor"
								data-setting-key="content"
								rows="15"
								style="font-family: monospace; font-size: 13px; line-height: 1.6; width: 100%;"
								placeholder="<?php esc_attr_e( 'google.com, pub-0000000000000000, DIRECT, f08c47fec0942fa0', 'wp-powersuite' ); ?>"
							><?php echo esc_textarea( $file_content ); ?></textarea>
							<p class="description">
								<?php esc_html_e( 'Enter your ads.txt content. Each line should follow the format: domain, publisher_id, relationship, certification_authority_id', 'wp-powersuite' ); ?>
								<br>
								<a href="https://iabtechlab.com/ads-txt/" target="_blank" rel="noopener noreferrer">
									<?php esc_html_e( 'Learn more about ads.txt format', 'wp-powersuite' ); ?>
								</a>
							</p>
						</td>
					</tr>
				</tbody>
			</table>
		</div>

		<div class="wp-powersuite-info-box" style="background: #f0f6fc; border-left: 4px solid #2271b1; padding: 12px; margin: 20px 0;">
			<p style="margin: 0;">
				<strong><?php esc_html_e( 'How It Works:', 'wp-powersuite' ); ?></strong><br>
				<?php esc_html_e( 'The ads.txt file is placed in your site\'s root directory and tells advertising networks which companies are authorized to sell ad space on your site. This helps prevent unauthorized inventory sales and ensures you receive proper revenue.', 'wp-powersuite' ); ?>
			</p>
		</div>
	</form>
</div>
