<?php

namespace App\Models;

use App\Models\Concerns\InteractsWithActivityLog;
use Illuminate\Database\Eloquent\Attributes\Appends;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

#[Guarded(['id'])]
#[Appends(['balance_formatted'])]
class CashAccount extends Model
{
    // 1. Use Trait
    use HasFactory, InteractsWithActivityLog;

    // 2. Casting
    protected function casts(): array
    {
        return [
            'balance' => 'integer',
        ];
    }

    // 3. Attribute
    public function balanceFormatted(): Attribute
    {
        return Attribute::make(
            get: fn () => 'Rp '.number_format($this->balance, 0, ',', '.'),
        );
    }

    // 4. Relation
    public function transactions(): HasMany
    {
        return $this->hasMany(CashTransaction::class);
    }
}
