<?php

namespace Bixo\Photorequest\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class PhotorequestResource extends JsonResource
{

    public function itemLink()
    {
        return guard_url('photorequest/photorequest') . '/' . $this->getRouteKey();
    }

    public function title()
    {
        if ($this->title != '') {
            return $this->title;
        }

        if ($this->name != '') {
            return $this->name;
        }

        return 'None';
    }

    public function toArray($request)
    {
        return [
            'id' => $this->getRouteKey(),
            'title' => $this->title(),
            'slug' => $this->slug,
            'ref' => $this->ref,
            'slug' => $this->slug,
            'photographer_id' => $this->photographer_id,
            'listing_id' => $this->listing_id,
            'start' => $this->start,
            'end' => $this->end,
            'time_slot' => $this->time_slot,
            'photographer_start' => $this->photographer_start,
            'photographer_end' => $this->photographer_end,
            'status' => $this->status,
            'manager_approval' => $this->manager_approval,
            'category' => $this->category,
            'sub_category' => $this->sub_category,
            'location' => $this->location,
            'title' => $this->title,
            'details' => $this->details,
            'admin_comments' => $this->admin_comments,
            'key_location' => $this->key_location,
            'access_card' => $this->access_card,
            'parking_card' => $this->parking_card,
            'key_location_description' => $this->key_location_description,
            'access_card_description' => $this->access_card_description,
            'parking_card_description' => $this->parking_card_description,
            'occupancy' => $this->occupancy,
            'agent_present' => $this->agent_present,
            'rejected_id' => $this->rejected_id,
            'rejected_at' => $this->rejected_at,
            'reject_reason' => $this->reject_reason,
            'reject_description' => $this->reject_description,
            'created_by' => $this->created_by,
            'user_id' => $this->user_id,
            'user_type' => $this->user_type,
            'created_at' => !is_null($this->created_at) ? $this->created_at->format('Y-m-d H:i:s') : null,
            'updated_at' => !is_null($this->updated_at) ? $this->updated_at->format('Y-m-d H:i:s') : null,
            'meta' => [
                'exists' => $this->exists(),
                'link' => $this->itemLink(),
                'upload_url' => $this->getUploadURL(''),
                'workflow' => $this->workflows(),
                'actions' => $this->actions(),
            ],
        ];
    }

    /**
     * Get additional data that should be returned with the resource array.
     *
     * @param     \Illuminate\Http\Request  $request
     * @return array
     */
    public function with($request)
    {
        return [
            'meta' => [
                'exists' => $this->exists(),
                'link' => $this->itemLink(),
                'upload_url' => $this->getUploadURL(''),
                'workflow' => $this->workflows(),
                'actions' => $this->actions(),
            ],
        ];
    }

    /**
     * Get the workflows for the resource.
     *
     * @return array
     */
    private function workflows()
    {
        $arr = [];
                $workflow = $this->resource->workflow();
        foreach ($workflow->transitions($this) as $key => $transition) {
            $name = $transition->getName();
            $arr[$key]['url'] = guard_url('photorequest/photorequest/workflow/' . $this->getRouteKey() . '/' . $name);
            $arr[$key]['name'] = $name;
            $arr[$key]['key'] = $name;
            $arr[$key]['form'] = $workflow->form($transition);
            $arr[$key]['label'] = trans('photorequest::photorequest.workflow.' . $name);
        }
                return $arr;

    }
    
    /**
     * Get the actions for the resource.
     *
     * @return array
     */
    private function actions()
    {

        $arr = [];
        
        return $arr;
    }
}