<?php

namespace Maestro\Core\DataTables\Resources;

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

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

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

    public function baseQuery(Request $request): Builder
    {
        return DefaultTask::query()
            ->leftJoin('checklists', 'checklists.id', '=', 'default_tasks.checklist_id')
            ->leftJoin('email_templates', 'email_templates.id', '=', 'default_tasks.email_template_id')
            ->select([
                'default_tasks.id',
                'default_tasks.name',
                'default_tasks.description',
                'default_tasks.checklist_id',
                'checklists.name as checklist_name',
                'default_tasks.email_template_id',
                'email_templates.name as email_template_name',
                'default_tasks.required',
                'default_tasks.show_on_timeline',
                'default_tasks.created_at',
                'default_tasks.updated_at',
            ]);
    }

    public function columns(): array
    {
        return [
            [
                'key' => 'id',
                'expr' => 'default_tasks.id',
                'type' => 'number',
                'sortable' => true,
                'searchable' => false,
                'filterable' => false,
                'filterMode' => 'equals',
                'label' => __('datatable.columns.default-tasks.id'),
                'defaultVisible' => false,
            ],
            [
                'key' => 'name',
                'expr' => 'default_tasks.name',
                'type' => 'text',
                'sortable' => true,
                'searchable' => true,
                'filterable' => true,
                'filterMode' => 'contains',
                'label' => __('datatable.columns.default-tasks.name'),
                'defaultVisible' => true,
            ],
            [
                'key' => 'description',
                'expr' => 'default_tasks.description',
                'type' => 'text',
                'sortable' => false,
                'searchable' => true,
                'filterable' => true,
                'filterMode' => 'contains',
                'label' => __('datatable.columns.default-tasks.description'),
                'defaultVisible' => true,
            ],
            [
                'key' => 'checklist_name',
                'expr' => 'checklists.name',
                'type' => 'text',
                'sortable' => true,
                'searchable' => true,
                'filterable' => true,
                'filterMode' => 'contains',
                'label' => __('datatable.columns.default-tasks.checklist_name'),
                'defaultVisible' => true,
            ],
            [
                'key' => 'email_template_name',
                'expr' => 'email_templates.name',
                'type' => 'text',
                'sortable' => true,
                'searchable' => true,
                'filterable' => true,
                'filterMode' => 'contains',
                'label' => __('datatable.columns.default-tasks.email_template_name'),
                'defaultVisible' => true,
            ],
            [
                'key' => 'required',
                'expr' => 'default_tasks.required',
                'type' => 'boolean',
                'sortable' => true,
                'searchable' => false,
                'filterable' => true,
                'filterMode' => 'equals',
                'label' => __('datatable.columns.default-tasks.required'),
                'defaultVisible' => true,
            ],
            [
                'key' => 'show_on_timeline',
                'expr' => 'default_tasks.show_on_timeline',
                'type' => 'boolean',
                'sortable' => true,
                'searchable' => false,
                'filterable' => true,
                'filterMode' => 'equals',
                'label' => __('datatable.columns.default-tasks.show_on_timeline'),
                'defaultVisible' => true,
            ],
            [
                'key' => 'created_at',
                'expr' => 'default_tasks.created_at',
                'type' => 'date',
                'sortable' => true,
                'searchable' => false,
                'filterable' => true,
                'filterMode' => 'date_equals',
                'label' => __('datatable.columns.default-tasks.created_at'),
                'defaultVisible' => false,
            ],
            [
                'key' => 'updated_at',
                'expr' => 'default_tasks.updated_at',
                'type' => 'date',
                'sortable' => true,
                'searchable' => false,
                'filterable' => true,
                'filterMode' => 'date_equals',
                'label' => __('datatable.columns.default-tasks.updated_at'),
                'defaultVisible' => true,
            ],
        ];
    }

    /**
     * @param  object  $model  DefaultTask
     * @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 ?? '',
            'checklist_id' => $model->checklist_id,
            'checklist_name' => $model->checklist_name ?? '',
            'email_template_id' => $model->email_template_id,
            'email_template_name' => $model->email_template_name ?? '',
            'required' => (bool) $model->required,
            'show_on_timeline' => (bool) $model->show_on_timeline,
            'created_at' => $formatDate($model->created_at ?? null),
            'updated_at' => $formatDate($model->updated_at ?? null),
        ];
    }
}
