<?php

namespace App\Models;

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\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

#[Appends(['formatted_balance', 'formatted_name'])]
#[Guarded(['id'])]
class CashAccount extends Model
{
    use HasFactory, SoftDeletes;

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

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

    protected function formattedName(): Attribute
    {
        return Attribute::make(
            get: fn () => ucfirst($this->name),
        );
    }

    public function cashTransactions(): HasMany
    {
        return $this->hasMany(CashTransaction::class);
    }

    public function createdBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by_id');
    }
}
