<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class ShopperRequestResource extends JsonResource
{
    public function toArray($request)
    {
        $locale = $this->getLocale($request);

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

            'buy_from'              => $this->{"buy_from_{$locale}"},
            'deliver_to'            => $this->{"deliver_to_{$locale}"},

            'deliver_latitude'      => $this->deliver_latitude,
            'deliver_longitude'     => $this->deliver_longitude,

            'remove_box_tags'       => (bool) $this->remove_box_tags,
            'over_2kg'              => (bool) $this->over_2kg,
            'includes_tobacco'      => (bool) $this->includes_tobacco,
            'includes_liquids_or_medicines' => (bool) $this->includes_liquids_or_medicines,

            'notes'                 => $this->notes,
            'status'                => $this->status,
            'items_count'           => $this->items_count,
            'total_estimated_price' => $this->total_estimated_price,
            'published_at'          => $this->published_at,
            'created_at'            => $this->created_at,
            'updated_at'            => $this->updated_at,

            'items' => ShopperItemResource::collection($this->whenLoaded('items')),
        ];
    }

    private function getLocale($request): string
    {
        $lang = $request->header('Accept-Language', 'en');
        return match (true) {
            str_starts_with($lang, 'ar') => 'ar',
            str_starts_with($lang, 'zh') => 'zh',
            default => 'en',
        };
    }
}