<?php

namespace Ignite\Core\Models\Grid;

use Illuminate\Database\DatabaseManager;
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 array                $params
     * @param TransactionFormatter $formatter
     */
    public function __construct(
        DataTables $datatables,
        DatabaseManager $databaseManager,
        array $params = [],
        TransactionFormatter $formatter
    ) {
        parent::__construct($datatables, $databaseManager, $params);

        $this->formatter = $formatter;
    }

    /**
     * The columns to show.
     *
     * @return array
     */
    public function columns()
    {
        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 report query.
     *
     * @return \Illuminate\Database\Query\Builder
     */
    public function query()
    {
        $columns = collect($this->getColumns())->keyBy('name')->except('actions')->keys()->toArray();

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

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

    /**
     * Get default builder parameters.
     *
     * @return array
     */
    protected function getBuilderParameters()
    {
        $params = parent::getBuilderParameters();

        return array_merge($params, [
            'order' => [[1, 'desc']],
            'buttons' => [
                'pageLength',
                $this->getColumnVisibilityButton(),
                'csv'
            ],
        ]);
    }
}
