<?php

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

class AIEO_DMM_SEO_Schema extends AIEO_DMM_Module_Base {

	public function get_id()       { return 'seo_schema'; }
	public function get_label()    { return __( 'Schema', 'ai-eshop-optimizer' ); }
	public function get_priority() { return 10; }
	public function get_group()    { return 'seo'; }
	public function get_section()  { return 'schema'; }

	public function register_hooks() {
		add_action( 'wp_ajax_aieo_seo_save_schema', array( $this, 'ajax_save' ) );
	}

	public function ajax_save() {
		AIEO_DMM::verify_ajax_request();
		$txt = static function ( $k ) {
			return isset( $_POST[ $k ] ) ? sanitize_text_field( wp_unslash( $_POST[ $k ] ) ) : ''; 
		};
		
		
		$star_in    = ( isset( $_POST['rating_star_meanings'] ) && is_array( $_POST['rating_star_meanings'] ) ) ? wp_unslash( $_POST['rating_star_meanings'] ) : array(); 
		$star_clean = array();
		for ( $i = 1; $i <= 5; $i++ ) {
			$star_clean[ $i ] = isset( $star_in[ $i ] ) ? sanitize_text_field( (string) $star_in[ $i ] ) : '';
		}
		$clean = array(
			'org_represents' => ( 'person' === $txt( 'org_represents' ) ) ? 'person' : 'organization',
			'org_name'       => $txt( 'org_name' ),
			'org_logo'       => isset( $_POST['org_logo'] ) ? esc_url_raw( wp_unslash( $_POST['org_logo'] ) ) : '',
			'org_phone'      => $txt( 'org_phone' ),
			'org_email'      => isset( $_POST['org_email'] ) ? sanitize_email( wp_unslash( $_POST['org_email'] ) ) : '',
			'org_street'     => $txt( 'org_street' ),
			'org_locality'   => $txt( 'org_locality' ),
			'org_region'     => $txt( 'org_region' ),
			'org_postcode'   => $txt( 'org_postcode' ),
			'org_country'    => $txt( 'org_country' ),
			
			
			'locations_schema_enabled' => ! empty( $_POST['locations_schema_enabled'] ), 
			
			
			
			'return_enabled' => ! empty( $_POST['return_enabled'] ),
			'return_days'    => isset( $_POST['return_days'] ) ? max( 0, (int) $_POST['return_days'] ) : 0,
			'return_country' => isset( $_POST['return_country'] ) ? strtoupper( substr( sanitize_text_field( wp_unslash( $_POST['return_country'] ) ), 0, 2 ) ) : '',
			'return_free'    => ! empty( $_POST['return_free'] ),
			'ship_enabled'   => ! empty( $_POST['ship_enabled'] ),
			'ship_country'   => isset( $_POST['ship_country'] ) ? strtoupper( substr( sanitize_text_field( wp_unslash( $_POST['ship_country'] ) ), 0, 2 ) ) : '',
			'ship_rate'      => ( isset( $_POST['ship_rate'] ) && '' !== $_POST['ship_rate'] ) ? (float) $_POST['ship_rate'] : '',
			'handling_min'   => isset( $_POST['handling_min'] ) ? max( 0, (int) $_POST['handling_min'] ) : 0,
			'handling_max'   => isset( $_POST['handling_max'] ) ? max( 0, (int) $_POST['handling_max'] ) : 0,
			'transit_min'    => isset( $_POST['transit_min'] ) ? max( 0, (int) $_POST['transit_min'] ) : 0,
			'transit_max'    => isset( $_POST['transit_max'] ) ? max( 0, (int) $_POST['transit_max'] ) : 0,
			
			
			
			
			'schema_faq_enabled' => ! empty( $_POST['schema_faq_enabled'] ), 
			
			
			'color_attribute_taxonomies' => ( isset( $_POST['color_attribute_taxonomies'] ) && is_array( $_POST['color_attribute_taxonomies'] ) ) 
				? array_values( array_unique( array_filter( array_map( 'sanitize_key', wp_unslash( $_POST['color_attribute_taxonomies'] ) ) ) ) )
				: array(),
			
			
			
			'rating_review_enabled' => ! empty( $_POST['rating_review_enabled'] ), 
			'rating_star_meanings'  => $star_clean,
		);
		AIEO_DMM_Settings::update( array( 'seo' => $clean ) ); 
		wp_send_json_success( array( 'message' => __( 'Schema settings saved.', 'ai-eshop-optimizer' ) ) );
	}

	public function render_subtab() {
		if ( function_exists( 'wp_enqueue_media' ) ) {
			wp_enqueue_media(); 
		}
		$loc_count = 0;
		if ( class_exists( 'AIEO_Google_Reviews_Locations' ) ) {
			$loc_count = count( (array) AIEO_Google_Reviews_Locations::list_all( true ) );
		}
		$this->render_template(
			'seo/schema.php',
			array(
				'seo'       => (array) AIEO_DMM_Settings::get( 'seo', array() ),
				'loc_count' => $loc_count,
				'loc_url'   => admin_url( 'admin.php?page=ai-eshop-dmm&dmm_tab=google_reviews' ),
				'nonce'     => wp_create_nonce( AIEO_DMM::NONCE_ACTION ),
				'ajax'      => admin_url( 'admin-ajax.php' ),
			)
		);
	}
}
