<?php

namespace App\Models;

use App\Models\Concerns\SyncsToCloud;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Expense extends Model
{
    use HasFactory;
    use SyncsToCloud;

    protected $fillable = [
        'uuid',
        'expense_date',
        'category_id',
        'description',
        'payment_method',
        'remark',
        'amount',
        'sort_order',
    ];

    protected $casts = [
        'expense_date' => 'date',
        'amount' => 'decimal:2',
    ];

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

    public function categoryName(): string
    {
        return $this->category?->name ?? '—';
    }

    public function categoryIcon(): string
    {
        return $this->category?->icon() ?? config('tracker.default_category_icon');
    }

    public function categoryTint(): int
    {
        return $this->category?->tint() ?? 8;
    }

    public function paymentIcon(): string
    {
        return config('tracker.payment_icons')[$this->payment_method] ?? 'wallet';
    }

    /** Whole taka stay whole (as in the sheet); paisa show two decimals. */
    public static function money($amount): string
    {
        $amount = (float) $amount;

        return number_format($amount, fmod($amount, 1.0) === 0.0 ? 0 : 2);
    }
}
