<?php
/**
 * Livewire Component
 *
 * @version     1.0.0
 * @package     bplan-base/globals
 * @subpackage  LivewireComponents
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 * @copyright   Copyright (C) 2025, 2026 bplan-solutions GmbH & Co. KG <https://www.bplan-solutions.de/>
 * /Δ\
 */

namespace BplanBase\Globals\Livewire\Core\Role;


use BplanBase\Globals\DataGrid\DataGrid;
use BplanBase\Globals\Enums\UserAccessLevel;
use BplanBase\Globals\Models\Role;
use BplanBase\Globals\Repositories\IndexCollectionRepository;
use BplanBase\Globals\Traits\WithDataGrid;
use Illuminate\Pagination\LengthAwarePaginator;
use Livewire\Component;
use Livewire\WithPagination;


/**
 * Livewire Component
 *
 * @version     1.0.0 / 2026-06-07
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class Index extends Component
{


/* +++ TRAITS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */


    use WithDataGrid;
    use WithPagination;


/* +++ CLASS CONSTANTS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ OBJECT MEMBERS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++ */


    /**
     * @var     array
     */
    protected array $_formatCasts = [
        'active'     => 'boolean',
        'created_at' => 'datetime',
        'internal'   => 'boolean',
        'updated_at' => 'datetime',
    ];


    /**
     * @var     array
     */
    protected array $_gridColumns = [
        'name',
        'labels',
        'active',
    ];


    /**
     * @var     string
     */
    protected string $_gridLocaleFile = 'globals::roles';


    /**
     * @var     string
     */
    protected string $_modelClass = Role::class;


    /**
     * @var     array
     */
    protected array $_searchFields = [
        'name',
    ];


    /**
     * @var     array
     */
    protected $listeners = [
        'role-created' => 'render',
        'role-saved'   => 'render',
    ];


/* +++ CLASS MEMBERS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ OBJECT METHODS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++ */


    /**
     *
     * @return      LengthAwarePaginator<Role>
     *
     * @version     1.0.0 / 2026-06-07
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    private function _getData(): LengthAwarePaginator
    {
        return IndexCollectionRepository::getRoles(
            ExtendQuery: $this->_getQuery(),
            paginate: ['pageName' => 'roles-page'],
            orderBy: $this->_getOrderBy(),
            withInactive: true,
            withInternal: \Registry::get('core.user')->hasMinAccessLevel(UserAccessLevel::Maintainer)
        );

    } // _getData()


    /**
     *
     * @return      DataGrid
     *
     * @version     1.0.0 / 2026-06-07
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    private function _getGrid(): DataGrid
    {
        $gridData = $this->_getData();

        $locale = app()->getLocale();

        return (new DataGrid('role'))
            ->setActions([
                'edit-role',
            ])
            ->setActionSvgIcons([
                'edit-role' => svg_icon('globals.svg-icons.action-edit'),
            ])
            ->setActiveFilters($this->_hasActiveFilters())
            ->setColumnCallback(function ($Item, $property, $value) use ($locale) {
                switch ($property) {
                    case 'labels':
                        $value = $Item->labels->{$locale} ?? '-';
                        break;
                }
                return $value;
            })
            ->setColumns($this->_gridColumns)
            ->setData($gridData)
            ->setLocaleFile($this->_gridLocaleFile)
            ->setSorting($this->sortColumns, $this->sortColumn, $this->sortDirection)
            ->useCaptions(true)
            ->useDefaultCreateButton(true)
            ->useDefaultSearch(true);

    } // _getGrid()


    /**
     *
     * @return      \Illuminate\Database\Eloquent\Builder|null
     *
     * @version     1.0.0 / 2026-06-07
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    protected function _getQuery()
    {
        $ExtendQuery = null;

        $this->search = trim($this->search);

        if (!empty($this->search)) {
            $ExtendQuery = Role::where('name', 'LIKE', '%'.$this->search.'%');
        }

        return $ExtendQuery;

    } // _getQuery()


    /**
     *
     * @version     1.0.0 / 2026-06-07
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function render()
    {
        return view('globals::livewire.core.role.index', [
            'grid' => $this->_getGrid(),
        ]);

    } // render()


/* +++ CLASS METHODS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++++ */


} // class Index extends Component {}
