<?php

namespace Ignite\Core\Models\Grid;

use Ignite\Core\Entities\Import;
use Illuminate\Database\DatabaseManager;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Yajra\DataTables\DataTables;

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

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

    /**
     * ImportTable constructor.
     *
     * @param DataTables $datatables
     * @param DatabaseManager $databaseManager
     * @param ImportFormatter $formatter
     * @param array $params
     */
    public function __construct(
        DataTables $datatables,
        DatabaseManager $databaseManager,
        ImportFormatter $formatter,
        array $params = []
    ) {
        parent::__construct($datatables, $databaseManager, $params);
        $this->formatter = $formatter;
    }

    /**
     * The columns to show.
     *
     * @return array
     */
    public function columns(): array
    {
        return [
            'action' => [
                'title' => 'Actions',
                'name' => 'actions',
                'searchable' => false,
                'orderable' => false,
                'exportable' => false,
                'printable' => false,
                'width' => '87px',
                'style' => 'min-width:87px!important;'
            ],
            'status' => [
                'title' => 'Status',
                'name' => 'status',
                'searchable' => true,
                'orderable' => true,
                'exportable' => true,
                'printable' => true,
                'width' => '110px',
            ],
            'created_at' => [
                'title' => 'Created At',
                'name' => 'created_at',
                'searchable' => true,
                'orderable' => true,
                'exportable' => true,
                'printable' => true,
            ],
            'created_by' => [
                'title' => 'Created By',
                'name' => 'created_by',
                'searchable' => true,
                'orderable' => true,
                'exportable' => true,
                'printable' => true,
            ],
            'type' => [
                'title' => 'Type',
                'name' => 'type',
                'searchable' => true,
                'orderable' => true,
                'exportable' => true,
                'printable' => true,
            ],
            'file' => [
                'title' => 'File',
                'name' => 'file',
                'searchable' => true,
                'orderable' => true,
                'exportable' => true,
                'printable' => true,
            ],
            'records' => [
                'title' => 'Records',
                'name' => 'records',
                'searchable' => true,
                'orderable' => true,
                'exportable' => true,
                'printable' => true,
            ],
            'run_at' => [
                'title' => 'Run At',
                'name' => 'run_at',
                'searchable' => true,
                'orderable' => true,
                'exportable' => true,
                'printable' => true,
            ],
            'run_by' => [
                'title' => 'Run By',
                'name' => 'run_by',
                '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); },
            'type' => function ($model) { return $this->formatter->type($model); },
            'file' => function ($model) { return $this->formatter->file($model); },
            'created_at' => function ($model) { return $this->formatter->createdAt($model); },
            'created_by' => function ($model) { return $this->formatter->createdBy($model); },
            'run_at' => function ($model) { return $this->formatter->runAt($model); },
            'run_by' => function ($model) { return $this->formatter->runBy($model); },
        ];
    }

    /**
     * The imports query.
     *
     * @return Builder|\Illuminate\Database\Query\Builder|Collection
     */
    public function query(): Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
    {
        return Import::query();
    }

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