<?php

namespace App\Models\SelfPaced;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Crypt;

/**
 * PaystackConfig Model
 *
 * Stores Paystack payment gateway configuration including encrypted credentials.
 * One active record per portal.
 *
 * Requirements: 1.1, 10.3
 */
class PaystackConfig extends Model
{
    protected $table = 'self_paced_paystack_configs';

    protected $fillable = [
        'public_key',
        'secret_key',
        'webhook_secret',
        'currencies',
        'is_active',
        'created_by',
        'updated_by',
    ];

    protected $casts = [
        'currencies' => 'array',
        'is_active' => 'boolean',
    ];

    // =====================================================================
    // Encryption/Decryption Accessors and Mutators
    // =====================================================================

    public function getPublicKeyAttribute($value)
    {
        try {
            return Crypt::decrypt($value);
        } catch (\Exception $e) {
            return $value;
        }
    }

    public function setPublicKeyAttribute($value)
    {
        $this->attributes['public_key'] = Crypt::encrypt($value);
    }

    public function getSecretKeyAttribute($value)
    {
        try {
            return Crypt::decrypt($value);
        } catch (\Exception $e) {
            return $value;
        }
    }

    public function setSecretKeyAttribute($value)
    {
        $this->attributes['secret_key'] = Crypt::encrypt($value);
    }

    public function getWebhookSecretAttribute($value)
    {
        try {
            return Crypt::decrypt($value);
        } catch (\Exception $e) {
            return $value;
        }
    }

    public function setWebhookSecretAttribute($value)
    {
        $this->attributes['webhook_secret'] = Crypt::encrypt($value);
    }

    // =====================================================================
    // Methods
    // =====================================================================

    /**
     * Get masked public key for display (first 7 + **** + last 4)
     *
     * @return string
     */
    public function getMaskedPublicKey(): string
    {
        $key = $this->public_key;
        if (strlen($key) <= 11) {
            return str_repeat('*', strlen($key));
        }
        return substr($key, 0, 7) . '****' . substr($key, -4);
    }

    /**
     * Get masked secret key for display (first 7 + **** + last 4)
     *
     * @return string
     */
    public function getMaskedSecretKey(): string
    {
        $key = $this->secret_key;
        if (strlen($key) <= 11) {
            return str_repeat('*', strlen($key));
        }
        return substr($key, 0, 7) . '****' . substr($key, -4);
    }

    /**
     * Get the active Paystack configuration
     *
     * @return static|null
     */
    public static function getActive(): ?static
    {
        return static::where('is_active', true)->first();
    }

    /**
     * Check if a currency is supported by this configuration
     *
     * @param string $currency ISO 4217 currency code (e.g., 'KES', 'USD')
     * @return bool
     */
    public function supportsCurrency(string $currency): bool
    {
        return in_array(strtoupper($currency), $this->currencies ?? ['KES']);
    }
}
