<?php

namespace Maestro\Core\DataTables\Resources;

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

class ChecklistsTable implements DataTableResource
{
    public function key(): string
    {
        return 'checklists';
    }

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

    public function baseQuery(Request $request): Builder
    {
        return Checklist::query()
            ->select([
                'checklists.id',
                'checklists.name',
                'checklists.description',
                'checklists.created_at',
                'checklists.updated_at',
            ])
            ->withCount('items')
            ->withCount([
                'items as required_items_count' => fn ($q) => $q->where('required', true),
            ]);
    }

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

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