<?php

namespace Ignite\Core\Models\Grid;

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

class ReportTable extends EloquentTable
{
    /** @var DataTables */
    protected DataTables $datatables;

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

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

        $this->formatter = $formatter;
    }

    /**
     * The report query.
     *
     * @return Builder
     */
    public function query(): Builder
    {
        return QueryPermissionFilters::for('core.report.browse')->apply(Report::query());
    }

    /**
     * The columns to show.
     *
     * @return array
     */
    public function columns(): array
    {
        return [
            'action' => [
                'title' => 'Actions',
                'name' => 'actions',
                'searchable' => false,
                'orderable' => false,
                'exportable' => false,
                'printable' => false,
            ],
            'name' => [
                'title' => 'Name',
                'name' => 'name',
                'searchable' => true,
                'orderable' => true,
                'exportable' => true,
                'printable' => true,
            ],
            'status' => [
                'title' => 'Status',
                'name' => 'status',
                'searchable' => true,
                'orderable' => true,
                'exportable' => true,
                'printable' => true,
            ],
            'sensitive' => [
                'name' => 'sensitive',
                'title' => 'Contains Sensitive Data',
                'searchable' => true,
                'orderable' => true,
                'exportable' => true,
                'printable' => true,
            ],
        ];
    }

    /**
     * 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); },
            'status' => function ($model) { return $this->formatter->statusLabel($model); },
            'created_at' => function ($model) { return $this->formatter->createdAt($model); },
            'sensitive' => function ($model) { return $this->formatter->sensitive($model); },
        ];
    }

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