<?php

namespace App\Http\Resources;

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

class VendorResource extends JsonResource
{
    public function toArray($request): array
    {
        return [
            'id'             => $this->id,
            'store_name'     => $this->store_name,
            'slug'           => $this->slug,
            'description'    => $this->description,
            'logo'           => $this->logo ? Storage::disk('public')->url($this->logo) : null,
            'banner'         => $this->banner ? Storage::disk('public')->url($this->banner) : null,
            'phone'          => $this->phone,
            'email'          => $this->email,
            'address'        => $this->address,
            'locality'       => $this->locality,
            'tier'           => $this->tier,
            'rating'         => [
                'average' => (float) $this->rating_average,
                'count'   => $this->rating_count,
            ],
            'total_orders'   => $this->total_orders,
            'responsiveness' => [
                'score'       => $this->response_score,   // 0-100, or null until enough data
                'avg_minutes' => $this->avg_response_seconds ? (int) round($this->avg_response_seconds / 60) : null,
                'fast'        => (int) ($this->response_score ?? 0) >= \App\Services\VendorResponseService::FAST_THRESHOLD,
            ],
            'min_order_value'=> (float) $this->min_order_value,
            'delivery_zones' => $this->delivery_zones,
            'category'       => $this->whenLoaded('category', fn() => new CategoryResource($this->category)),
        ];
    }
}
