<?php

namespace App\Resources;

use App\Models\User;
use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource {
    private User $user;

    public function __construct(User $user) {
        $this->user = $user;
    }

    public function toArray($request) {
        $data = array_merge(
            $this->user->toArray(),
            [
                'nicename' => $this->user->getNicename(),
                'fullname' => $this->user->getFullName(),
                'localGroup' => $this->user->localGroup()->id,
            ]);

        unset($data['password']);
        unset($data['remember_token']);
        unset($data['email_verified_at']);
        unset($data['created_at']);
        unset($data['updated_at']);
        unset($data['activation_token']);
        unset($data['activation_token_expires_at']);

        return $data;
    }

    public function getReducedData(): array {
        return [
            'id' => $this->user->id,
            'nicename' => $this->user->getNicename(),
            'fullname' => $this->user->getFullName(),
            'firstname' => $this->user->firstname,
            'lastname' => $this->user->lastname,
            'localGroup' => $this->user->localGroup()->name,
            'localGroupId' => $this->user->localGroup()->id,
        ];
    }
}
