<?php

namespace App\Services\ImportExport\Handlers;

use App\Models\User;

class UsersHandler extends AbstractModuleHandler
{
    public function key(): string
    {
        return 'users';
    }

    public function label(): string
    {
        return 'Users';
    }

    public function permissionModule(): string
    {
        return 'users';
    }

    public function exportData(array $filters): array
    {
        $users = User::query()->with('roles')->orderBy('name')->get();

        return [
            'title' => 'Users Export',
            'sheet_name' => 'Users',
            'headings' => ['Name', 'Email', 'Roles', 'Created'],
            'rows' => $users->map(fn (User $u) => [
                $u->name,
                $u->email,
                $u->roles->pluck('name')->implode(', '),
                optional($u->created_at)->format('Y-m-d'),
            ])->all(),
            'filters' => $this->filterSummary($filters),
            'totals' => ['Total Users' => $users->count()],
        ];
    }
}
