<?php

namespace App\Http\Resources;

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

class ReviewDetailResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        $schedule = $this->schedule;

        $customSchedule = $schedule?->customSchedule;

        $address = $schedule?->address;

        return [
            'id'         => $this->id,
            'origin'     => $this->origin,
            'origin_id'  => $this->origin_id,
            'stars'      => $this->stars,
            'comment'    => $this->comment,
            'created_at' => $this->created_at?->format('Y-m-d H:i'),

            'improvements' => $this->whenLoaded('improvements', function () {
                return $this->improvements->map(fn($imp) => [
                    'id'          => $imp->id,
                    'description' => $imp->description,
                ]);
            }),

            'schedule' => $schedule ? [
                'id'            => $schedule->id,
                'schedule_type' => $schedule->schedule_type,
                'status'        => $schedule->status,
                'date'          => $schedule->date?->format('d/m/Y'),
                'start_time'    => $schedule->start_time,
                'end_time'      => $schedule->end_time,
                'period_type'   => $schedule->period_type,
                'total_amount'  => $schedule->total_amount,
                'code'          => $schedule->code,

                'client' => $schedule->client ? [
                    'id'    => $schedule->client->id,
                    'name'  => $schedule->client->user?->name,
                    'email' => $schedule->client->user?->email,
                    'phone' => $schedule->client->user?->phone,
                ] : null,

                'provider' => $schedule->provider ? [
                    'id'    => $schedule->provider->id,
                    'name'  => $schedule->provider->user?->name,
                    'email' => $schedule->provider->user?->email,
                    'phone' => $schedule->provider->user?->phone,
                ] : null,

                'address'  => $address ? [
                    'street'       => $address->street,
                    'number'       => $address->number,
                    'complement'   => $address->complement,
                    'neighborhood' => $address->neighborhood,
                    'city'         => $address->city?->name,
                    'state'        => $address->state?->name,
                ] : null,

                'custom_schedule' => $customSchedule ? [
                    'address_type' => $customSchedule->address_type,
                    'service_type' => $customSchedule->serviceType?->description,

                    'specialities' => $customSchedule->specialities
                        ? $customSchedule->specialities->map(fn($s) => [
                            'id'          => $s->id,
                            'description' => $s->description,
                        ])
                        : [],

                    'min_price'   => $customSchedule->min_price,
                    'max_price'   => $customSchedule->max_price,
                    'offers_meal' => $customSchedule->offers_meal,
                    'description' => $customSchedule->description,
                ] : null,
            ] : null,
        ];
    }
}
