<?php

use App\Models\Magento\DealerModel;

if (! function_exists('get_dealer_options')) {
    /**
     * Retourne la liste des dealers actifs pour un select filtre.
     *
     * @return array<int, string>
     */
    function get_dealer_options(): array
    {
        $model = new DealerModel();
        $dealers = $model->getDealers();
        $options = [];

        foreach ($dealers as $dealer) {
            $options[(int) $dealer['dealer_id']] = $dealer['admin_fullname'];
        }

        return $options;
    }
}

if (! function_exists('get_dealer_id_from_request')) {
    /**
     * Retourne l'ID dealer passé en GET/POST, ou null.
     */
    function get_dealer_id_from_request(): ?int
    {
        $dealerId = service('request')->getGet('dealer_id') ?? service('request')->getPost('dealer_id');
        $dealerId = (int) $dealerId;

        return $dealerId > 0 ? $dealerId : null;
    }
}

if (! function_exists('get_dealer_customer_ids')) {
    /**
     * Retourne les IDs client d'un dealer.
     *
     * @return int[]
     */
    function get_dealer_customer_ids(int $dealerId): array
    {
        $model = new DealerModel();
        return $model->getCustomerIdsByDealer($dealerId);
    }
}
