<?php

namespace App\Models;

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

class UserCredit extends Model
{
    use HasFactory;

    const TYPE_SUBSCRIPTION = 'subscription';

    const TYPE_ALACARTE = 'alacarte';

    protected $fillable = [
        'user_id',
        'subscription_credits',
        'alacarte_credits',
        'spend_subscription_first',
    ];

    protected $casts = [
        'spend_subscription_first' => 'boolean',
    ];

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

    public function transactions(): HasMany
    {
        return $this->hasMany(UserCreditTransaction::class, 'user_id', 'user_id')
            ->orderBy('created_at', 'desc');
    }
}
