<?php

namespace App\Models;

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

class UserCreditTransaction extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'credit_type',
        'operation',
        'amount',
        'subscription_balance_after',
        'alacarte_balance_after',
        'description',
        'metadata',
    ];

    protected $casts = [
        'metadata' => 'array',
    ];

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

    public function userCredit(): BelongsTo
    {
        return $this->belongsTo(UserCredit::class, 'user_id', 'user_id');
    }

    public function scopeDeposits($query)
    {
        return $query->where('operation', 'deposit');
    }

    public function scopeSpending($query)
    {
        return $query->where('operation', 'spend');
    }

    public function isCredit(): bool
    {
        return $this->amount > 0;
    }

    public function isDebit(): bool
    {
        return $this->amount < 0;
    }
}
