<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id'             => $this->id,
            'full_name'      => $this->full_name,
            'furigana_name'  => $this->furigana_name,
            'name'           => $this->name,
            'gender'         => $this->gender,
            'age'            => $this->age,
            'post_code'      => $this->post_code,
            'address'        => $this->address,
            'address2'       => $this->address2,
            'line_id'        => $this->line_id,
            'email'          => $this->email,
            'phone_number'   => $this->phone_number,
            'contact_method' => $this->contact_method,
            'introduction'   => $this->introduction,
            'projectCount'   => $this->projectCount,
            'posterCount'    => $this->posterCount,
            'avatar'         => ($this->avatar != null && $this->avatar != "") ? $this->avatar : 'avatar.png',
            'login_url'      => $this->login_url,
            'created_by'     => $this->created_by,
            'roles'          => array_map(
                function ($role) {
                    return $role['name'];
                },
                $this->roles->toArray()
            ),
            'permissions'    => array_map(
                function ($permission) {
                    return $permission['name'];
                },
                $this->getAllPermissions()->toArray()
            ),
        ];
    }
}
