<?php

namespace Maestro\Core\DataTables\Resources;

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

class DefaultDocumentsTable implements DataTableResource
{
    public function key(): string
    {
        return 'default-documents';
    }

    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(), 'default_documents.view')) {
            throw new AuthorizationException('You do not have permission to view default documents.');
        }
    }

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

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

    /**
     * @param  object  $model  DefaultDocument
     * @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 ?? '',
            'created_at' => $formatDate($model->created_at ?? null),
            'updated_at' => $formatDate($model->updated_at ?? null),
        ];
    }
}
