<?php

namespace App\Http\Resources\Tenant;

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

class PhotoResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $tenantId = $request->attributes->get('tenant_id');
        $showSensitive = $this->event->tenant_id === $tenantId;
        $fullUrl = $this->getSignedUrl('full');
        $thumbnailUrl = $this->getSignedUrl('thumbnail');

        return [
            'id' => $this->id,
            'filename' => $this->filename,
            'original_name' => $this->original_name,
            'mime_type' => $this->mime_type,
            'size' => (int) ($this->size ?? 0),
            'url' => $showSensitive ? ($fullUrl ?? $thumbnailUrl) : ($thumbnailUrl ?? $fullUrl),
            'thumbnail_url' => $thumbnailUrl ?? $fullUrl,
            'width' => $this->width,
            'height' => $this->height,
            'is_featured' => (bool) ($this->is_featured ?? false),
            'status' => $showSensitive ? $this->status : 'approved',
            'moderation_notes' => $showSensitive ? $this->moderation_notes : null,
            'live_status' => $showSensitive ? $this->live_status?->value ?? $this->live_status : null,
            'live_approved_at' => $showSensitive ? $this->live_approved_at?->toISOString() : null,
            'live_reviewed_at' => $showSensitive ? $this->live_reviewed_at?->toISOString() : null,
            'live_rejection_reason' => $showSensitive ? $this->live_rejection_reason : null,
            'live_priority' => $showSensitive ? (int) ($this->live_priority ?? 0) : null,
            'likes_count' => (int) ($this->likes_count ?? $this->likes()->count()),
            'is_liked' => false,
            'uploaded_at' => $this->created_at->toISOString(),
            'uploader_name' => $this->guest_name ?? null,
            'ingest_source' => $this->ingest_source,
            'event' => [
                'id' => $this->event->id,
                'name' => $this->event->name,
                'slug' => $this->event->slug,
            ],
        ];
    }

    /**
     * Get signed URL for variant
     */
    private function getSignedUrl(string $variant): ?string
    {
        if (empty($this->id) || empty($this->event?->slug)) {
            return null;
        }

        return \URL::temporarySignedRoute(
            'api.v1.tenant.events.photos.asset',
            now()->addMinutes(30),
            [
                'event' => $this->event->slug,
                'photo' => $this->id,
                'variant' => $variant === 'thumbnail' ? 'thumbnail' : 'full',
            ]
        );
    }
}
