<?php
/**
 * Settings screen: the Verein's own data — everything the SEPA file and the
 * PDF letterhead need. Registers the top-level menu the other screens hang off.
 */

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

class BSTEW_Settings {

	public static function init() {
		add_action( 'admin_menu', array( __CLASS__, 'menu' ) );
		add_action( 'admin_init', array( __CLASS__, 'register' ) );
	}

	public static function menu() {
		add_menu_page(
			__( 'Beitrag Steward', 'beitrag-steward' ),
			__( 'Beiträge', 'beitrag-steward' ),
			'manage_options',
			'beitrag-steward',
			array( 'BSTEW_Admin_Members', 'render' ),
			'dashicons-groups'
		);
		add_submenu_page(
			'beitrag-steward',
			__( 'Members', 'beitrag-steward' ),
			__( 'Members', 'beitrag-steward' ),
			'manage_options',
			'beitrag-steward',
			array( 'BSTEW_Admin_Members', 'render' )
		);
		add_submenu_page(
			'beitrag-steward',
			__( 'Collection run', 'beitrag-steward' ),
			__( 'Collection run', 'beitrag-steward' ),
			'manage_options',
			'beitrag-steward-run',
			array( 'BSTEW_Admin_Run', 'render' )
		);
		add_submenu_page(
			'beitrag-steward',
			__( 'SEPA file check', 'beitrag-steward' ),
			__( 'SEPA file check', 'beitrag-steward' ),
			'manage_options',
			'beitrag-steward-validator',
			array( 'BSTEW_Admin_Validator', 'render' )
		);
		add_submenu_page(
			'beitrag-steward',
			__( 'Settings', 'beitrag-steward' ),
			__( 'Settings', 'beitrag-steward' ),
			'manage_options',
			'beitrag-steward-settings',
			array( __CLASS__, 'render' )
		);
	}

	public static function register() {
		register_setting( 'bstew_options_group', 'bstew_options', array( __CLASS__, 'sanitize' ) );
	}

	public static function sanitize( $input ) {
		$clean = bstew_get_options();
		foreach ( array( 'verein_name', 'street', 'postcode', 'city', 'purpose' ) as $key ) {
			if ( isset( $input[ $key ] ) ) {
				$clean[ $key ] = sanitize_text_field( $input[ $key ] );
			}
		}
		if ( isset( $input['country'] ) ) {
			$clean['country'] = strtoupper( substr( sanitize_text_field( $input['country'] ), 0, 2 ) );
		}
		if ( isset( $input['iban'] ) ) {
			$iban = strtoupper( preg_replace( '/\s+/', '', sanitize_text_field( $input['iban'] ) ) );
			if ( '' !== $iban && ! bstew_iban_valid( $iban ) ) {
				add_settings_error( 'bstew_options', 'bstew_iban', __( 'The IBAN failed its check digits and was not saved.', 'beitrag-steward' ) );
			} else {
				$clean['iban'] = $iban;
			}
		}
		if ( isset( $input['bic'] ) ) {
			$clean['bic'] = strtoupper( preg_replace( '/\s+/', '', sanitize_text_field( $input['bic'] ) ) );
		}
		if ( isset( $input['creditor_id'] ) ) {
			$cid = strtoupper( preg_replace( '/\s+/', '', sanitize_text_field( $input['creditor_id'] ) ) );
			if ( '' !== $cid && ! bstew_creditor_id_valid( $cid ) ) {
				add_settings_error( 'bstew_options', 'bstew_cid', __( 'The creditor identifier (Gläubiger-ID) does not look valid and was not saved.', 'beitrag-steward' ) );
			} else {
				$clean['creditor_id'] = $cid;
			}
		}
		if ( isset( $input['default_amount'] ) ) {
			$clean['default_amount'] = number_format( (float) str_replace( ',', '.', $input['default_amount'] ), 2, '.', '' );
		}
		return $clean;
	}

