<?php

namespace App\Models\PaymentManagement;

use App\Models\SettingManagement\PaymentTransactionStatus;
use App\Models\SettingManagement\Traits\Statuable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class PaymentTransaction extends Model{
    use HasFactory,Statuable;

    protected $guarded = ['id',"created_at"];
    protected $table = "payment_mgt__transactions";
    const MORPH_ID_COLUMN = "payer_id";
    const MORPH_TYPE_COLUMN = "payer_type";
    const MORPH_FUNCTION_NAME = "payer";

    protected $casts = [
      "order" => "array",
      "payment_result" => "array",
    ];

    protected $dates = [
        "workout_at"
    ];

    public function getStatusClass(){
        return PaymentTransactionStatus::class;
    }

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

    protected static function boot(){
        parent::boot();

        // Quand la transaction est crée
        static::creating(function ($transaction) {
            $transaction->setAttribute("status_code",PaymentTransactionStatus::UNDER_PROCESS);
            $transaction->setAttribute("workout_at",now());
        });
    }



    //
    public function payer(){
        return $this->morphTo(__FUNCTION__,
            self::MORPH_TYPE_COLUMN,self::MORPH_ID_COLUMN);
    }

    public function paymentMethod(){
        return $this->belongsTo(PaymentMethod::class,"payment_method_id");
    }

}
