<?php

namespace Maestro\Core\DataTables\Resources;

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

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

    /**
     * Require authenticated user with module access to job functions (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(), 'jobfunctions.view')) {
            throw new AuthorizationException('You do not have permission to view job functions.');
        }
    }

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

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

    /**
     * @param  object  $model  JobFunction 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),
        ];
    }
}
