<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Route;

use App\Http\Resources\UserResource;
use App\Http\Resources\FileResource;

class BundleResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
		/**
		Do not return private data on the preview page
		 */
		$full = false;
		$middleware = Route::current()->gatherMiddleware('access.guest');
		foreach ($middleware as $m) {
			if ($m === 'access.owner') {
				$full = true;
			}
		}

		$response = [
			'created_at' 		=> $this->created_at,
			'completed'			=> (bool)$this->completed,
			'expiry'			=> (int)$this->expiry,
			'expires_at'		=> $this->expires_at,
			'slug'				=> $this->slug,
			'fullsize'			=> (int)$this->fullsize,
			'title'				=> $this->title,
			'description'		=> $this->description,
			'max_downloads'		=> (int)$this->max_downloads,
			'downloads'			=> (int)$this->downloads,
			'files'				=> FileResource::collection($this->files),
			'preview_link'		=> $this->preview_link,
			'download_link'		=> $this->download_link,
			'password'			=> $this->when($full === true, $this->password),
			'owner_token'		=> $this->when($full === true, $this->owner_token),
			'preview_token'		=> $this->when($full === true, $this->preview_token),
			'deletion_link'		=> $this->when($full === true, $this->deletion_link),
			'user'				=> $this->when($full === true, new UserResource($this->user))
		];

		return $response;
	}
}
