<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Auth;

class RewardDetailsResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $userId = $request->route('user_id');
        if(!$userId){
            $userId = Auth::user()->id;
        }

        $userReward = $this->users->where('user_id', $userId)->first();

        return [
            'id' => $this->id,
            'name' => $this->name,
            'description' => $this->description,
            'is_active' => $this->is_active,
            'milestones' =>  $this->milestones,
            'milestones_progress' => MilestoneWIthMiltestoneProgressResource::collection($this->milestones),
            'user_rewards' => new UserRewardResource($userReward),
            'voucher' => $userReward ? $userReward->voucher : null
        ];
    }
}
