<?php

namespace App\Http\Resources\User;

use App\Http\Resources\BaseResource;
use App\Models\Bot;
use Illuminate\Support\Collection;

class UserResource extends BaseResource
{
    public function toArray($request = NULL): array
    {
        $permissions = collect($this->getAllPermissions())
            ->pluck("name")
            ->toArray();

        $isAuthorizedUser = auth()->user()->id === $this->id;

        $this->load("createdUser");

        $tags = $this->tags->pluck("tag");

        if($this->role === "root") {
            $tags = Bot::select("tag")->groupBy("tag")->get()->pluck("tag");
        }

        return [
            "id" => $this->id,
            "name" => $this->name,
            "token" => $this->token,
            "email" => $this->email,
            "is_paused" => $this->is_paused,
            "expired_at" => $this->expired_at?->format("Y-m-d H:i:s"),
            "created_at" => $this->created_at?->format("Y-m-d H:i:s"),
            "updated_at" => $this->updated_at?->format("Y-m-d H:i:s"),
            "role" => $this->getRoleNames()->first(),
            "tags" => $tags,
            "permissions" => $permissions,
            "createdUser" => $this->createdUser ? [
                "id" => $this->createdUser->id,
                "name" => $this->createdUser->name,
            ] : NULL,
        ];
    }
}
