<?php
/**
 * API Key Authentication Strategy
 *
 * @category   WordPress_Plugin
 * @package    Common
 * @subpackage Strategies\Authentication
 * @author     miniOrange <info@miniorange.com>
 * @license    MIT/Expat
 * @link       https://plugins.miniorange.com/woocommerce-api-product-sync-with-woocommerce-rest-apis
 */

namespace MOWCPS\Common\Strategies\Authentication;

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

use WP_Error;
use MOWCPS\Common\Abstracts\Auth_Base_Strategy;

/**
 * Class ApiKeyAuth_Strategy
 *
 * Handles API key authentication
 */
class Api_Key_Auth_Strategy extends Auth_Base_Strategy {
	/**
	 * Generate authorization headers
	 *
	 * @param int   $supplier_id Supplier ID.
	 * @param array $settings Auth settings.
	 * @return array|WP_Error Headers array or error
	 */
	public function generate_headers( $settings, $supplier_id ) {
		// Validate settings first.
		$validation = $this->validate_settings( $settings );
		if ( is_wp_error( $validation ) ) {
			return $validation;
		}

		$key    = $settings['key'];
		$secret = $settings['secret'];

		return array(
			$key => $secret,
		);
	}

	/**
	 * Validate auth settings
	 *
	 * @param array $settings Auth settings.
	 * @return bool|WP_Error True if valid, WP_Error if invalid
	 */
	public function validate_settings( $settings ) {
		if ( empty( $settings['key'] ) || empty( $settings['secret'] ) ) {
			return new WP_Error(
				'invalid_api_key_settings',
				'Invalid API key settings: missing key or secret',
				array( 'status' => 400 )
			);
		}

		return true;
	}
}
