<?php

namespace App\Http\Resources;

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

class CustomScheduleResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id'              => $this->id,
            'schedule_id'     => $this->schedule_id,
            'address_type'    => $this->address_type,
            'service_type_id' => $this->service_type_id,

            'service_type_name' => $this->whenLoaded('serviceType', function () {
                return $this->serviceType->description;
            }),

            'description' => $this->description,
            'min_price'   => number_format($this->min_price, 2, '.', ''),
            'max_price'   => number_format($this->max_price, 2, '.', ''),
            'offers_meal' => $this->offers_meal,
            'created_at'  => $this->created_at?->format('Y-m-d H:i'),
            'updated_at'  => $this->updated_at?->format('Y-m-d H:i'),

            'schedule' => $this->whenLoaded('schedule', function () {
                return [
                    'id'          => $this->schedule->id,
                    'client_id'   => $this->schedule->client_id,
                    'provider_id' => $this->schedule->provider_id,
                    'client_name' => $this->schedule->client?->user?->name,

                    'customer_photo' => (function () {
                        $path = $this->schedule->client?->profileMedia?->path;
                        return $path ? Storage::temporaryUrl($path, now()->addMinutes(60)) : null;
                    })(),

                    'address_id'  => $this->schedule->address_id,

                    'address' => $this->schedule->address ? [
                        'address'    => $this->schedule->address->address,
                        'number'     => $this->schedule->address->number,
                        'district'   => $this->schedule->address->district,
                        'complement' => $this->schedule->address->complement,
                        'zip_code'   => $this->schedule->address->zip_code,
                        'city'       => $this->schedule->address->city,
                        'state'      => $this->schedule->address->state,

                        'address_full' => implode(', ', array_filter([
                            $this->schedule->address->address,
                            $this->schedule->address->number ? "nº {$this->schedule->address->number}" : null,
                            $this->schedule->address->district,
                            $this->schedule->address->city ? "{$this->schedule->address->city->name}/{$this->schedule->address->state?->code}" : null,
                        ])),
                    ] : null,

                    'date'        => $this->schedule->date?->format('d/m/Y'),
                    'period_type' => $this->schedule->period_type,
                    'start_time'  => $this->schedule->start_time,
                    'end_time'    => $this->schedule->end_time,
                    'status'      => $this->schedule->status,
                ];
            }),

            'specialities' => $this->whenLoaded('specialities', function () {
                return $this->specialities->map(function ($item) {
                    return [
                        'id'   => $item->id,
                        'name' => $item->description,
                    ];
                });
            }),
        ];
    }
}
