<?php
/**
 * BookiFlex Cart Extension
 *
 * @package    BookiFlex
 * @subpackage Cart
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */

declare(strict_types=1);

namespace BookiFlex\Extensions\Cart\Service\DTO;

use LogicException;

defined( 'ABSPATH' ) || exit;

class CheckoutResult
{
	private function __construct(
		public readonly bool $success,
		public readonly ?array $reservations,
		public readonly array $errors,
	) {
	}

	public static function success(array $reservations): self
	{
		return new self(
			success: true,
			reservations: $reservations,
			errors: []
		);
	}

	public static function failure(array $errors): self
	{
		return new self(
			success: false,
			reservations: null,
			errors: $errors
		);
	}

	public function isSuccess(): bool
	{
		return $this->success;
	}

	public function getReservations(): array
	{
		if (!$this->success) {
			throw new LogicException('Cannot get reservations from failed checkout result');
		}

		return $this->reservations;
	}

	public function getErrors(): array
	{
		return $this->errors;
	}
}
