<?php
/**
 * GurmeCargo için WC ayarlar sayfasına ayar ekleyen sınıfı barındırır.
 *
 * @package GurmeHub
 */

/**
 * Tab sınıfı.
 */
class GCARGO_WC_Settings_Tab extends WC_Settings_Page {

	/**
	 * Kurucu method.
	 */
	public function __construct() {
		$this->id    = GCARGO_PREFIX;
		$this->label = __( 'Cargo', 'kargo-entegrator' );
		parent::__construct();
	}


	/**
	 * Output
	 */
	public function output() {

		$localize_data = array(
			'api_key'                => gcargo_api_key()->get(),
			'warehouses'             => gcargo_saas_warehouses_settings()->get_settings(),
			'cargo_integrations'     => gcargo_saas_cargo_integrations_settings()->get_settings(),
			'cargo_companies'        => gcargo_api_requests()->get_cargo_companies(),
			'http_logs'              => gcargo_http_client()->logger->get_logs(),
			'shipment_setting'       => gcargo_saas_shipment_settings()->get_settings(),
			'returned_setting'       => gcargo_saas_returned_settings()->get_settings(),
			'account_details'        => gcargo_api_requests()->get_profile(),
			'other_settings'         => gcargo_other_settings()->get_settings(),
			'package_setting'        => gcargo_package_settings()->get_settings(),
			'auto_shipment_settings' => gcargo_auto_shipment_settings()->get_settings(),
			'auto_shipment_statuses' => gcargo_auto_shipment_statuses(),
			'auto_shipment_timeouts' => gcargo_auto_shipment_timeouts(),
			'all_order_statuses'     => gcargo_wc_status_manager()->get_merged_statuses_for_frontend(),
			'order_status_settings'  => gcargo_wc_order_status_settings()->get_settings(),
			'gate_settings'          => gcargo_gate_settings()->get_settings(),
			'status'                 => $this->get_env_info(),
			'gcargo_env'             => defined( 'GCARGO_DEVELOPMENT' ) && GCARGO_DEVELOPMENT ? 'staging' : 'app',
			'gcargo_connect_url'     => home_url( GCARGO_PREFIX . '-connect' ),
			'gcargo_webhook_url'     => home_url( GCARGO_PREFIX . '-webhook' ),
			'last_sync_time'         => gcargo_get_last_sync_time(),
		);

		if ( class_exists( 'GurmeCargoMP' ) ) {
			$localize_data['sellers']            = $this->get_sellers_with_address();
			$localize_data['marketplace_active'] = true;
		}

		gcargo_vue()
			->set_localize( array_merge( $localize_data, gcargo_data_handler()->default_localize_data() ) )
			->set_vue_page( 'settings' )
			->require_script()
			->require_style()
			->create_app_div();
	}

	/**
	 * GurmeKargo için ortam bilgisi
	 *
	 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
	 * @return array
	 */
	public function get_env_info() {
		$theme = wp_get_theme( get_stylesheet() );

		$response   = wp_remote_get( 'https://icanhazip.com/' );
		$ip_address = __( 'Not available', 'kargo-entegrator' );

		if ( ! is_wp_error( $response ) ) {
			$ip_address = trim( wp_remote_retrieve_body( $response ) );
			$ip_address = filter_var( $ip_address, FILTER_VALIDATE_IP ) ? $ip_address : __( 'Not available', 'kargo-entegrator' );
		}

		global $wpdb;

		$shipments_table = $wpdb->prefix . GCARGO_PREFIX . '_shipments';

		$returned_table = $wpdb->prefix . GCARGO_PREFIX . '_returneds';

		return array(
			'wordpress'   => array(
				array(
					'label' => __( 'Theme Name', 'kargo-entegrator' ),
					'value' => $theme->get( 'Name' ),
				),
				array(
					'label' => __( 'Theme Version', 'kargo-entegrator' ),
					'value' => $theme->get( 'Version' ),
				),
				array(
					'label' => __( 'WordPress Version', 'kargo-entegrator' ),
					'value' => get_bloginfo( 'version' ),
				),
				array(
					'label' => __( 'Multisite', 'kargo-entegrator' ),
					'value' => is_multisite() ? __( 'Yes', 'kargo-entegrator' ) : __( 'No', 'kargo-entegrator' ),
				),
				array(
					'label' => __( 'Debug Mode', 'kargo-entegrator' ),
					'value' => ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? __( 'Activated', 'kargo-entegrator' ) : __( 'Disabled', 'kargo-entegrator' ),
				),
			),
			'server'      => array(
				array(
					'label' => 'PHP Version',
					'value' => function_exists( 'phpversion' ) && phpversion() ? phpversion() : __( 'Not available', 'kargo-entegrator' ),
				),
				array(
					'label' => 'PHP cURL',
					'value' => function_exists( 'curl_init' ) ? __( 'Yes', 'kargo-entegrator' ) : __( 'No', 'kargo-entegrator' ),
				),
				array(
					'label' => 'PHP Memory Limit',
					'value' => ini_get( 'memory_limit' ),
				),
				array(
					'label' => 'PHP Max Execution Time',
					'value' => ini_get( 'max_execution_time' ),
				),
				array(
					'label' => __( 'IP Address', 'kargo-entegrator' ),
					'value' => $ip_address,
				),
			),
			'woocommerce' => array(
				array(
					'label' => 'Version',
					'value' => WC_VERSION,
				),
			),
			'db'          => array(
				array(
					'label' => 'Kargo Entegrator Database Version',
					'value' => get_option( 'gcargo_db_version', '0.0.0' ) . ' / ' . GCARGO_DB_VERSION,
				),
				array(
					'label' => 'Database Version',
					'value' => $wpdb->get_var('SELECT VERSION()'), //phpcs:ignore
				),
				array(
					'key'   => 'table',
					'label' => $shipments_table,
					'value' => $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $shipments_table ) ),
				),
				array(
					'key'   => 'table',
					'label' => $returned_table,
					'value' => $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $returned_table ) ),
				),
			),
		);
	}

	/**
	 * Seller bilgilerini adres bilgileriyle birlikte getirir
	 *
	 * @return array
	 */
	private function get_sellers_with_address() {

		if ( function_exists( 'dokan_get_sellers' ) ) {

			$sellers_data = dokan_get_sellers( [ 'number' => -1 ] );

			if ( ! empty( $sellers_data['users'] ) ) {
				foreach ( $sellers_data['users'] as &$seller ) {
					$seller_id = $seller->ID;

					$address = dokan_get_seller_address( $seller_id, true );

					$store_info = dokan_get_store_info( $seller_id );

					$seller->address    = $address;
					$seller->store_info = $store_info;

					$seller->name    = $seller->first_name;
					$seller->surname = $seller->last_name;

					$seller->gcargo_seller_id = get_user_meta( $seller_id, 'gcargo_seller_id', true );
				}
			}

			return $sellers_data;
		}
	}
}
