<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class WithdrawalMethod extends Model
{
    use HasFactory;

    protected $fillable = [
        'slug',
        'name',
        'symbol',
        'currency_type',
        'type',
        'supported_currencies',
        'minimum',
        'maximum',
        'charges_fixed',
        'charges_percentage',
        'processing_duration',
        'is_active',
    ];

    protected $casts = [
        'supported_currencies' => 'array',
        'minimum' => 'decimal:2',
        'maximum' => 'decimal:2',
        'charges_fixed' => 'decimal:2',
        'charges_percentage' => 'decimal:2',
        'is_active' => 'boolean',
    ];

    /**
     * Calculate withdrawal charge
     */
    public function calculateCharge($amount)
    {
        $percentageCharge = ($amount * $this->charges_percentage) / 100;
        return $percentageCharge + $this->charges_fixed;
    }

    /**
     * Get net amount after charges
     */
    public function getNetAmount($amount)
    {
        return $amount - $this->calculateCharge($amount);
    }

    /**
     * Check if amount is within limits
     */
    public function isWithinLimits($amount)
    {
        return $amount >= $this->minimum && $amount <= $this->maximum;
    }

    /**
     * Scope: Active methods
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * Scope: External methods
     */
    public function scopeExternal($query)
    {
        return $query->where('type', 'external');
    }

    /**
     * Scope: Internal methods
     */
    public function scopeInternal($query)
    {
        return $query->where('type', 'internal');
    }

    public function minAmount(){
        return $this->minimum * 1;
    }

    public function maxAmount(){
        return $this->maximum * 1;
    }
}
