<?php

declare (strict_types=1);
namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Violations;

use WP_REST_Response;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Violations\Enums\MessageCode;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Violations\Enums\MessageSeverity;
/**
 * Represents a UCP error response.
 *
 * This class wraps a single Violation with an HTTP status code for use in REST responses.
 */
final class CheckoutError
{
    private int $http_status;
    private Violation $violation;
    private function __construct(int $http_status, Violation $violation)
    {
        $this->http_status = $http_status;
        $this->violation = $violation;
    }
    public function get_content(): string
    {
        return $this->violation->to_array()['content'] ?? '';
    }
    public static function checkout_not_found(string $id): self
    {
        return new self(404, Violation::create(MessageCode::MISSING, "Checkout session not found: {$id}", MessageSeverity::RECOVERABLE));
    }
    public static function header_missing(string $header_name): self
    {
        return new self(400, Violation::create(MessageCode::MISSING, "Required header missing: {$header_name}", MessageSeverity::RECOVERABLE));
    }
    public static function version_unsupported(string $requested, string $supported): self
    {
        return new self(400, Violation::create(MessageCode::INVALID, "UCP version '{$requested}' is not supported. Supported versions: {$supported}", MessageSeverity::RECOVERABLE));
    }
    public static function idempotency_conflict(): self
    {
        return new self(409, Violation::create(MessageCode::IDEMPOTENCY_CONFLICT, 'Idempotency key conflict — a request with this key already returned a different response', MessageSeverity::RECOVERABLE));
    }
    public static function invalid_request(string $code, string $content, ?string $path = null): self
    {
        return new self(400, Violation::create($code, $content, MessageSeverity::RECOVERABLE, $path));
    }
    public static function checkout_already_completed(): self
    {
        return new self(409, Violation::create(MessageCode::INVALID_STATE, 'Checkout is already completed', MessageSeverity::RECOVERABLE));
    }
    public static function checkout_already_canceled(): self
    {
        return new self(409, Violation::create(MessageCode::INVALID_STATE, 'Checkout is already canceled', MessageSeverity::RECOVERABLE));
    }
    public static function checkout_conflict(string $status): self
    {
        return new self(409, Violation::create(MessageCode::INVALID_STATE, "Checkout cannot be modified in '{$status}' state", MessageSeverity::RECOVERABLE));
    }
    public static function checkout_not_ready(string $status): self
    {
        return new self(400, Violation::create(MessageCode::INVALID_STATE, "Checkout session is not ready for completion. Current status: {$status}", MessageSeverity::RECOVERABLE));
    }
    public static function processing_error(string $code, string $content, ?string $path = null): self
    {
        return new self(500, Violation::create($code, $content, MessageSeverity::RECOVERABLE, $path));
    }
    public static function payment_failed(string $content): self
    {
        return new self(402, Violation::create('payment_failed', $content, MessageSeverity::RECOVERABLE));
    }
    public function to_rest_response(): WP_REST_Response
    {
        return new WP_REST_Response(['status' => 'requires_escalation', 'messages' => [$this->violation->to_array()]], $this->http_status);
    }
}
