<?php

namespace App\Http\Resources;

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

class ServiceRequestResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $lang = $request->header('Accept-Language') ?? 'ru';
        if ($this->problem) {
            $problem = new ProblemResource($this->problem);
        } else {
            $problem = null;
        }

        return [
            'id' => $this->id,
            'service' => new ServiceResource($this->service),
            'power' => $this->power ? [
                'id' => $this->power->id,
                'name' => $this->power->name[$lang],
                'power' => $this->power->power,
            ] : null,
            'city' => new CityWithRegionResource($this->city),
            'comment' => $this->comment,
            'phone' => $this->phone,
            'full_name' => $this->full_name,
            'status' => new StatusResource($this->getStatus),
            'problem' => $problem,
        ];
    }
}
