<?php
/**
 * ValidationException class.
 *
 * @package DokanKits\Core\Exception
 * @since   3.0.0
 */

namespace DokanKits\Core\Exception;

use Throwable;
use WP_REST_Response;

/**
 * Validation Exception
 *
 * @since   3.0.0
 * @package DokanKits\Core\Exception
 */
class ValidationException extends BaseException {
	/**
	 * Constructor
	 *
	 * @since 3.0.0
	 *
	 * @param string         $message  Error message.
	 * @param array          $errors   Validation errors.
	 * @param int            $code     Error code.
	 * @param Throwable|null $previous Previous exception.
	 */
	public function __construct( $message = 'Validation failed', $errors = array(), $code = 422, Throwable $previous = null ) {
		parent::__construct( $message, $code, array( 'errors' => $errors ), $previous );
	}

	/**
	 * Get validation errors
	 *
	 * @since 3.0.0
	 * @return array
	 */
	public function get_errors(): array {
		return $this->data['errors'] ?? array();
	}

	/**
	 * Convert exception to REST response
	 *
	 * @since 3.0.0
	 *
	 * @param int $status_code HTTP status code.
	 *
	 * @return WP_REST_Response
	 */
	public function to_rest_response( int $status_code = 422 ): WP_REST_Response {
		return new WP_REST_Response(
			array(
				'code'    => $this->getCode(),
				'message' => $this->getMessage(),
				'data'    => array(
					'status' => $status_code,
					'errors' => $this->get_errors(),
				),
			),
			$status_code
		);
	}
}
