<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class LatestEpisodeResource extends JsonResource
{
     /**
     * Transform the resource into an array.
     *
     * @param  Request  $request
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $podcast = $this->resource;
        $artwork = $podcast->latestArtwork;
        $episode = $artwork->episode;
        $artist = $artwork->artist;

        $static = config('app.static_asset_url');
        $full = $static . '/artworks/' . $artwork->filename;
        $thumb = $static . '/thumbnails/' . $artwork->filename;
        $raw = (float) $episode->episode_number;
        if (fmod($raw, 1) === 0.0) {
            // no fractional part
            $episodeNumber = (int) $raw;
        } else {
            // keep one decimal place
            $episodeNumber = round($raw, 1);
        }
        return [
            'artist_name'    => $artist->name,
            'artist_profile' => url('/artist/' . $artist->slug),
            'artist_avatar'  => $artist->avatar(),

            'artwork_full'   => $full,
            'artwork_thumb'  => $thumb,

            'episode_title'  => $episode->title,
            'episode_number' => $episodeNumber,
            'episode_date'   => $episode->episode_date->format('Y-m-d'),
            'episode_mp3'    => $episode->mp3,
            'podcast_title'  => $podcast->name,
            'podcast_archive' => url('/podcasts/' . $podcast->slug),
        ];
    }
}
