<?php

namespace Maestro\Core\DataTables\Resources;

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

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

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

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

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

    /**
     * @param  object  $model  SubsidyTheme 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),
        ];
    }
}

