<?php

namespace App\Models\PaymentManagement;

use App\Models\OrderManagement\Order;
use App\Models\Traits\CanUploadFiles;
use App\Models\Traits\Activable;
use App\Models\Traits\HasMedias;
use Axn\EloquentAuthorable\AuthorableTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\File;
use Nicolaslopezj\Searchable\SearchableTrait;
use Spatie\MediaLibrary\HasMedia as SpatieHasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;

class PaymentMethod extends Model{
    use HasFactory, SearchableTrait,Activable,
        HasMedias,AuthorableTrait;

    const MTN_MOMO_CMR = "mtn-momo-cmr";
    const ORANGE_MOMO_CMR = "orange-momo-cmr";
    const PAYPAL_REST = "paypal-rest";
    const CASH = "cash";

    protected $guarded = [];
    protected $table = "payment_mgt__methods";

    protected $searchable = [
        'columns' => [
            'payment_methods.name' => 10,
            'payment_methods.code' => 10,
        ],
    ];

    protected $casts = [
        "api_config" => "array",
    ];

    protected $appends = [
        'image'
    ];

    public function getRouteKeyName(){
        return "id";
    }

    public function orders(): HasMany{
        return $this->hasMany(Order::class,"payment_method_id");
    }

    public function getImageAttribute(){
        $logo_file_name = $this->logo_file_name;
        $filePath = "images/payments-methods/$logo_file_name";
        return ( filled($logo_file_name) and File::exists(public_path($filePath)))
               ? url($filePath)
               : url("images/payments-methods/default.jpg");
    }
}
