<?php
/**
 * Integration Validator
 *
 * Validates third-party API integration connections.
 *
 * @package Sequensy\Validation
 * @since 2.0.0
 */

declare(strict_types=1);

namespace Sequensy\Validation;

use Sequensy\Validation\Contracts\ValidatorInterface;
use Sequensy\Validation\Concerns\CollectsErrors;
use Sequensy\Integrations\IntegrationStorage;

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * Class IntegrationValidator
 *
 * Validates API integration connections.
 */
class IntegrationValidator implements ValidatorInterface {

	use CollectsErrors;

	/**
	 * API-based integrations that require credentials.
	 *
	 * @var array
	 */
	private static array $api_integrations = array( 'mailchimp', 'kit', 'campaign-monitor', 'airtable', 'notion' );

	/**
	 * Validate integration configuration.
	 *
	 * This method validates all actions for integration connections.
	 * For single integration validation, use validate_connection().
	 *
	 * @param array $config  Workflow configuration.
	 * @param array $context Additional context (unused).
	 * @return array Validation result.
	 */
	public function validate( array $config, array $context = array() ): array {
		$this->clear_errors();

		// This validator is typically called per-action via validate_connection().
		// The validate() method can be used for batch validation if needed.

		return $this->get_result();
	}

	/**
	 * Validate a specific integration connection.
	 *
	 * @param string     $integration   Integration identifier.
	 * @param int|string $action_index  Action index for error context.
	 * @param string     $action_label  Action label for error messages.
	 * @return array Validation result with 'valid' and 'errors'.
	 */
	public function validate_connection( string $integration, int|string $action_index, string $action_label ): array {
		$this->clear_errors();

		// Only check API-based integrations that require credentials.
		if ( ! $this->is_api_integration( $integration ) ) {
			return $this->get_result();
		}

		// Check if integration is connected.
		if ( ! IntegrationStorage::is_connected( $integration ) ) {
			$integration_info = IntegrationStorage::get_integration( $integration );
			$integration_name = $integration_info ? $integration_info['name'] : ucfirst( $integration );

			$this->add_error(
				'integration_not_connected',
				sprintf( '%s requires %s integration to be connected.', $action_label, $integration_name ),
				array(
					'action_index' => $action_index,
					'integration'  => $integration,
				)
			);
		}

		return $this->get_result();
	}

	/**
	 * Check if an integration is API-based (requires credentials).
	 *
	 * @param string $integration Integration identifier.
	 * @return bool True if API-based integration.
	 */
	public function is_api_integration( string $integration ): bool {
		return in_array( $integration, self::$api_integrations, true );
	}

	/**
	 * Get list of API integrations.
	 *
	 * @return array Array of API integration identifiers.
	 */
	public static function get_api_integrations(): array {
		return self::$api_integrations;
	}
}
