<?php

namespace App\Http\Resources\Api\App\Notification;

use App\Models\User;
use Illuminate\Http\Resources\Json\JsonResource;

class NotificationResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        $data = $this->data;

        if (isset($data['sender_data']) && $data['sender_data']) {
            $sender_data         = $data['sender_data'];
            $user                = User::find($sender_data['id']);
            $sender['id']        = $sender_data['id'];
            $sender['full_name'] = $sender_data['full_name'];
            $sender['image']     = $user ? $user->image : $sender_data['image'];
        }

        if (isset($data['title']) && is_array($data['title']) && isset($data['body']) && is_array($data['body'])) {
            $data['title']         = $data['title'][app()->getLocale()];
            $data['body']          = $data['body'][app()->getLocale()];
            $data['notify_id']     = isset($data['notify_id']) && $data['notify_id'] ? $data['notify_id'] : null;
            $data['notify_type']   = isset($data['notify_type']) && $data['notify_type'] ? $data['notify_type'] : null;
            $data['notify_status'] = isset($data['notify_status']) && $data['notify_status'] ? $data['notify_status'] : null;
        }

        $return_data =  [
            'id'         => $this->id,
            'read_at'    => $this->read_at?->timestamp,
            'created_at' => $this->created_at?->timestamp,
            'is_read'    => $this->read_at ? true : false,
        ] + $data;

        return $return_data;
    }
}
