<?php

namespace App\Modules\TurkmenNumberFormatter\Repositories;

class TurkmenNumberFormatter
{
    /**
     * Units
     *
     * @var array<int, string>
     */
    private static array $units = ['', 'bir', 'iki', 'üç', 'dört', 'bäş', 'alty', 'ýedi', 'sekiz', 'dokuz'];

    /**
     * Tens
     *
     * @var array<int, string>
     */
    private static array $tens = ['', 'on', 'ýigrimi', 'otuz', 'kyrk', 'elli', 'altmyş', 'ýetmiş', 'segsen', 'togsan'];

    /**
     * Hundreds
     *
     * @var array<int, string>
     */
    private static array $hundreds = ['', 'ýüz', 'iki ýüz', 'üç ýüz', 'dört ýüz', 'bäş ýüz', 'alty ýüz', 'ýedi ýüz', 'sekiz ýüz', 'dokuz ýüz'];

    /**
     * Large mumbers
     *
     * @var array<int, string>
     */
    private static array $largeNumbers = ['', 'müň', 'million', 'milliard', 'trillion'];

    /**
     * Main method to format a given amount in Turkmen.
     *
     * @param  float  $amount  The amount to format.
     * @return string The amount in written Turkmen format.
     */
    public static function format(float $amount): string
    {
        // Split the amount into whole and fractional parts
        [$whole, $fraction] = explode('.', number_format($amount, 2, '.', ''));

        // Convert the whole part
        $wholeWords = self::convertWholePart(intval($whole));

        // Convert the fractional part
        $fractionWords = self::convertFractionalPart(intval($fraction));

        return trim($wholeWords.' '.$fractionWords);
    }

    /**
     * Convert the whole part of the number.
     *
     * @param  int  $whole  The whole part of the number.
     * @return string The converted whole part in Turkmen.
     */
    private static function convertWholePart(int $whole): string
    {
        $words = '';
        $level = 0;

        while ($whole > 0) {
            $part = $whole % 1000;
            $whole = (int) ($whole / 1000);

            if ($part > 0) {
                $words = self::convertToWords($part).' '.self::$largeNumbers[$level].' '.$words;
            }
            $level++;
        }

        return trim($words).' manat';
    }

    /**
     * Convert the fractional part of the number.
     *
     * @param  int  $fraction  The fractional part of the number.
     * @return string The converted fractional part in Turkmen.
     */
    private static function convertFractionalPart(int $fraction): string
    {
        if ($fraction <= 0) {
            return '';
        }

        $fractionTens = (int) ($fraction / 10);
        $fractionUnits = $fraction % 10;

        $words = '';
        $words .= ($fractionTens > 0 ? self::$tens[$fractionTens].' ' : '');
        $words .= ($fractionUnits > 0 ? self::$units[$fractionUnits].' ' : '');

        return trim($words).' teňňe';
    }

    /**
     * Convert a number up to 999 to Turkmen words.
     *
     * @param  int  $num  The number to convert.
     * @return string The converted number in Turkmen.
     */
    private static function convertToWords(int $num): string
    {
        $words = '';

        if ($num >= 100) {
            $hundredsPart = (int) ($num / 100);
            $num %= 100;
            $words .= self::$hundreds[$hundredsPart].' ';
        }

        if ($num >= 10) {
            $tensPart = (int) ($num / 10);
            $num %= 10;
            $words .= self::$tens[$tensPart].' ';
        }

        if ($num > 0) {
            $words .= self::$units[$num].' ';
        }

        return trim($words);
    }
}
