<?php
class NumberToWordsFR
{
    private static $units = [
        0 => 'zéro', 1 => 'un', 2 => 'deux', 3 => 'trois', 4 => 'quatre',
        5 => 'cinq', 6 => 'six', 7 => 'sept', 8 => 'huit', 9 => 'neuf',
        10 => 'dix', 11 => 'onze', 12 => 'douze', 13 => 'treize', 14 => 'quatorze',
        15 => 'quinze', 16 => 'seize', 17 => 'dix-sept', 18 => 'dix-huit', 19 => 'dix-neuf'
    ];

    private static $tens = [
        20 => 'vingt', 30 => 'trente', 40 => 'quarante',
        50 => 'cinquante', 60 => 'soixante', 70 => 'soixante-dix',
        80 => 'quatre-vingt', 90 => 'quatre-vingt-dix'
    ];

    /**
     * Convertit un nombre décimal en lettres (avec francs CFA + centimes)
     */
    public static function convert($number)
    {
        if (!is_numeric($number)) {
            return 'Valeur non valide';
        }

        $number = (float) $number;

        $integerPart = floor($number);
        $decimalPart = round(($number - $integerPart) * 100);

        $words = self::convertNumber($integerPart) . ' franc' . ($integerPart > 1 ? 's' : '') . ' CFA';

        if ($decimalPart > 0) {
            $words .= ' et ' . self::convertNumber($decimalPart) . ' centime' . ($decimalPart > 1 ? 's' : '');
        }

        return ucfirst(trim($words));
    }

    /**
     * Conversion générique selon la taille du nombre
     */
    private static function convertNumber($number)
    {
        if ($number < 20) {
            return self::$units[$number];
        } elseif ($number < 100) {
            return self::convertTens($number);
        } elseif ($number < 1000) {
            return self::convertHundreds($number);
        } elseif ($number < 1000000) {
            return self::convertThousands($number);
        } elseif ($number < 1000000000) {
            return self::convertMillions($number);
        } else {
            return self::convertBillions($number);
        }
    }

    /**
     * Gestion des dizaines (20–99)
     */
    private static function convertTens($number)
    {
        if ($number < 20) {
            return self::$units[$number];
        }

        $tens = floor($number / 10) * 10;
        $unit = $number % 10;

        // Cas particuliers du français (21, 31, 41, etc.)
        if ($unit === 1 && $tens !== 80) {
            return self::$tens[$tens] . '-et-un';
        } elseif ($unit > 0) {
            return self::$tens[$tens] . '-' . self::$units[$unit];
        } else {
            return self::$tens[$tens];
        }
    }

    /**
     * Gestion des centaines (100–999)
     */
    private static function convertHundreds($number)
    {
        $hundreds = floor($number / 100);
        $remainder = $number % 100;

        if ($hundreds === 0) {
            return self::convertNumber($remainder);
        }

        $words = ($hundreds > 1 ? self::$units[$hundreds] . ' cent' : 'cent');

        // Accord du pluriel
        if ($hundreds > 1 && $remainder === 0) {
            $words .= 's';
        }

        if ($remainder > 0) {
            $words .= ' ' . self::convertNumber($remainder);
        }

        return $words;
    }

    /**
     * Gestion des milliers (1 000 – 999 999)
     */
    private static function convertThousands($number)
    {
        $thousands = floor($number / 1000);
        $remainder = $number % 1000;

        $words = ($thousands > 1 ? self::convertNumber($thousands) . ' mille' : 'mille');

        if ($remainder > 0) {
            $words .= ' ' . self::convertNumber($remainder);
        }

        return $words;
    }

    /**
     * Gestion des millions
     */
    private static function convertMillions($number)
    {
        $millions = floor($number / 1000000);
        $remainder = $number % 1000000;

        $words = ($millions > 1 ? self::convertNumber($millions) . ' millions' : 'un million');

        if ($remainder > 0) {
            $words .= ' ' . self::convertNumber($remainder);
        }

        return $words;
    }

    /**
     * Gestion des milliards
     */
    private static function convertBillions($number)
    {
        $billions = floor($number / 1000000000);
        $remainder = $number % 1000000000;

        $words = ($billions > 1 ? self::convertNumber($billions) . ' milliards' : 'un milliard');

        if ($remainder > 0) {
            $words .= ' ' . self::convertNumber($remainder);
        }

        return $words;
    }
}
