<?php

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

class AIEO_DMM_Food_Beverages extends AIEO_DMM_Module_Base {

	const NONCE  = 'aieo_food_admin';
	const OPTION = 'aieo_food_settings';

	public function get_id()       { return 'food_beverages'; }
	public function get_label()    { return __( 'Food &amp; Beverages', 'ai-eshop-optimizer' ); }
	public function get_group()    { return 'custom_post_types'; }
	public function get_priority() { return 95; } 

	public function register_hooks() {
		add_action( 'wp_ajax_aieo_food_save_settings', array( $this, 'ajax_save_settings' ) );
	}

	

	public static function settings() {
		$s = get_option( self::OPTION, array() );
		return is_array( $s ) ? $s : array();
	}

	
	public static function is_enabled() {
		$s = self::settings();
		return ! empty( $s['enabled'] );
	}

	
	public static function feature_on( $feature ) {
		$s = self::settings();
		if ( empty( $s['enabled'] ) ) { return false; }
		if ( ! isset( $s['features'][ $feature ] ) ) {
			return 'option_groups' === $feature;
		}
		return ! empty( $s['features'][ $feature ] );
	}

	

	public function render_subtab() {
		$enabled = self::is_enabled();
		$og_on   = self::feature_on( 'option_groups' );
		$nonce   = wp_create_nonce( self::NONCE );
		$og_url  = admin_url( 'admin.php?page=ai-eshop-dmm&dmm_tab=option_groups' );
		?>
		<div id="aieo-food-root" data-nonce="<?php echo esc_attr( $nonce ); ?>">
			<h2><?php esc_html_e( 'Food & Beverages', 'ai-eshop-optimizer' ); ?></h2>
			<p class="description" style="max-width:760px;">
				<?php esc_html_e( 'Turn on the Food & Beverages toolset — product options & modifiers (sizes, toppings, with/without extras) now, and Wolt & eFood order-taking + menu sync soon. It is off by default, so shops that don’t sell food keep a clean admin with none of these screens.', 'ai-eshop-optimizer' ); ?>
			</p>

			<div class="aieo-dmm-card" style="max-width:100%;padding:16px 20px;">
				<p class="form-field" style="margin:0 0 12px;">
					<label style="font-weight:600;font-size:14px;cursor:pointer;">
						<input type="checkbox" id="aieo-food-enabled" <?php checked( $enabled ); ?>>
						<?php esc_html_e( 'Enable Food & Beverages features', 'ai-eshop-optimizer' ); ?>
					</label>
				</p>

				<div id="aieo-food-features" style="margin-left:24px;<?php echo $enabled ? '' : 'opacity:.5;pointer-events:none;'; ?>">
					<p style="margin:0 0 6px;">
						<label><input type="checkbox" id="aieo-food-feature-og" <?php checked( $og_on ); ?>>
							<strong><?php esc_html_e( 'Product options & modifiers', 'ai-eshop-optimizer' ); ?></strong>
							<span class="description"><?php esc_html_e( '— sizes, toppings, extras, with/without choices, per-option pricing & availability.', 'ai-eshop-optimizer' ); ?></span>
						</label>
					</p>
					<p style="margin:0 0 6px;color:#9ca3af;">
						<label><input type="checkbox" disabled>
							<?php esc_html_e( 'Wolt — orders & menu', 'ai-eshop-optimizer' ); ?>
							<span class="description"><?php esc_html_e( '(coming soon)', 'ai-eshop-optimizer' ); ?></span>
						</label>
					</p>
					<p style="margin:0;color:#9ca3af;">
						<label><input type="checkbox" disabled>
							<?php esc_html_e( 'eFood — orders & catalog', 'ai-eshop-optimizer' ); ?>
							<span class="description"><?php esc_html_e( '(coming soon)', 'ai-eshop-optimizer' ); ?></span>
						</label>
					</p>
				</div>

				<p style="margin:16px 0 0;">
					<button type="button" class="button button-primary" id="aieo-food-save"><?php esc_html_e( 'Save', 'ai-eshop-optimizer' ); ?></button>
					<span id="aieo-food-status" style="margin-left:10px;font-size:12px;color:#646970;"></span>
				</p>

				<?php if ( $enabled && $og_on ) : ?>
					<p class="description" style="margin:12px 0 0;">
						<?php
						printf(
							
							wp_kses_post( __( 'Product Options is active → manage your groups in %s, then attach them to a product from its “Options” tab.', 'ai-eshop-optimizer' ) ),
							'<a href="' . esc_url( $og_url ) . '">' . esc_html__( 'Product Options', 'ai-eshop-optimizer' ) . '</a>'
						);
						?>
					</p>
				<?php endif; ?>
			</div>
		</div>
		<script>
		(function(){
			var root = document.getElementById('aieo-food-root'); if(!root) return;
			var en = document.getElementById('aieo-food-enabled');
			var feats = document.getElementById('aieo-food-features');
			var st = document.getElementById('aieo-food-status');
			en.addEventListener('change', function(){
				feats.style.opacity = en.checked ? '' : '.5';
				feats.style.pointerEvents = en.checked ? '' : 'none';
			});
			document.getElementById('aieo-food-save').addEventListener('click', function(){
				st.style.color = '#646970'; st.textContent = <?php echo wp_json_encode( __( 'Saving…', 'ai-eshop-optimizer' ) ); ?>;
				var fd = new FormData();
				fd.append('action','aieo_food_save_settings');
				fd.append('nonce', root.getAttribute('data-nonce'));
				fd.append('enabled', en.checked ? 1 : 0);
				fd.append('feature_option_groups', document.getElementById('aieo-food-feature-og').checked ? 1 : 0);
				fetch(ajaxurl, {method:'POST', body:fd, credentials:'same-origin'}).then(function(r){return r.json();}).then(function(j){
					if(j && j.success){ st.style.color='#15803d'; st.textContent = <?php echo wp_json_encode( __( 'Saved — reloading…', 'ai-eshop-optimizer' ) ); ?>; setTimeout(function(){ location.reload(); }, 600); }
					else { st.style.color='#b91c1c'; st.textContent = (j && j.data && j.data.message) || 'Error'; }
				}).catch(function(){ st.style.color='#b91c1c'; st.textContent='Request failed'; });
			});
		})();
		</script>
		<?php
	}

	public function ajax_save_settings() {
		if ( ! check_ajax_referer( self::NONCE, 'nonce', false ) ) {
			wp_send_json_error( array( 'message' => __( 'Security check failed.', 'ai-eshop-optimizer' ) ) );
		}
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
			wp_send_json_error( array( 'message' => __( 'Not allowed.', 'ai-eshop-optimizer' ) ) );
		}
		$enabled = ! empty( $_POST['enabled'] );
		$og      = ! empty( $_POST['feature_option_groups'] );
		update_option( self::OPTION, array(
			'enabled'  => $enabled,
			'features' => array( 'option_groups' => $og ),
		), false );
		wp_send_json_success( array( 'reload' => true ) );
	}
}
