<?php

namespace App\Http\Resources;

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

class AvailableShopperRequestResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        $locale = app()->getLocale();

        return [
            'id' => $this->id,

            'shopper' => [
                'id' => $this->user?->id,
                'name' => $this->user?->name,
                'phone' => $this->user?->phone_number,

                'avatar_url' => $this->user?->avatar_url
                    ? asset('storage/' . $this->user->avatar_url)
                    : null,
            ],

            'buy_from' => match ($locale) {
                'ar' => $this->buy_from_ar,
                'zh' => $this->buy_from_zh,
                default => $this->buy_from_en,
            },

            'deliver_to' => match ($locale) {
                'ar' => $this->deliver_to_ar,
                'zh' => $this->deliver_to_zh,
                default => $this->deliver_to_en,
            },

            'deliver_location' => [
                'latitude' => $this->deliver_latitude,
                'longitude' => $this->deliver_longitude,
            ],

            'items_count' => $this->items_count,
            'total_estimated_price' => $this->total_estimated_price,
            'notes' => $this->notes,
            'published_at' => $this->published_at?->toDateTimeString(),

            'items' => $this->whenLoaded('items', function () use ($locale) {
                return $this->items->map(function ($item) use ($locale) {

                    return [
                        'id' => $item->id,

                        'name' => match ($locale) {
                            'ar' => $item->name_ar,
                            'zh' => $item->name_zh,
                            default => $item->name_en,
                        },

                        'details' => match ($locale) {
                            'ar' => $item->details_ar,
                            'zh' => $item->details_zh,
                            default => $item->details_en,
                        },

                        'price' => $item->price,
                        'currency' => $item->currency,
                        'quantity' => $item->quantity,
                        'total_price' => $item->total_price,
                        'gallery' => $item->gallery ?? [],
                    ];
                });
            }),
        ];
    }
}