<?php

namespace Maestro\Core\DataTables\Resources;

use Maestro\Core\DataTables\DataTableResource;
use App\Models\Department;
use App\Services\Access\PermissionResolver;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

/**
 * DataTable resource for departments. Table: departments.
 * Safe columns: id, name, sort_order, is_active, created_at, updated_at.
 */
class DepartmentsTable implements DataTableResource
{
    public function key(): string
    {
        return 'departments';
    }

    /**
     * Require authenticated user with module access to departments (resolver global or Spatie).
     */
    public function authorize(Request $request): void
    {
        if (! $request->user()) {
            throw new AuthorizationException('You are not authorized to access this DataTable resource.');
        }
        $resolver = app(PermissionResolver::class);
        if (! $resolver->userHasModuleAccess($request->user(), 'departments.view')) {
            throw new AuthorizationException('You do not have permission to view departments.');
        }
    }

    public function baseQuery(Request $request): Builder
    {
        return Department::query()->select([
            'id', 'name', 'sort_order', 'is_active', 'created_at', 'updated_at',
        ]);
    }

    public function columns(): array
    {
        return [
            [
                'key' => 'id',
                'expr' => 'departments.id',
                'type' => 'number',
                'sortable' => true,
                'searchable' => false,
                'filterable' => false,
                'filterMode' => 'equals',
                'label' => __('datatable.columns.departments.id'),
                'defaultVisible' => false,
            ],
            [
                'key' => 'name',
                'expr' => 'departments.name',
                'type' => 'text',
                'sortable' => true,
                'searchable' => true,
                'filterable' => true,
                'filterMode' => 'contains',
                'label' => __('datatable.columns.departments.name'),
                'defaultVisible' => true,
            ],
            [
                'key' => 'sort_order',
                'expr' => 'departments.sort_order',
                'type' => 'number',
                'sortable' => true,
                'searchable' => false,
                'filterable' => true,
                'filterMode' => 'equals',
                'label' => __('datatable.columns.departments.sort_order'),
                'defaultVisible' => true,
            ],
            [
                'key' => 'is_active',
                'expr' => 'departments.is_active',
                'type' => 'boolean',
                'sortable' => true,
                'searchable' => false,
                'filterable' => true,
                'filterMode' => 'equals',
                'label' => __('datatable.columns.departments.is_active'),
                'defaultVisible' => true,
            ],
            [
                'key' => 'created_at',
                'expr' => 'departments.created_at',
                'type' => 'date',
                'sortable' => true,
                'searchable' => false,
                'filterable' => true,
                'filterMode' => 'date_equals',
                'label' => __('datatable.columns.departments.created_at'),
                'defaultVisible' => false,
            ],
            [
                'key' => 'updated_at',
                'expr' => 'departments.updated_at',
                'type' => 'date',
                'sortable' => true,
                'searchable' => false,
                'filterable' => true,
                'filterMode' => 'date_equals',
                'label' => __('datatable.columns.departments.updated_at'),
                'defaultVisible' => false,
            ],
        ];
    }

    /**
     * @param  object  $model  Department instance
     * @return array<string, mixed>
     */
    public function transformRow(object $model): array
    {
        $formatDate = fn ($v) => $v instanceof \DateTimeInterface ? $v->format('Y-m-d H:i:s') : (string) $v;

        return [
            'id' => $model->id,
            'name' => $model->name ?? '',
            'sort_order' => (int) ($model->sort_order ?? 0),
            'is_active' => (bool) ($model->is_active ?? true),
            'created_at' => $formatDate($model->created_at ?? null),
            'updated_at' => $formatDate($model->updated_at ?? null),
        ];
    }
}
