<?php

namespace App\Helpers;

use App\Models\SystemSetting;

class MoneyHelper
{
    public static function format(
        $amount,
        bool $withSymbol = true
    ): string {
        $settings = SystemSetting::query()->first();

        $symbol = $settings?->currency_symbol ?? '₹';

        $decimals = (int) (
            $settings?->currency_decimal_places ?? 2
        );

        $round = (bool) (
            $settings?->currency_round_display ?? false
        );

        $amount = (float) $amount;

        if ($round) {
            $amount = round($amount);
            $decimals = 0;
        }

        $formatted = number_format(
            $amount,
            $decimals,
            '.',
            ','
        );

        return $withSymbol
            ? $symbol . $formatted
            : $formatted;
    }

    public static function symbol(): string
{
    $settings = SystemSetting::query()->first();

    return $settings?->currency_symbol ?? '₹';
}


    /**
     * Format EMI card number into groups of 4 digits.
     *
     * Example:
     * 1234567890123456
     * → 1234 5678 9012 3456
     */
    public static function formatCardNumber($number): string
    {
        $number = preg_replace(
            '/\s+/',
            '',
            (string) $number
        );

        return implode(
            ' ',
            str_split($number, 4)
        );
    }
}