	public static function render() {
		$o = bstew_get_options();
		settings_errors( 'bstew_options' );
		?>
		<div class="wrap">
			<h1><?php esc_html_e( 'Beitrag Steward — Settings', 'beitrag-steward' ); ?></h1>
			<form method="post" action="options.php">
				<?php settings_fields( 'bstew_options_group' ); ?>
				<table class="form-table" role="presentation">
					<tr><th><label for="bstew_name"><?php esc_html_e( 'Organization name', 'beitrag-steward' ); ?></label></th>
						<td><input id="bstew_name" class="regular-text" name="bstew_options[verein_name]" value="<?php echo esc_attr( $o['verein_name'] ); ?>"></td></tr>
					<tr><th><label for="bstew_street"><?php esc_html_e( 'Street and number', 'beitrag-steward' ); ?></label></th>
						<td><input id="bstew_street" class="regular-text" name="bstew_options[street]" value="<?php echo esc_attr( $o['street'] ); ?>"></td></tr>
					<tr><th><label for="bstew_postcode"><?php esc_html_e( 'Postcode / City', 'beitrag-steward' ); ?></label></th>
						<td>
							<input id="bstew_postcode" style="width:7em" name="bstew_options[postcode]" value="<?php echo esc_attr( $o['postcode'] ); ?>">
							<input style="width:16em" name="bstew_options[city]" value="<?php echo esc_attr( $o['city'] ); ?>">
							<input style="width:3.5em" name="bstew_options[country]" value="<?php echo esc_attr( $o['country'] ); ?>" maxlength="2">
							<p class="description"><?php esc_html_e( 'City and country are written into every SEPA file — structured addresses are mandatory from 15 November 2026.', 'beitrag-steward' ); ?></p>
						</td></tr>
					<tr><th><label for="bstew_iban"><?php esc_html_e( 'Organization IBAN', 'beitrag-steward' ); ?></label></th>
						<td><input id="bstew_iban" class="regular-text code" name="bstew_options[iban]" value="<?php echo esc_attr( $o['iban'] ); ?>"></td></tr>
					<tr><th><label for="bstew_bic"><?php esc_html_e( 'BIC (optional)', 'beitrag-steward' ); ?></label></th>
						<td><input id="bstew_bic" class="regular-text code" name="bstew_options[bic]" value="<?php echo esc_attr( $o['bic'] ); ?>"></td></tr>
					<tr><th><label for="bstew_cid"><?php esc_html_e( 'Creditor identifier (Gläubiger-ID)', 'beitrag-steward' ); ?></label></th>
						<td><input id="bstew_cid" class="regular-text code" name="bstew_options[creditor_id]" value="<?php echo esc_attr( $o['creditor_id'] ); ?>" placeholder="DE98ZZZ09999999999">
						<p class="description"><?php esc_html_e( 'Issued free of charge by the Bundesbank. Direct debits cannot be submitted without it.', 'beitrag-steward' ); ?></p></td></tr>
					<tr><th><label for="bstew_amount"><?php esc_html_e( 'Default annual fee (EUR)', 'beitrag-steward' ); ?></label></th>
						<td><input id="bstew_amount" style="width:7em" name="bstew_options[default_amount]" value="<?php echo esc_attr( $o['default_amount'] ); ?>"></td></tr>
					<tr><th><label for="bstew_purpose"><?php esc_html_e( 'Payment reference', 'beitrag-steward' ); ?></label></th>
						<td><input id="bstew_purpose" class="regular-text" name="bstew_options[purpose]" value="<?php echo esc_attr( $o['purpose'] ); ?>">
						<p class="description"><?php esc_html_e( '{jahr} is replaced by the collection year. Appears on the member’s bank statement.', 'beitrag-steward' ); ?></p></td></tr>
				</table>
				<?php submit_button(); ?>
			</form>
			<p class="description">
				<?php esc_html_e( 'Member bank data is stored encrypted on this server and never transmitted anywhere. Note: the encryption key is derived from this site’s AUTH_KEY — if you rotate your WordPress salts, stored IBANs must be re-entered.', 'beitrag-steward' ); ?>
			</p>
		</div>
		<?php
	}
}

BSTEW_Settings::init();
