<?php

namespace App\Models;

use App\Enums\CreditLedgerEntryType;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class CreditLedgerEntry extends Model
{
    /** @use HasFactory<\Database\Factories\CreditLedgerEntryFactory> */
    use HasFactory, HasUlids;

    protected $fillable = [
        'user_id',
        'credit_product_id',
        'type',
        'credits',
        'balance_after',
        'source',
        'stripe_checkout_session_id',
        'stripe_invoice_id',
        'stripe_payment_intent_id',
        'stripe_subscription_id',
        'metadata',
    ];

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

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

    protected function casts(): array
    {
        return [
            'type' => CreditLedgerEntryType::class,
            'credits' => 'integer',
            'balance_after' => 'integer',
            'metadata' => 'array',
        ];
    }
}
