<?php

namespace App\Http\Resources;

use App\Models\Setting;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
 * Compact video representation used anywhere a video is listed (grid, related,
 * search). Includes fake+real display stats and channel/uploader info.
 */
class VideoCardResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'slug' => $this->slug,
            'thumbnail' => $this->thumbnail,
            'duration' => (int) $this->duration,
            'is_locked' => (bool) $this->is_locked,
            'is_live' => (bool) $this->is_live,
            'is_exclusive' => (bool) $this->is_exclusive,
            'created_at' => $this->created_at,
            'category' => $this->whenLoaded('category', fn () => [
                'id' => $this->category?->id,
                'name' => $this->category?->name,
                'slug' => $this->category?->slug,
            ]),
            'stats' => [
                'views' => $this->displayViews(),
                'likes' => $this->displayLikes(),
                'comments' => $this->displayComments(),
                'shares' => $this->displayShares(),
            ],
            'uploader' => [
                'name' => Setting::get('channel_name'),
                'avatar' => Setting::get('channel_avatar'),
            ],
        ];
    }
}
