<?php

namespace Botble\JobBoard\Tables;

use Botble\Base\Enums\BaseStatusEnum;
use Botble\Base\Facades\Html;
use Botble\JobBoard\Models\FunctionalArea;
use Botble\Table\Abstracts\TableAbstract;
use Botble\Table\Actions\DeleteAction;
use Botble\Table\Actions\EditAction;
use Botble\Table\BulkActions\DeleteBulkAction;
use Botble\Table\Columns\Column;
use Botble\Table\Columns\CreatedAtColumn;
use Botble\Table\Columns\IdColumn;
use Botble\Table\Columns\NameColumn;
use Botble\Table\Columns\StatusColumn;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Http\JsonResponse;

class FunctionalAreaTable extends TableAbstract
{
    public function setup(): void
    {
        $this
            ->model(FunctionalArea::class)
            ->addActions([
                EditAction::make()->route('functional-areas.edit'),
                DeleteAction::make()->route('functional-areas.destroy'),
            ]);
    }

    public function ajax(): JsonResponse
    {
        $data = $this->table
            ->eloquent($this->query())
            ->editColumn('name', function (FunctionalArea $item) {
                if (! $this->hasPermission('functional-areas.edit')) {
                    return $item->name;
                }

                return Html::link(route('functional-areas.edit', $item->getKey()), $item->name);
            })
            ->editColumn('is_default', function (FunctionalArea $item) {
                return $item->is_default ? trans('core/base::base.yes') : trans('core/base::base.no');
            });

        return $this->toJson($data);
    }

    public function query(): Relation|Builder|QueryBuilder
    {
        $query = $this->getModel()->query()->select([
            'id',
            'name',
            'order',
            'is_default',
            'created_at',
            'status',
        ]);

        return $this->applyScopes($query);
    }

    public function columns(): array
    {
        return [
            IdColumn::make(),
            NameColumn::make(),
            Column::make('order')
                ->title(trans('core/base::tables.order'))
                ->width(100),
            Column::make('is_default')
                ->title(trans('core/base::tables.is_default'))
                ->width(100),
            StatusColumn::make(),
            CreatedAtColumn::make(),
        ];
    }

    public function buttons(): array
    {
        return $this->addCreateButton(route('functional-areas.create'), 'functional-areas.create');
    }

    public function bulkActions(): array
    {
        return [
            DeleteBulkAction::make()->permission('functional-areas.destroy'),
        ];
    }

    public function getBulkChanges(): array
    {
        return [
            'name' => [
                'title' => trans('core/base::tables.name'),
                'type' => 'text',
                'validate' => 'required|max:120',
            ],
            'status' => [
                'title' => trans('core/base::tables.status'),
                'type' => 'select',
                'choices' => BaseStatusEnum::labels(),
                'validate' => 'required|in:' . implode(',', BaseStatusEnum::values()),
            ],
            'created_at' => [
                'title' => trans('core/base::tables.created_at'),
                'type' => 'datePicker',
            ],
        ];
    }
}
