<?php

namespace DeliciousBrains\WP_Offload_Media\Providers\Storage;

use Exception;

class S3_Compatible_Provider extends AWS_Provider {

	/**
	 * @var string
	 */
	protected static $provider_name = 'S3-Compatible';

	/**
	 * @var string
	 */
	protected static $provider_short_name = 'S3 Compatible';

	/**
	 * Used in filters and settings.
	 *
	 * @var string
	 */
	protected static $provider_key_name = 's3compatible';

	/**
	 * @var string
	 */
	protected static $service_name = 'S3-Compatible Storage';

	/**
	 * @var string
	 */
	protected static $service_short_name = 'S3';

	/**
	 * Used in filters and settings.
	 *
	 * @var string
	 */
	protected static $service_key_name = 's3compat';

	/**
	 * Optional override of "Provider Name" + "Service Name" for friendly name for service.
	 *
	 * @var string
	 */
	protected static $provider_service_name = 'S3-Compatible Storage';

	/**
	 * The slug for the service's quick start guide doc.
	 *
	 * @var string
	 */
	protected static $provider_service_quick_start_slug = '';

	/**
	 * @var array
	 */
	protected static $access_key_id_constants = array(
		'AS3CF_S3COMPAT_ACCESS_KEY_ID',
	);

	/**
	 * @var array
	 */
	protected static $secret_access_key_constants = array(
		'AS3CF_S3COMPAT_SECRET_ACCESS_KEY',
	);

	/**
	 * Server roles not supported for generic S3-compatible services.
	 *
	 * @var array
	 */
	protected static $use_server_roles_constants = array();

	/**
	 * Block Public Access is not universally supported by S3-compatible services.
	 *
	 * @var bool
	 */
	protected static $block_public_access_supported = false;

	/**
	 * Object Ownership is not universally supported by S3-compatible services.
	 *
	 * @var bool
	 */
	protected static $object_ownership_supported = false;

	/**
	 * S3-compatible services use free-form region strings; no fixed list.
	 *
	 * @var array
	 */
	protected static $regions = array();

	/**
	 * @var bool
	 */
	protected static $region_required = false;

	/**
	 * Default region required by the AWS SDK, even for S3-compatible services.
	 *
	 * @var string
	 */
	protected static $default_region = 'us-east-1';

	/**
	 * No fixed default domain; derived from the configured endpoint.
	 *
	 * @var string
	 */
	protected $default_domain = '';

	/**
	 * @var string
	 */
	protected $console_url = '';

	/**
	 * @var string
	 */
	protected $console_url_prefix_param = '/';

	/**
	 * Process the args before instantiating a new client for the provider's SDK.
	 * Injects the custom endpoint and enables path-style access.
	 *
	 * @param array $args
	 *
	 * @return array
	 */
	protected function init_client_args( array $args ) {
		$endpoint = $this->as3cf->get_setting( 'endpoint' );

		if ( ! empty( $endpoint ) ) {
			$args['endpoint']               = rtrim( $endpoint, '/' );
			$args['use_path_style_endpoint'] = true;
		}

		return $args;
	}

	/**
	 * Returns the host (and port if non-standard) extracted from the configured endpoint URL.
	 *
	 * @return string
	 */
	public function get_domain() {
		$endpoint = $this->as3cf->get_setting( 'endpoint' );

		if ( empty( $endpoint ) ) {
			return '';
		}

		$parsed = parse_url( $endpoint );
		$host   = isset( $parsed['host'] ) ? $parsed['host'] : '';

		if ( ! empty( $parsed['port'] ) ) {
			$host .= ':' . $parsed['port'];
		}

		return $host;
	}

	/**
	 * No region-based prefix is needed for S3-compatible services.
	 *
	 * @param string   $region
	 * @param null|int $expires
	 *
	 * @return string
	 */
	protected function url_prefix( $region = '', $expires = null ) {
		return '';
	}

	/**
	 * Always use path-style URLs: endpoint-host/bucket instead of bucket.endpoint-host.
	 *
	 * @param string $domain
	 * @param string $bucket
	 * @param string $region
	 * @param int    $expires
	 * @param array  $args
	 *
	 * @return string
	 */
	protected function url_domain( $domain, $bucket, $region = '', $expires = null, $args = array() ) {
		return $domain . '/' . $bucket;
	}

	/**
	 * Create bucket without a LocationConstraint, which many S3-compatible
	 * services do not support.
	 *
	 * @param array $args
	 */
	public function create_bucket( array $args ) {
		// Remove AWS-specific location constraint; S3-compatible services
		// typically don't support it and use the endpoint region instead.
		unset( $args['LocationConstraint'] );
		parent::create_bucket( $args );
	}

	/**
	 * Block Public Access is not supported by most S3-compatible services.
	 *
	 * @param string $bucket
	 * @param bool   $block
	 */
	public function block_public_access( string $bucket, bool $block ) {
		// Not supported — do nothing.
	}

	/**
	 * Object Ownership enforcement is not supported by most S3-compatible services.
	 *
	 * @param string $bucket
	 * @param bool   $enforce
	 */
	public function enforce_object_ownership( string $bucket, bool $enforce ) {
		// Not supported — do nothing.
	}

	/**
	 * Returns the console title (not applicable for generic S3-compatible services).
	 *
	 * @return string
	 */
	public static function get_console_title(): string {
		return _x( 'Console', 'Provider console link text', 'amazon-s3-and-cloudfront' );
	}
}
