<?php

namespace Maestro\Core\DataTables\Resources;

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

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

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

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

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

    /**
     * @param  object  $model  Sector 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 ?? '',
            'description' => $model->description ?? '',
            '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),
        ];
    }
}
