<?php

namespace App\Models;

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

class MexcoPayment extends Model
{
    public const STATUS_PENDING = 'pending';

    public const STATUS_PAID = 'paid';

    public const STATUS_FAILED = 'failed';

    public const STATUS_REFUNDED = 'refunded';

    public const METHOD_MANUAL = 'manual';

    public const METHOD_GATEWAY = 'gateway';

    public const METHOD_CREDIT = 'credit';

    protected $table = 'mexco_payments';

    protected $fillable = [
        'invoice_id',
        'amount_cents',
        'currency',
        'status',
        'method',
        'gateway_reference',
        'paid_at',
        'metadata',
    ];

    protected function casts(): array
    {
        return [
            'paid_at' => 'datetime',
            'metadata' => 'array',
        ];
    }

    public function invoice(): BelongsTo
    {
        return $this->belongsTo(MexcoInvoice::class, 'invoice_id');
    }

    public function formattedAmount(): string
    {
        return $this->currency.' '.number_format($this->amount_cents / 100, 2);
    }
}
