<?php
/**
 * Validation Result Value Object
 *
 * Immutable result of a validation operation, carrying validity state,
 * error message, and optional warnings.
 *
 * @package Sequensy
 * @since 2.0.0
 */

declare(strict_types=1);

namespace Sequensy\Engine;

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

/**
 * Class ValidationResult
 *
 * Simple value object for validation outcomes. Supports a single error
 * (invalid state) and a single warning (valid but noteworthy).
 */
final class ValidationResult {

	/**
	 * Whether the validation passed.
	 *
	 * @var bool
	 */
	private bool $valid;

	/**
	 * Error message if validation failed.
	 *
	 * @var string
	 */
	private string $error;

	/**
	 * Warning message if validation passed with concerns.
	 *
	 * @var string
	 */
	private string $warning;

	/**
	 * Constructor.
	 *
	 * @param bool   $valid   Whether the validation passed.
	 * @param string $error   Error message (empty if valid).
	 * @param string $warning Warning message (empty if no concerns).
	 */
	private function __construct( bool $valid, string $error = '', string $warning = '' ) {
		$this->valid   = $valid;
		$this->error   = $error;
		$this->warning = $warning;
	}

	/**
	 * Create a successful validation result.
	 *
	 * @param string $warning Optional warning message.
	 * @return self
	 */
	public static function ok( string $warning = '' ): self {
		return new self( true, '', $warning );
	}

	/**
	 * Create a failed validation result.
	 *
	 * @param string $error Error message describing the failure.
	 * @return self
	 */
	public static function fail( string $error ): self {
		return new self( false, $error );
	}

	/**
	 * Whether the validation passed.
	 *
	 * @return bool
	 */
	public function is_valid(): bool {
		return $this->valid;
	}

	/**
	 * Whether the result carries a warning.
	 *
	 * @return bool
	 */
	public function has_warnings(): bool {
		return '' !== $this->warning;
	}

	/**
	 * Get the error message.
	 *
	 * @return string Error message, or empty string if valid.
	 */
	public function get_error(): string {
		return $this->error;
	}

	/**
	 * Get the warning message.
	 *
	 * @return string Warning message, or empty string if none.
	 */
	public function get_warning(): string {
		return $this->warning;
	}
}
