<?php

namespace App\Services\Payments;

/** Where to send the applicant after starting a payment. */
class PaymentInitialization
{
    public function __construct(
        public readonly bool $successful,
        public readonly ?string $redirectUrl = null,
        public readonly ?string $providerReference = null,
        public readonly ?string $instructions = null,
        public readonly array $metadata = [],
        public readonly ?string $error = null,
    ) {}

    public static function redirect(string $url, ?string $reference = null, array $metadata = []): self
    {
        return new self(true, redirectUrl: $url, providerReference: $reference, metadata: $metadata);
    }

    /** Provider needs no redirect: show the applicant what to do instead. */
    public static function inline(string $instructions, ?string $reference = null, array $metadata = []): self
    {
        return new self(true, providerReference: $reference, instructions: $instructions, metadata: $metadata);
    }

    public static function failed(string $error): self
    {
        return new self(false, error: $error);
    }
}
