<?php

namespace App\Resources;

use App\Models\AvailablePaymentMethod;
use App\Models\PaymentMethod;
use Illuminate\Http\Resources\Json\JsonResource;

class AvailablePaymentMethodResource extends JsonResource {
    private AvailablePaymentMethod $availablePaymentMethod;

    public function __construct(AvailablePaymentMethod $availablePaymentMethod) {
        parent::__construct($availablePaymentMethod);
        $this->availablePaymentMethod = $availablePaymentMethod;
    }

    public function toArray($request) : array {
        // Wird die Instanz über Event::paymentMethods() geladen, liegt die pro-Event-Konfiguration
        // im Pivot; ansonsten (Tenant-Kontext) gilt die Konfiguration der Instanz selbst.
        $pivot = $this->availablePaymentMethod->pivot ?? null;
        $configuration = $pivot !== null
            ? ($pivot->configuration ?? [])
            : ($this->availablePaymentMethod->configuration ?? []);

        return [
            'id' => $this->availablePaymentMethod->id,
            'slug' => $this->availablePaymentMethod->slug,
            'name' => $this->availablePaymentMethod->name,
            'description' => $this->availablePaymentMethod->description,
            'active' => $this->availablePaymentMethod->active,
            'configuration' => (object) $configuration,
            'optionsSchema' => PaymentMethod::optionsFor($this->availablePaymentMethod->slug),
            'participantOptionsSchema' => PaymentMethod::participantOptionsFor($this->availablePaymentMethod->slug),
        ];
    }
}
