<?php

namespace App\Models;

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

class PartnerWalletTransaction extends Model
{
    use HasFactory;

    const TYPE_TOPUP = 'topup';
    const TYPE_DEDUCTION = 'deduction';

    const REFERENCE_FOOD_ORDER = 'food_order';
    const REFERENCE_GAS_ORDER = 'gas_order';
    const REFERENCE_TOPUP = 'topup';
    const REFERENCE_TRIP_REQUEST = 'trip_request';
    const REFERENCE_TAXI_REQUEST = 'taxi_request';

    const STATUS_PENDING = 'pending';
    const STATUS_COMPLETED = 'completed';
    const STATUS_FAILED = 'failed';
    const STATUS_REFUNDED = 'refunded';

    protected $fillable = [
        'partner_wallet_id',
        'type',
        'amount',
        'balance_before',
        'balance_after',
        'payment_method',
        'reference_type',
        'reference_id',
        'description',
        'meta',
        'status',
        'external_id',
    ];

    protected $casts = [
        'amount' => 'decimal:2',
        'balance_before' => 'decimal:2',
        'balance_after' => 'decimal:2',
        'meta' => 'array',
    ];

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

    public function isCredit(): bool
    {
        return $this->type === self::TYPE_TOPUP;
    }

    public function isDebit(): bool
    {
        return $this->type === self::TYPE_DEDUCTION;
    }
}
