<?php

namespace Ignite\Core\Models\Grid;

use Ignite\Core\Entities\Filters\QueryPermissionFilters;
use Illuminate\Database\DatabaseManager;
use Illuminate\Database\Query\Builder;
use Yajra\DataTables\DataTables;

class TransactionTable extends QueryTable
{
    /**
     * The excluded column keys.
     * @var array
     */
    protected $excludedKeys = [
        'internal_notes',
    ];

    /**
     * The column keys that should be always visible.
     * @var array
     */
    protected $alwaysVisible = [
        'user_id', 'type', 'value',
    ];

    /**
     * @var ParticipantFormatter
     */
    protected $formatter;

    /**
     * ParticipantTable constructor.
     *
     * @param DataTables $datatables
     * @param DatabaseManager $databaseManager
     * @param TransactionFormatter $formatter
     * @param array $params
     */
    public function __construct(
        DataTables           $datatables,
        DatabaseManager      $databaseManager,
        TransactionFormatter $formatter,
        array                $params = []
    ) {
        parent::__construct($datatables, $databaseManager, $params);

        $this->formatter = $formatter;
    }

    /**
     * The columns to show.
     *
     * @return array
     */
    public function columns(): array
    {
        return [
            'id' => [
                'title' => 'ID',
                'name' => 'transaction.id',
                'orderable' => true,
                'exportable' => true,
            ],
            'transaction_date' => [
                'title' => 'Date',
                'name' => 'transaction.transaction_date',
                'orderable' => true,
                'exportable' => true,
            ],
            'value' => [
                'title' => 'Value',
                'name' => 'transaction.value',
                'orderable' => true,
                'exportable' => true,
            ],
            'user_id' => [
                'title' => 'User ID',
                'name' => 'transaction.user_id',
                'orderable' => true,
                'exportable' => true,
            ],
            'email' => [
                'title' => 'Email',
                'name' => 'user.email',
                'orderable' => true,
                'exportable' => true,
            ],
            'first' => [
                'title' => 'First Name',
                'name' => 'user.first',
                'orderable' => true,
                'exportable' => true,
            ],
            'last' => [
                'title' => 'Last Name',
                'name' => 'user.last',
                'orderable' => true,
                'exportable' => true,
            ],
            'type' => [
                'title' => 'Type',
                'name' => 'transaction.type',
                'orderable' => true,
                'exportable' => true,
            ],
            'related_name' => [
                'title' => 'Name',
                'name' => 'transaction.related_name',
                'orderable' => true,
                'exportable' => true,
            ],
            'description' => [
                'title' => 'Description',
                'name' => 'transaction.description',
                'orderable' => true,
                'exportable' => true,
            ],
            'notes' => [
                'title' => 'Notes',
                'name' => 'transaction.notes',
                'orderable' => true,
                'exportable' => true,
            ],
        ];
    }

    /**
     * The transactions query.
     *
     * @return Builder
     */
    public function query(): Builder
    {
        $columns = collect($this->getColumns())
            ->keyBy('name')
            ->except('actions')
            ->keys()
            ->toArray();

        $query = $this->getConnection()
            ->table('core_transaction as transaction')
            ->select($columns)
            ->leftJoin('core_user as user', 'user.user_id', '=', 'transaction.user_id');

        return QueryPermissionFilters::for('core.user.transaction.browse')
            ->apply($query, 'transaction');
    }

    /**
     * Get a map of column keys => functions to format columns.
     *
     * @return array
     */
    protected function getColumnFormattingMap(): array
    {
        return [
//            'action' => function ($model) { return $this->formatter->actions($model); },
            'value' => function ($model) { return $this->formatter->balance($model); },
            'transaction_date' => function ($model) { return $this->formatter->transactionDate($model); },
            'user_id' => function ($model) { return $this->formatter->idWithLinkToUser($model); },
            'email' => function ($model) { return $this->formatter->emailWithLinkToUser($model); },
        ];
    }

    /**
     * Get default builder parameters.
     *
     * @return array
     */
    protected function getBuilderParameters(): array
    {
        return parent::getBuilderParameters([
            'order' => [
                [1, 'desc'] // Date
            ],
        ]);
    }
}
