<?php

namespace App\DataTables;

use App\Models\User;
use Illuminate\Database\Eloquent\Builder as QueryBuilder;
use Yajra\DataTables\EloquentDataTable;
use Yajra\DataTables\Html\Builder as HtmlBuilder;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Services\DataTable;

class UsersDataTable extends DataTable
{
    /**
     * Build the DataTable class.
     *
     * @param QueryBuilder<User> $query Results from query() method.
     */
    public function dataTable(QueryBuilder $query): EloquentDataTable
    {
        return (new EloquentDataTable($query))
            ->editColumn('status', function ($row) {
                if ($row->status == 1) {
                    return '<span class="badge bg-success-transparent text-success rounded-pill px-3 py-2">Active</span>';
                }
                return '<span class="badge bg-danger-transparent text-danger rounded-pill px-3 py-2">Inactive</span>';
            })
            ->editColumn('state.name', function ($row) {
                return $row->state?->name ?? '<span class="text-muted">N/A</span>';
            })
            ->editColumn('created_at', function ($row) {
                return $row->created_at ? $row->created_at->format('Y-m-d H:i:s') : '';
            })
            ->rawColumns(['status', 'state.name']);
    }

    /**
     * Get the query source of dataTable.
     *
     * @return QueryBuilder<User>
     */
    public function query(User $model): QueryBuilder
    {
        return $model->newQuery()->with('state');
    }

    /**
     * Optional method if you want to use the html builder.
     */
    public function html(): HtmlBuilder
    {
        return $this->builder()
                    ->setTableId('users-table')
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->orderBy(0, 'asc')
                    ->selectStyleSingle()
                    ->parameters([
                        'responsive' => true,
                        'autoWidth' => false,
                    ])
                    ->buttons([
                        Button::make('excel'),
                        Button::make('csv'),
                        Button::make('pdf'),
                        Button::make('print'),
                        Button::make('reset'),
                        Button::make('reload')
                    ]);
    }

    /**
     * Get the dataTable columns definition.
     */
    public function getColumns(): array
    {
        return [
            Column::make('id')->title('ID')->addClass('align-middle'),
            Column::make('name')->title('Name')->addClass('align-middle'),
            Column::make('email')->title('Email')->addClass('align-middle'),
            Column::make('mobile')->title('Mobile')->addClass('align-middle'),
            Column::make('state.name')->title('State')->addClass('align-middle')->orderable(false),
            Column::make('status')->title('Status')->addClass('align-middle text-center'),
            Column::make('device_type')->title('Device Type')->addClass('align-middle'),
            Column::make('device_name')->title('Device Name')->addClass('align-middle'),
            Column::make('created_at')->title('Registered At')->addClass('align-middle'),
        ];
    }

    /**
     * Get the filename for export.
     */
    protected function filename(): string
    {
        return 'Users_' . date('YmdHis');
    }
}
