<?php

namespace App\Models;

use App\Scopes\InstancedModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
 * @property int $id
 * @property string $tenant
 * @property string $slug
 * @property string $name
 * @property ?string $description
 * @property bool $active
 * @property ?array $configuration
 */
class AvailablePaymentMethod extends InstancedModel
{
    protected $table = 'available_payment_methods';

    protected $fillable = [
        'tenant',
        'slug',
        'name',
        'description',
        'active',
        'configuration',
    ];

    protected $casts = [
        'active' => 'boolean',
        'configuration' => 'array',
    ];

    public function paymentMethod(): BelongsTo
    {
        return $this->belongsTo(PaymentMethod::class, 'slug', 'slug');
    }

    /**
     * Sind alle Pflicht-Optionen dieses Moduls (siehe Options-Schema des Zahlungsmoduls) befüllt?
     */
    public function isComplete(): bool
    {
        return PaymentMethod::isConfigurationComplete($this->slug, $this->configuration ?? []);
    }

    public function events(): BelongsToMany
    {
        // Pivot verknüpft über den Slug -- Event::paymentMethods() greift daher automatisch nur
        // auf Events des aktuell gebundenen Tenants zu (SiteScope auf Event + AvailablePaymentMethod).
        return $this->belongsToMany(Event::class, 'event_payment_methods', 'slug', 'event_id', 'slug', 'id')->withTimestamps();
    }
}
