<?php

namespace App\Models\Reference;

use Illuminate\Database\Eloquent\Model;

/**
 * Seeded, platform-wide currency reference data (see ReferenceDataSeeder) —
 * global like {@see \App\Models\Geo\City}, never tenant-scoped. A tenant's
 * Master Control "Locale" tab picks ONE of these by `code`; the symbol and
 * decimal precision it carries are what get copied onto that tenant's
 * settings row, so nobody types a currency symbol by hand.
 */
class Currency extends Model
{
    protected $fillable = ['code', 'name', 'symbol', 'decimal_digits', 'sort_order'];

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

    public static function findByCode(string $code): ?self
    {
        return static::query()->where('code', strtoupper($code))->first();
    }

    /**
     * @return array<string, string>
     */
    public static function selectOptions(): array
    {
        return static::query()
            ->orderBy('sort_order')
            ->orderBy('name')
            ->get()
            ->mapWithKeys(fn (self $currency): array => [
                $currency->code => "{$currency->code} — {$currency->name} ({$currency->symbol})",
            ])
            ->all();
    }
}
