<?php

namespace App\Models;

use App\Models\Concerns\BelongsToOwner;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class PaymentGatewaySetting extends Model
{
    use BelongsToOwner;

    public const PROVIDER_PAYSTACK = 'paystack';

    public const PROVIDER_FLUTTERWAVE = 'flutterwave';

    public const PROVIDER_HUBTEL = 'hubtel';

    protected $table = 'care_payment_gateway_settings';

    protected $fillable = [
        'owner_ref',
        'organization_id',
        'provider',
        'public_key',
        'secret_key',
        'webhook_secret',
        'is_active',
        'metadata',
    ];

    protected function casts(): array
    {
        return [
            'public_key' => 'encrypted',
            'secret_key' => 'encrypted',
            'webhook_secret' => 'encrypted',
            'is_active' => 'boolean',
            'metadata' => 'array',
        ];
    }

    public function organization(): BelongsTo
    {
        return $this->belongsTo(Organization::class, 'organization_id');
    }

    public function isConfigured(): bool
    {
        if (! $this->is_active) {
            return false;
        }

        $secret = trim((string) $this->secret_key);

        return $secret !== '' && in_array($this->provider, [
            self::PROVIDER_PAYSTACK,
            self::PROVIDER_FLUTTERWAVE,
            self::PROVIDER_HUBTEL,
        ], true);
    }

    /** @return list<string> */
    public static function providers(): array
    {
        return [
            self::PROVIDER_PAYSTACK,
            self::PROVIDER_FLUTTERWAVE,
            self::PROVIDER_HUBTEL,
        ];
    }
}
