<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class Payment extends Model
{
    protected $guarded = [];

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

    public function payable(): MorphTo
    {
        return $this->morphTo();
    }

    public function markPaid(?string $gatewayReference = null): void
    {
        $this->forceFill([
            'status' => 'paid',
            'paid_at' => now(),
            'gateway_reference' => $gatewayReference ?? $this->gateway_reference,
        ])->save();
    }
}
