<?php
/**
 * PeachPay Advanced Logging Settings.
 *
 * @package PeachPay
 */

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

/**
 * PeachPay Advanced Logging Settings page.
 */
final class PeachPay_Advanced_Logging_Settings extends PeachPay_Admin_Tab {

	/**
	 * The id to reference the stored settings with.
	 *
	 * @var string
	 */
	public $id = 'advanced_logging';

	/**
	 * Gets the section url key.
	 *
	 * @return string
	 */
	public function get_section() {
		return 'logging';
	}

	/**
	 * Gets the tab url key.
	 *
	 * @return string
	 */
	public function get_tab() {
		return 'settings';
	}

	/**
	 * Gets the tab title.
	 *
	 * @return string
	 */
	public function get_title() {
		return __( 'Advanced logging', 'peachpay-for-woocommerce' );
	}

	/**
	 * Gets the tab description.
	 *
	 * @return string
	 */
	public function get_description() {
		return __( 'Log detailed events (cron, webhooks, API calls, order flow) to WooCommerce logs. Use when troubleshooting issues. Logs appear under WooCommerce → Status → Logs. Only enable when needed; logging may increase log file size.', 'peachpay-for-woocommerce' );
	}

	/**
	 * Override option key to use peachpay_advanced_logging.
	 *
	 * @return string
	 */
	public function get_option_key() {
		return 'peachpay_advanced_logging';
	}

	/**
	 * Include dependencies here.
	 */
	protected function includes() {}

	/**
	 * Register form fields here.
	 *
	 * @return array
	 */
	protected function register_form_fields() {
		return array(
			'enable_logging' => array(
				'type'        => 'checkbox',
				'title'       => __( 'Enable advanced logging', 'peachpay-for-woocommerce' ),
				'label'       => __( 'Enable advanced logging', 'peachpay-for-woocommerce' ),
				'description' => __( 'Log detailed events (cron, webhooks, API calls, order flow) to WooCommerce logs. Use when troubleshooting issues. Logs appear under WooCommerce → Status → Logs.', 'peachpay-for-woocommerce' ),
				'default'     => 'no',
				'class'       => 'toggle',
			),
			'log_level'      => array(
				'type'        => 'select',
				'title'       => __( 'Log level', 'peachpay-for-woocommerce' ),
				'description' => __( 'Choose verbosity. Debug logs all events including API requests; Error logs failures only. Debug level may increase log size and slightly affect performance. Use only when troubleshooting.', 'peachpay-for-woocommerce' ),
				'default'     => 'info',
				'options'     => array(
					'error'   => __( 'Error (errors only)', 'peachpay-for-woocommerce' ),
					'warning' => __( 'Warning (warnings + errors)', 'peachpay-for-woocommerce' ),
					'info'    => __( 'Info (info + warnings + errors)', 'peachpay-for-woocommerce' ),
					'debug'   => __( 'Debug (everything, including API requests)', 'peachpay-for-woocommerce' ),
				),
			),
		);
	}

	/**
	 * Renders the Admin page.
	 */
	public function do_admin_view() {
		$from_date = gmdate( 'Y-m-d' );
		$from_time = '00:00';
		$to_date   = gmdate( 'Y-m-d' );
		$to_time   = gmdate( 'H:i' );
		?>
		<h1><?php echo esc_html( $this->get_title() ); ?></h1>
		<p><?php echo esc_html( $this->get_description() ); ?></p>
		<?php
		parent::do_admin_view();
		?>
		<div class="peachpay-log-download-section" style="margin-top: 2rem; padding-top: 1.5rem; border-top: 1px solid #ddd;">
			<h3><?php esc_html_e( 'Download logs', 'peachpay-for-woocommerce' ); ?></h3>
			<p class="description"><?php esc_html_e( 'Download PeachPay log entries for a date/time range. Use when sharing logs with support.', 'peachpay-for-woocommerce' ); ?></p>
			<form method="get" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" target="_blank" style="display: flex; flex-wrap: wrap; align-items: center; gap: 1rem; margin-top: 1rem;">
				<input type="hidden" name="action" value="peachpay_download_logs" />
				<?php wp_nonce_field( 'peachpay_download_logs', '_wpnonce', false ); ?>
				<label>
					<?php esc_html_e( 'From', 'peachpay-for-woocommerce' ); ?>
					<input type="date" name="from_date" value="<?php echo esc_attr( $from_date ); ?>" style="margin: 0 4px;" />
					<input type="time" name="from_time" value="<?php echo esc_attr( $from_time ); ?>" />
				</label>
				<label>
					<?php esc_html_e( 'To', 'peachpay-for-woocommerce' ); ?>
					<input type="date" name="to_date" value="<?php echo esc_attr( $to_date ); ?>" style="margin: 0 4px;" />
					<input type="time" name="to_time" value="<?php echo esc_attr( $to_time ); ?>" />
				</label>
				<button type="submit" class="button button-secondary"><?php esc_html_e( 'Download logs', 'peachpay-for-woocommerce' ); ?></button>
			</form>
		</div>
		<?php
	}
}
