<?php

declare(strict_types=1);


namespace Plugins\Neatstudio\Payment\Models;

use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Payment extends Model
{
    protected $fillable = [
        'order_no', 'user_id', 'subject', 'description', 'amount', 'channel', 'status', 'gateway_trade_no', 'payload', 'paid_at',
    ];

    protected $casts = [
        'payload' => 'array',
        'paid_at' => 'datetime',
    ];

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function markPaid(string $gatewayTradeNo = null): void
    {
        $this->update([
            'status' => 'paid',
            'gateway_trade_no' => $gatewayTradeNo,
            'paid_at' => now(),
        ]);

        app(\App\Blog\Support\PluginManager::class)->doAction('payment.paid', $this);
    }
}
