<?php
/**
 * Settings: a small page under WooCommerce → Settings → Delivery Date.
 *
 * @package Datery
 */

defined( 'ABSPATH' ) || exit;

class Datery_Settings {

	private static $instance = null;
	private static $cache    = null;

	public static function instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	private function __construct() {
		add_filter( 'woocommerce_get_settings_pages', array( $this, 'register_settings_page' ) );
	}

	/**
	 * Read a single setting with fallback to its default.
	 */
	public static function get( $key, $default = '' ) {
		if ( null === self::$cache ) {
			self::$cache = wp_parse_args(
				get_option( 'datery_settings', array() ),
				array(
					'field_label'   => __( 'Delivery date', 'datery-delivery-date-for-woocommerce' ),
					'required'      => 'yes',
					'min_lead_days' => 1,
					'max_days'      => 30,
					'cutoff_time'   => '',
				)
			);
		}
		return isset( self::$cache[ $key ] ) ? self::$cache[ $key ] : $default;
	}

	public function register_settings_page( $pages ) {
		require_once DATERY_PATH . 'includes/class-datery-settings-page.php';
		$pages[] = new Datery_Settings_Page();
		return $pages;
	}
}
