<?php

declare(strict_types=1);

namespace App\Framework\View\Table\Formatters;

use App\Framework\View\Table\CellFormatter;

final readonly class CurrencyFormatter implements CellFormatter
{
    public function __construct(
        private string $currency = 'EUR',
        private int $decimals = 2,
        private string $decimalSeparator = ',',
        private string $thousandsSeparator = '.'
    ) {
    }

    public function format(mixed $value): string
    {
        if ($value === null || $value === '') {
            return '';
        }

        $numericValue = is_numeric($value) ? (float) $value : 0;

        $formatted = number_format(
            $numericValue,
            $this->decimals,
            $this->decimalSeparator,
            $this->thousandsSeparator
        );

        return match ($this->currency) {
            'EUR' => $formatted . ' €',
            'USD' => '$ ' . $formatted,
            'GBP' => '£ ' . $formatted,
            default => $formatted . ' ' . $this->currency
        };
    }

    public static function euro(): self
    {
        return new self('EUR', 2, ',', '.');
    }

    public static function dollar(): self
    {
        return new self('USD', 2, '.', ',');
    }

    public static function cents(): self
    {
        return new self('EUR', 0, ',', '.');
    }
}
