<?php

namespace Maestro\Core\DataTables\Resources;

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

class ProjectTemplatesTable implements DataTableResource
{
    public function key(): string
    {
        return 'project-templates';
    }

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

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

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

    /**
     * @param  object  $model  ProjectTemplate
     * @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;
        $raw = (string) ($model->description ?? '');
        $plain = trim(preg_replace('/\s+/', ' ', strip_tags($raw)));
        if (strlen($plain) > 200) {
            $plain = substr($plain, 0, 197).'...';
        }

        return [
            'id' => $model->id,
            'name' => $model->name ?? '',
            'template_kind' => $model->template_kind ?? '',
            'description' => $plain,
            'created_at' => $formatDate($model->created_at ?? null),
            'updated_at' => $formatDate($model->updated_at ?? null),
        ];
    }
}
