<?php

namespace App\Http\Resources;

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

class NotificationsResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $notification  = $this->relationLoaded('notification') ? $this->whenLoaded('notification') : null;
        return[
            'id' => $this->id,
            'idnotification' => $this->idnotification,
            'idreceiver' => $this->idreceiver,
            'date_seen' => $this->date_seen,
            'status_N' => $this->status_N,
            $this->mergeWhen($notification, fn() => [
                'receiver_rut' => isset($notification->sender->rut) ? $notification->sender->rut : '',
                'receiver_email' => isset($notification->sender->email) ? $notification->sender->email : '',
                'receiver_username' => isset($notification->sender->username) ?  $notification->sender->username : '',
                'idsender' => $notification->idsender,
                'message_title' => $notification->message_title,
                'message_body' => $notification->message_body,
                'data_json' => json_decode($notification->data_json),
                'date_sent' => $notification->date_sent,
                'idcompany' => $notification->idcompany,
            ]),
            'status' => $this->status
        ];
    }
}


