<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\Customers\DataTables;

use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Maestro\Core\DataTables\DataTableResource;
use Maestro\Core\Modules\Customers\Models\Customer;

final class CustomersDataTable implements DataTableResource
{
    public function key(): string
    {
        return 'customers';
    }

    public function authorize(Request $request): void
    {
        if (! $request->user()?->can('customers.view')) {
            throw new AuthorizationException('You do not have permission to view customers.');
        }
    }

    public function baseQuery(Request $request): Builder
    {
        return Customer::query();
    }

    /**
     * @return array<int, array<string, mixed>>
     */
    public function columns(): array
    {
        $t = (new Customer)->getTable();

        return [
            [
                'key' => 'id',
                'expr' => $t.'.id',
                'type' => 'number',
                'sortable' => true,
                'searchable' => false,
                'filterable' => false,
                'label' => __('maestro::datatable.columns.customers.id'),
                'defaultVisible' => false,
            ],
            [
                'key' => 'name',
                'expr' => $t.'.name',
                'type' => 'text',
                'sortable' => true,
                'searchable' => true,
                'filterable' => true,
                'label' => __('maestro::datatable.columns.customers.name'),
                'defaultVisible' => true,
            ],
            [
                'key' => 'email',
                'expr' => $t.'.email',
                'type' => 'text',
                'sortable' => true,
                'searchable' => true,
                'filterable' => true,
                'label' => __('maestro::datatable.columns.customers.email'),
                'defaultVisible' => true,
            ],
            [
                'key' => 'phone',
                'expr' => $t.'.phone',
                'type' => 'text',
                'sortable' => true,
                'searchable' => true,
                'filterable' => true,
                'label' => __('maestro::datatable.columns.customers.phone'),
                'defaultVisible' => true,
            ],
            [
                'key' => 'is_active',
                'expr' => $t.'.is_active',
                'type' => 'boolean',
                'sortable' => true,
                'searchable' => false,
                'filterable' => true,
                'label' => __('maestro::datatable.columns.customers.is_active'),
                'defaultVisible' => true,
            ],
            [
                'key' => 'created_at',
                'expr' => $t.'.created_at',
                'type' => 'date',
                'sortable' => true,
                'searchable' => false,
                'filterable' => true,
                'label' => __('maestro::datatable.columns.customers.created_at'),
                'defaultVisible' => false,
            ],
        ];
    }

    public function transformRow(object $model): array
    {
        /** @var Customer $model */
        $created = $model->created_at;

        return [
            'id' => $model->id,
            'name' => $model->name ?? '',
            'email' => $model->email ?? '',
            'phone' => $model->phone ?? '',
            'is_active' => (bool) ($model->is_active ?? true),
            'created_at' => $created instanceof \DateTimeInterface ? $created->format(\DateTimeInterface::ATOM) : (string) $created,
        ];
    }
}
