<?php

namespace App\Models;

use App\Models\Concerns\BelongsToMerchant;
use Illuminate\Database\Eloquent\Model;

class Invoice extends Model
{
    use BelongsToMerchant;

    protected $fillable = [
        'public_id',
        'merchant_id',
        'environment',
        'order_id',
        'price_amount_minor',
        'price_currency',
        'pay_asset_id',
        'quoted_pay_amount_atomic',
        'paid_amount_atomic',
        'quote_rate',
        'quote_expires_at',
        'ipn_callback_url',
        'success_url',
        'cancel_url',
        'status',
        'expires_at',
        'metadata',
        'sub_merchant_id',
        'customer_id',
        'payer_id',
        'payer_name',
        'payer_email',
        'accept_partial',
        'accept_overpayment',
        'underpayment_tolerance_atomic',
        'overpayment_tolerance_atomic',
        'late_payment_policy',
        'locale',
    ];

    protected function casts(): array
    {
        return [
            'expires_at' => 'datetime',
            'quote_expires_at' => 'datetime',
            'metadata' => 'array',
            'accept_partial' => 'boolean',
            'accept_overpayment' => 'boolean',
        ];
    }

    public function merchant()
    {
        return $this->belongsTo(Merchant::class);
    }

    public function asset()
    {
        return $this->belongsTo(Asset::class, 'pay_asset_id');
    }

    public function address()
    {
        return $this->hasOne(WalletAddress::class);
    }

    public function items()
    {
        return $this->hasMany(InvoiceItem::class);
    }

    public function subMerchant()
    {
        return $this->belongsTo(Merchant::class, 'sub_merchant_id');
    }

    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }

    public function invoiceTransactions()
    {
        return $this->hasMany(InvoiceTransaction::class);
    }
}
