<?php
/**
 * SEPA file check screen — paste or upload any pain.008 file and learn,
 * before the bank does, whether it will be accepted and whether it survives
 * 15 November 2026.
 */

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

class BSTEW_Admin_Validator {

	public static function init() {
		add_action( 'admin_post_bstew_validate', array( __CLASS__, 'handle' ) );
	}

	public static function handle() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( 'forbidden' );
		}
		check_admin_referer( 'bstew_validate' );

		$xml = '';
		if ( ! empty( $_FILES['bstew_file']['tmp_name'] ) ) {
			$xml = file_get_contents( sanitize_text_field( $_FILES['bstew_file']['tmp_name'] ) ); // phpcs:ignore
		} elseif ( ! empty( $_POST['bstew_xml'] ) ) {
			$xml = wp_unslash( $_POST['bstew_xml'] ); // phpcs:ignore -- raw XML, parsed not stored.
		}

		$validator = new BSTEW_Validator();
		$result    = $validator->validate( (string) $xml );
		set_transient( 'bstew_validate_result_' . get_current_user_id(), $result, 300 );

		wp_safe_redirect( add_query_arg( array( 'page' => 'beitrag-steward-validator', 'bstew_checked' => 1 ), admin_url( 'admin.php' ) ) );
		exit;
	}

	public static function render() {
		$result = null;
		if ( isset( $_GET['bstew_checked'] ) ) { // phpcs:ignore
			$result = get_transient( 'bstew_validate_result_' . get_current_user_id() );
		}
		?>
		<div class="wrap">
			<h1><?php esc_html_e( 'SEPA file check', 'beitrag-steward' ); ?></h1>
			<p style="max-width:46em">
				<?php esc_html_e( 'Check any SEPA direct-debit file (pain.008) before uploading it to your bank — including files from other software or old Excel converters. From 15 November 2026 German banks stop accepting the old format versions and require structured addresses; this check tells you where your file stands.', 'beitrag-steward' ); ?>
			</p>

			<?php if ( is_array( $result ) ) : ?>
				<?php if ( $result['ok'] ) : ?>
					<div class="notice notice-success"><p><strong><?php esc_html_e( 'No rejection-level problems found.', 'beitrag-steward' ); ?></strong></p></div>
				<?php else : ?>
					<div class="notice notice-error"><p><strong><?php esc_html_e( 'This file has problems your bank will reject it for.', 'beitrag-steward' ); ?></strong></p></div>
				<?php endif; ?>
				<table class="widefat striped" style="max-width:1000px;margin-bottom:24px">
					<thead><tr><th style="width:9em"><?php esc_html_e( 'Severity', 'beitrag-steward' ); ?></th><th><?php esc_html_e( 'Finding', 'beitrag-steward' ); ?></th></tr></thead>
					<tbody>
					<?php foreach ( $result['findings'] as $f ) : ?>
						<tr>
							<td><?php
								if ( 'reject' === $f['severity'] ) {
									echo '<span style="color:#b32d2e;font-weight:600">' . esc_html__( 'Rejection', 'beitrag-steward' ) . '</span>';
								} elseif ( 'deadline' === $f['severity'] ) {
									echo '<span style="color:#996800;font-weight:600">' . esc_html__( '15 Nov 2026', 'beitrag-steward' ) . '</span>';
								} elseif ( 'warn' === $f['severity'] ) {
									echo '<span style="color:#996800">' . esc_html__( 'Warning', 'beitrag-steward' ) . '</span>';
								} else {
									echo '<span style="color:#1a7a33">' . esc_html__( 'Info', 'beitrag-steward' ) . '</span>';
								}
							?></td>
							<td><?php echo esc_html( $f['message'] ); ?></td>
						</tr>
					<?php endforeach; ?>
					</tbody>
				</table>
			<?php endif; ?>

			<form method="post" enctype="multipart/form-data" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="max-width:760px">
				<?php wp_nonce_field( 'bstew_validate' ); ?>
				<input type="hidden" name="action" value="bstew_validate">
				<table class="form-table" role="presentation">
					<tr><th><?php esc_html_e( 'Upload file', 'beitrag-steward' ); ?></th>
						<td><input type="file" name="bstew_file" accept=".xml,text/xml"></td></tr>
					<tr><th><?php esc_html_e( '… or paste XML', 'beitrag-steward' ); ?></th>
						<td><textarea name="bstew_xml" rows="8" style="width:100%;font-family:monospace"></textarea></td></tr>
				</table>
				<?php submit_button( __( 'Check file', 'beitrag-steward' ) ); ?>
			</form>
			<p class="description"><?php esc_html_e( 'The file is checked on this server and is not stored or transmitted anywhere.', 'beitrag-steward' ); ?></p>
		</div>
		<?php
	}
}

BSTEW_Admin_Validator::init();
