<?php
/**
 * GurmeCargo ile ödeme geçitlerine atılacak istekleri organize eder.
 *
 * @package GurmeHub
 */

/**
 * GurmeCargo istek sınıfı
 */
class GCARGO_Api_Requests extends GCARGO_Connection {

	/**
	 * Api bağlantı testi.
	 *
	 * @param string $api_key Erişim anahtarı.
	 */
	public function check_connection( $api_key ) {
		return $this->http_client->set_headers(
			array(
				'Content-Type'  => 'application/json',
				'Accept'        => 'application/json',
				'Authorization' => 'Bearer ' . $api_key,
			)
		)->request( '/helpers/check-connection', 'GET' );
	}

	/**
	 * Webhook bağlantı isteği.
	 */
	public function save_webhook_connection() {
		$this->http_client->request(
			'/helpers/settings-webhook',
			'POST',
			[
				'webhook_url' => home_url( GCARGO_PREFIX . '-webhook' ),
			]
		);
	}

	/**
	 * Kargo firmaları.
	 *
	 * @return array
	 */
	public function get_cargo_companies() {
		$companies     = [];
		$http_response = $this->http_client->request( '/helpers/cargo-companies', 'GET' );

		if ( 200 === $http_response['response']['code'] ) {
			$companies = $this->http_client->get_body( $http_response );
		}
		return $companies;
	}

	/**
	 * Barkod yazdırma.
	 *
	 * @param array $shipments Gönderi kimlikleri.
	 *
	 * @return mixed
	 *
	 * @throws Exception Barko yazdırılamadı.
	 */
	public function print( $shipments ) {
		$url           = add_query_arg( [ 'shipments' => $shipments ], '/print-pdf' );
		$http_response = $this->http_client->request( $url, 'GET' );

		if ( 200 === $http_response['response']['code'] ) {
			// phpcs:ignore
			return base64_encode( $http_response['body'] );
		}

		throw new Exception( 'Barkod yazdırılamadı. Lütfen tekrar deneyin' );
	}

	/**
	 * Barkod yazdırma.
	 *
	 * @param array $returneds İade kimlikleri.
	 *
	 * @return mixed
	 *
	 * @throws Exception Barkod yazdırılamadı.
	 */
	public function print_returneds( $returneds ) {
		$url           = add_query_arg( [ 'returneds' => $returneds ], '/print-returned-pdf' );
		$http_response = $this->http_client->request( $url, 'GET' );

		if ( 200 === $http_response['response']['code'] ) {
			// phpcs:ignore
			return base64_encode( $http_response['body'] );
		}

		throw new Exception( 'İade barkodu yazdırılamadı. Lütfen tekrar deneyin' );
	}

	/**
	 * Gönderi ayarları kayıt etme.
	 *
	 * @param array $data Ayarlar.
	 *
	 * @return mixed
	 */
	public function update_shipment_setting( $data ) {
		$http_response = $this->http_client->request( '/settings/shipment-setting', 'PATCH', wp_json_encode( $data ) );
		if ( 200 === $http_response['response']['code'] ) {
			return $this->http_client->get_body( $http_response );
		}
	}

	/**
	 * Gönderi ayarları getirme.
	 *
	 * @return mixed
	 */
	public function get_shipment_setting() {
		$settings      = [];
		$http_response = $this->http_client->request( '/settings/shipment-setting', 'GET' );
		if ( 200 === $http_response['response']['code'] ) {
			$settings = $this->http_client->get_body( $http_response );
		}

		return $settings;
	}

	/**
	 * Return settings update endpoint.
	 *
	 * @param array $data Settings data.
	 *
	 * @return mixed
	 */
	public function update_returned_setting( $data ) {
		$http_response = $this->http_client->request( '/settings/returned-setting', 'PATCH', wp_json_encode( $data ) );
		if ( 200 === $http_response['response']['code'] ) {
			return $this->http_client->get_body( $http_response );
		}
	}

	/**
	 * Get return settings endpoint.
	 *
	 * @return mixed
	 */
	public function get_returned_setting() {
		$settings      = [];
		$http_response = $this->http_client->request( '/settings/returned-setting', 'GET' );
		if ( 200 === $http_response['response']['code'] ) {
			$settings = $this->http_client->get_body( $http_response );
		}

		return $settings;
	}
	/** Gönderi ayarları getirme.
	 *
	 * @return mixed
	 */
	public function get_profile() {
		$account_details = [];
		$http_response   = $this->http_client->request( '/helpers/profile', 'GET' );
		if ( 200 === $http_response['response']['code'] ) {
			$account_details = $this->http_client->get_body( $http_response );
		}
		return $account_details;
	}

	/**
	 * AI ile düzenlemeyi getirme
	 *
	 * @param array $data Data.
	 *
	 * @return mixed
	 */
	public function fix_address( $data ) {
		$http_response = $this->http_client->request( '/helpers/fix-address', 'POST', wp_json_encode( $data ) );
		if ( 200 === $http_response['response']['code'] ) {
			return $this->http_client->get_body( $http_response );
		}
		return $http_response;
	}

	/**
	 * Create mp seller and warehouse.
	 *
	 * @param array $data Data.
	 *
	 * @return mixed
	 */
	public function create_mp_seller( $data ) {
		$http_response = $this->http_client->request( '/seller-warehouse-create', 'POST', wp_json_encode( $data ) );
		if ( 201 === $http_response['response']['code'] ) {
			$data = $this->http_client->get_body( $http_response );
			update_user_meta( $data['platform_seller_id'], 'gcargo_seller_id', $data['id'] );
			gcargo_sync_options();
		}
		return $http_response;
	}
}
