<?php

namespace App\Domain\Expenses\Models;

use App\Support\Concerns\HasUuid;
use App\Support\Money\Money;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
 * One band of the approval matrix (§40).
 *
 * The amounts live here rather than in code, so an agency can change who signs
 * off what without a deployment.
 */
class ExpenseApprovalRule extends Model
{
    use HasUuid;

    protected $guarded = ['id', 'uuid'];

    protected function casts(): array
    {
        return [
            'up_to_amount' => 'integer',
            'sort_order' => 'integer',
            'is_active' => 'boolean',
        ];
    }

    /** A null ceiling is the catch-all band at the top of the matrix. */
    public function covers(int $amount): bool
    {
        return $this->up_to_amount === null || $amount <= $this->up_to_amount;
    }

    public function ceilingLabel(): string
    {
        return $this->up_to_amount === null
            ? 'Above every other band'
            : 'Up to '.Money::of($this->up_to_amount, $this->currency)->format();
    }

    public function scopeActive(Builder $query): Builder
    {
        return $query->where('is_active', true)->orderBy('sort_order')->orderBy('id');
    }
}
