<?php

namespace App\Http\Resources\Roles;

use Illuminate\Http\Resources\Json\JsonResource;

class UserWithRolesResource extends JsonResource
{
    public function toArray($request): array
    {
        $roles = $this['roles'] ?? [];
        if (is_string($roles)) {
            $roles = array_filter(array_map('trim', explode(',', $roles)));
        }

        return [
            'id' => (int) ($this['id'] ?? 0),
            'firstname' => (string) ($this['firstname'] ?? ''),
            'lastname' => (string) ($this['lastname'] ?? ''),
            'email' => (string) ($this['email'] ?? ''),
            'account_id' => (string) ($this['account_id'] ?? ''),
            'roles' => array_values(array_filter($roles)),
        ];
    }
}
