<?php

namespace Modules\Reception\Outil;

use Helpers\Filter\FiltreAvance\Generator;
use Helpers\Pagination\PaginatorGestion;
use Helpers\Trad\Trad;
use Modules\Reception\Repository\GestionBR;

class GestionBonRecepDatatable extends Datatable
{
    /** @var GestionBR */
    private GestionBR $repo;

    /** @var Generator */
    private Generator $filter;

    /** @var array<mixed> */
    private $paginationInfo;

    /**
     * @param GestionBR $repo
     * @param Generator $filter
     */
    public function __construct(GestionBR $repo, Generator $filter)
    {
        parent::__construct();

        $this->repo = $repo;
        $this->filter = $filter;
        $this
            ->addColumn(
                'NUM_BR',
                'Référence',
                ['class' => 'table-col-small'],
                function ($row, $data, $key) {
                    return '#' . $data;
                }
            )
            ->addColumn(
                'TYP_BR',
                'Type',
                ['class' => 'table-col-small'],
                [$this, 'renderBonrecepTypeLabel']
            )
            // TODO : use self istead of this
            ->addColumn('CODE_STE', 'Société', [], [$this, 'renderSocieteLabel'])
            ->addColumn('DATE_CRE', 'Enreg. le', ['class' => 'table-col-small'])
            ->addColumn('DATE_VAL', 'Date de validation', ['class' => 'table-col-small'])
        ;
    }

    /**
     * @return Generator
     */
    public function getAdvFilter()
    {
        return $this->filter;
    }

    /**
     * @return array<mixed>
     */
    public function getRawData(): array
    {
        $paginator = new PaginatorGestion();
        $paginator->generate(12, 'NUM_BR', 'ASC');
        $raw_data = $this->repo->getListBonrecep($this->filter);
        $this->paginationInfo = PaginatorGestion::getInfoPagination(); // delete session

        return $raw_data;
    }

    /**
     * @param string $col_key
     * @return mixed|string
     */
    public function getSortDir(string $col_key)
    {
        return $this->filter->getPost()['tri_' . $col_key] ?? '';
    }

    /**
     * @param string $filter_key
     * @return mixed|string
     */
    public function getColFilter(string $filter_key)
    {
        return $this->filter->getPost()['filter_' . $filter_key] ?? '';
    }

    /**
     * @return array<mixed>
     * @throws \Exception
     */
    public function getPaginationInfo()
    {
        if (empty($this->paginationInfo)) {
            throw new \Exception('getRawData must be invoked first');
        }

        return $this->paginationInfo;
    }


    //_____________________________________________________________________________
    // Field rendering

    /**
     * @param array<mixed> $row
     * @param string $value
     * @param string $key
     * @return string
     * @throws \Exception
     */
    public static function renderFournissLabel(array $row, string $value, string $key)
    {
        // Failsafe
        if (!isset($row['CODE_FOUR'])) {
            throw new \Exception('Expected CODE_FOUR');
        }

        return iconv('UTF-8', 'ISO-8859-1', $value)
            . (
            !empty($row['CODE_FOUR']) ?
                ' (' . $row['CODE_FOUR'] . ')' : ''
            );
    }

    /**
     * @param array<mixed> $row
     * @param mixed $value
     * @param string $key
     * @return string
     */
    public static function renderBonrecepTypeLabel(array $row, $value, string $key)
    {
        return $value == 'R' ? 'Réception' : 'Retour';
    }

    /**
     * @param array<mixed> $row
     * @param mixed $value
     * @param string $key
     * @return string
     * @throws \Exception
     */
    public static function renderSocieteLabel(array $row, $value, string $key)
    {

        // Failsafe
        if (!isset($row['CODE_STE'])) {
            throw new \Exception('Expected CODE_STE for "' . var_export($row, true) . '"');
        }

        // Failsafe
        if (!isset($row['RAIS_SOC'])) {
            throw new \Exception('Expected RAIS_SOC');
        }

        return iconv('UTF-8', 'ISO-8859-1', $row['RAIS_SOC'])
            . (
            !empty($row['CODE_STE']) ?
                ' (' . $row['CODE_STE'] . ')' : ''
            );
    }
}
