<?php
namespace ComptaFlowUK\Services;

defined('ABSPATH') || exit;

class AccountLabels
{
    /**
     * Retrieve the label of an account from ChartOfAccounts
     */
    public static function getLabel(string $account_num): string
    {
        require_once(COMPTAFLOW_UK_PLUGIN_DIR . 'src/Services/ChartOfAccounts.php');
        
        // Recherche exacte
        $account = ChartOfAccounts::getAccount($account_num);
        if ($account) {
            return $account['label'];
        }
        
        // Search by prefix (e.g. 6061 → searches 606)
        for ($len = strlen($account_num) - 1; $len >= 2; $len--) {
            $prefix = substr($account_num, 0, $len);
            $account = ChartOfAccounts::getAccount($prefix);
            if ($account) {
                return $account['label'];
            }
        }
        
        // Fallback
        return 'Account ' . $account_num;
    }
}