<?php
/**
 * _             _       _
 * | |____      _| | __ _| |__
 * | '_ \ \ /\ / / |/ _` | '_ \
 * | |_) \ V  V /| | (_| | |_) |
 * |_.__/ \_/\_/ |_|\__,_|_.__/.
 *
 * @author    Bwlab di Luigi Massa <info@bwlab.it>
 * @copyright since 2014 Bwlab
 * @license   proprietary
 */

declare(strict_types=1);

namespace Bwlab\Withdrawal\Grid\Definition\Factory;

if (!defined('_PS_VERSION_')) {
    exit;
}

use PrestaShop\PrestaShop\Core\Grid\Action\Row\RowActionCollection;
use PrestaShop\PrestaShop\Core\Grid\Action\Row\Type\LinkRowAction;
use PrestaShop\PrestaShop\Core\Grid\Column\ColumnCollection;
use PrestaShop\PrestaShop\Core\Grid\Column\Type\Common\ActionColumn;
use PrestaShop\PrestaShop\Core\Grid\Column\Type\DataColumn;
use PrestaShop\PrestaShop\Core\Grid\Definition\Factory\AbstractGridDefinitionFactory;
use PrestaShop\PrestaShop\Core\Grid\Filter\Filter;
use PrestaShop\PrestaShop\Core\Grid\Filter\FilterCollection;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class WithdrawalRequestGridDefinitionFactory extends AbstractGridDefinitionFactory
{
    protected function getId(): string
    {
        return 'withdrawal_request';
    }

    protected function getName(): string
    {
        return $this->trans('Withdrawal Requests', [], 'Modules.Bwwithdrawal.Admin');
    }

    protected function getColumns(): ColumnCollection
    {
        return (new ColumnCollection())
            ->add((new DataColumn('id_withdrawal_request'))
                ->setName($this->trans('ID', [], 'Admin.Global'))
                ->setOptions(['field' => 'id_withdrawal_request', 'sortable' => true])
            )
            ->add((new DataColumn('order_reference'))
                ->setName($this->trans('Order', [], 'Admin.Global'))
                ->setOptions(['field' => 'order_reference', 'sortable' => true])
            )
            ->add((new DataColumn('customer_name'))
                ->setName($this->trans('Customer', [], 'Admin.Global'))
                ->setOptions(['field' => 'customer_name', 'sortable' => true])
            )
            ->add((new DataColumn('customer_email'))
                ->setName($this->trans('Email', [], 'Admin.Global'))
                ->setOptions(['field' => 'customer_email', 'sortable' => true])
            )
            ->add((new DataColumn('status'))
                ->setName($this->trans('Status', [], 'Admin.Global'))
                ->setOptions(['field' => 'status', 'sortable' => true])
            )
            ->add((new DataColumn('date_add'))
                ->setName($this->trans('Date', [], 'Admin.Global'))
                ->setOptions(['field' => 'date_add', 'sortable' => true])
            )
            ->add((new DataColumn('withdrawal_deadline'))
                ->setName($this->trans('Deadline', [], 'Modules.Bwwithdrawal.Admin'))
                ->setOptions(['field' => 'withdrawal_deadline', 'sortable' => true])
            )
            ->add((new DataColumn('employee_name'))
                ->setName($this->trans('Operator', [], 'Modules.Bwwithdrawal.Admin'))
                ->setOptions(['field' => 'employee_name', 'sortable' => false])
            )
            ->add(
                (new ActionColumn('actions'))
                    ->setName($this->trans('Actions', [], 'Admin.Global'))
                    ->setOptions(['actions' => $this->getRowActions()])
            );
    }

    protected function getFilters(): FilterCollection
    {
        return (new FilterCollection())
            ->add((new Filter('status', ChoiceType::class))
                ->setTypeOptions([
                    'required' => false,
                    'choices' => [
                        'Pending' => 'pending',
                        'Processing' => 'processing',
                        'Completed' => 'completed',
                        'Rejected' => 'rejected',
                    ],
                    'placeholder' => '— All statuses —',
                ])
                ->setAssociatedColumn('status')
            )
            ->add((new Filter('date_from', DateType::class))
                ->setTypeOptions(['required' => false, 'widget' => 'single_text'])
                ->setAssociatedColumn('date_add')
            )
            ->add((new Filter('date_to', DateType::class))
                ->setTypeOptions(['required' => false, 'widget' => 'single_text'])
                ->setAssociatedColumn('date_add')
            )
            ->add((new Filter('employee_name', TextType::class))
                ->setTypeOptions(['required' => false])
                ->setAssociatedColumn('employee_name')
            );
    }

    private function getRowActions(): RowActionCollection
    {
        return (new RowActionCollection())
            ->add(
                (new LinkRowAction('view'))
                    ->setName($this->trans('Edit', [], 'Admin.Actions'))
                    ->setIcon('edit')
                    ->setOptions([
                        'route' => 'admin_bwwithdrawal_requests_view',
                        'route_param_name' => 'requestId',
                        'route_param_field' => 'id_withdrawal_request',
                    ])
            );
    }
}
