<?php

namespace App\Http\Resources;

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

class ProviderClientBlockResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id'            => $this->id,
            'provider_id'   => $this->provider_id,
            'client_id'     => $this->client_id,
            'client_name'   => $this->client->user->name     ?? null,
            'client_email'  => $this->client->user->email    ?? null,
            'client_phone'  => $this->client->user->phone    ?? null,
            'client_rating' => $this->client->average_rating ?? null,
            'client_photo'  => $this->resolveClientPhoto(),
            'created_at'    => $this->created_at?->format('Y-m-d H:i'),
            'updated_at'    => $this->updated_at?->format('Y-m-d H:i'),
        ];
    }

    private function resolveClientPhoto(): ?string
    {
        $path = $this->client->profileMedia->path ?? null;

        return $path ? Storage::temporaryUrl($path, now()->addMinutes(60)) : null;
    }
}
